TIL 23.10.12

ν™©μ€ν•˜Β·2023λ…„ 10μ›” 12일
0

TIL

λͺ©λ‘ 보기
96/146

πŸ“ŒToday I Learned

JavaScript

ex51_CSS

JavaScript > CSS μ‘°μž‘

  1. λͺ¨λ“  νƒœκ·Έ > style 속성 제곡(인라인 μŠ€νƒ€μΌ μ‹œνŠΈ) > μžλ°”μŠ€ν¬λ¦½νŠΈ style ν”„λ‘œνΌν‹° 제곡(직접적인 μ‘°μž‘)
    • 주의! js둜 건듀인 cssλŠ” 인라인 μŠ€νƒ€μΌ μ‹œνŠΈλ‘œ λ“€μ–΄κ°„λ‹€.
// 값은 λ¬Έμžμ—΄λ‘œ ν‘œκΈ°ν•œλ‹€.
box1.style = "color: blue;"; //BOM
box1.setAttribute("style", "color:red"); //DOM

box1.style = "color:blue;";
box1.style.fontSize = "2rem"

// 맡 ν˜•νƒœλ‘œλ„ κ°€λŠ₯. μ΄λ ‡κ²Œ μ‚¬μš©ν•˜λ©΄ μΉ΄λ©œν‘œκΈ°λ²•μœΌλ‘œ λ³€κ²½ν•˜μ§€ μ•Šμ•„λ„ λœλ‹€.
box1.style["color"] = "gold"; 
box1.style["background-image"] = "url(../asset/images/catty01.png)";

box1.style.width = parseInt(box1.style.width) + 50 + "px";

//CSS 적용 방식 무관 > ν˜„μž¬ 객체에 적용된 λͺ¨λ“  CSS 속성을 λ°˜ν™˜
const list = window.getComputedStyle(box1);

alert(box1.style.width); // null -> 인라인이 μ•„λ‹ˆλ©΄ λͺ»μ½μ–΄μ˜¨λ‹€.
alert(list.width); // 200px
alert(list.getPropertyValue("width")); // 200px

  1. class μ‘°μž‘(간접적인 μ‘°μž‘)
    • className ν”„λ‘œνΌν‹°
    • classList ν”„λ‘œνΌν‹°
//2번 방법
// 클래슀 1개일 λ–„ μ‚¬μš©
// <p class="one two">
// <p class="one two three">
// content.className += " three";

// 클래슀 μ—¬λŸ¬κ°œ 일 λ•Œ μ‚¬μš©
content.classList.add("three");
content.classList.remove("two");

ex52_css

ν™”λ©΄ 클릭 > μƒμž 생성

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 150px;
        height: 150px;
        position: absolute;
        left: 0;
        top: 0;
      }
      #box1 {
        background-color: tomato;
      }
      #box2 {
        background-color: orange;
      }
      #box3 {
        background-color: gold;
      }
      #box4 {
        background-color: greenyellow;
      }
      #box5 {
        background-color: cornflowerblue;
      }
    </style>
  </head>
  <body>
    <!-- ex52_css.html -->
    <div id="box1" class="box">μƒμž1</div>
    <div id="box2" class="box">μƒμž2</div>
    <div id="box3" class="box">μƒμž3</div>
    <div id="box4" class="box">μƒμž4</div>
    <div id="box5" class="box">μƒμž5</div>

    <script>
      const list = document.getElementsByClassName("box");
      let index = 0;
      let zindex = 1;

      window.onmousedown = () => {
        // μƒμž > postion(absolute) > λ¬Έμ„œ 쒌츑 상단 κΈ°μ€€
        // clientX, clientY > λ¬Έμ„œ 쒌츑 상단 κΈ°μ€€
        // alert(event.clientX);

        list[index].style.left = event.clientX - 75 + "px"; // ν˜„μž¬ ν΄λ¦­ν•œ xμ’Œν‘œ
        list[index].style.top = event.clientY - 75 + "px";
        list[index].style.zIndex = zindex;
        index++;
        zindex++;
        if (index >= list.length) index = 0;
      };
    </script>
  </body>
</html>

ex53_css

ν™”λ©΄ 클릭 > μƒμž 생성 > ν΄λ¦­ν•œ 곳으둜 이동

window.onmousedown = () => {
  if (event.buttons == 1) {
    const box = document.createElement("div");
    box.className = "box";
    box.style.left = event.clientX - 75 + "px";
    box.style.top = event.clientY - 75 + "px";

    let r = parseInt(Math.random() * 256);
    let g = parseInt(Math.random() * 256);
    let b = parseInt(Math.random() * 256);

    box.style.backgroundColor = `rgb(${r}, ${g}, ${b})`;

    document.body.appendChild(box);
  } else if (event.buttons == 2) {
    document.body.removeChild(event.target);
  }
};

window.oncontextmenu = () => false;

ex54_css

Drag & Drop > 개체 이동 μΈν„°νŽ˜μ΄μŠ€

absolute


ex55_css

λ“œλž˜κ·Έ μ•€ λ“œλž relative


ex56_css

λ“œλž˜κ·Έ μ•€ λ“œλž absolute2

ex57_css

λ“œλž˜κ·Έ μ•€ λ“œλž relative2


ex58_css

μƒμž 크기 쑰절


ex59_css

μƒμž νšŒμ „


ex60_css

메뉴 이벀트

const item = document.querySelectorAll("#menu > img");
let olditem = null;

for (let i = 0; i < item.length; i++) {
  item[i].onclick = () => {
    if (olditem != null) {
      //메뉴 1κ°œκ°€ μ—΄λ €μžˆλ‹€.
      olditem.style.transform = "translate(0px, 0px)";
    }

    if (event.target != olditem) {
      event.target.style.transform = "translate(0px, 90px)";
      olditem = event.target;
    } else {
      event.target.style.transform = "translate(0px, 0px)";
      olditem = null;
    }
  };
}

ex61_css

μŠ¬λΌμ΄λ“œ 이미지

const list = document.getElementById("list");
const sel1 = document.getElementById("sel1");

let dist = 0;

sel1.addEventListener("change", () => {
  // alert(sel1.value);

  list.style.transform = `translate(-${sel1.value * 250}px, 0px)`;
});

// μˆ«μžν‚€ μž…λ ₯
window.onkeydown = () => {
  list.style.transform = `translate(-${
    (event.keyCode - 49) * 250
  }px, 0px)`;
};

ex62_css

λ°°κ²½ 이미지 μ‘°μž‘

const puma = document.getElementById("puma");
x = 0;
let y = 0;
let timer = 0;

puma.onclick = () => {
  if (timer == 0) {
    timer = setInterval(() => {
      x -= 512;
      if (x < -512 * 3) {
        x = 0;
        y -= 256;

        if (y < -256) {
          y = 0;
        }
      }
      puma.style.backgroundPosition = `${x}px ${y}px`;
    }, 100);
  } else {
    clearInterval(timer);
    timer = 0;
  }
};

ex63_css

μ˜μ—­λ³„ 밝기 쑰절

//슀크둀 이벀트
window.onscroll = () => {
  if (window.scrollY > 1500 && window.scrollY <= 2500) {
    document.body.style.backgroundColor = "black";
    document.body.style.color = "white";
  } else {
    document.body.style.backgroundColor = "white";
    document.body.style.color = "black";
  }
};


토이 ν”„λ‘œμ νŠΈ

둜그인 ν–ˆμ„ λ•Œ Auth에 둜그인 ν•œ μ‚¬μš©μžμ˜ idλ₯Ό μ €μž₯ν•˜λŠ” κΈ°λŠ₯을 λ§Œλ“€μ—ˆλ‹€.

둜그인, 아이디 검색, λΉ„λ°€λ²ˆν˜Έ μˆ˜μ • μ‹œ μ‘°νšŒν•˜λ €λŠ” νšŒμ›μ΄ νƒˆν‡΄ν•œ νšŒμ›μΈμ§€ ν™•μΈν•˜λŠ” μž‘μ—…μ„ μΆ”κ°€ν–ˆλ‹€. νƒˆν‡΄ν•œ νšŒμ›μ€ ν•΄λ‹Ή κΈ°λŠ₯을 ν•  수 μ—†κ²Œ μ„€μ •ν–ˆλ‹€.


μ•Œκ³ λ¦¬μ¦˜

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€μ˜ 폰켓λͺ¬ 문제λ₯Ό ν’€μ—ˆλ‹€. mapκ³Ό set으둜 ν’€μ–΄λ³΄μ•˜λ‹€.

profile
μ°¨κ·Όμ°¨κ·Ό ν•˜λ‚˜μ”©

0개의 λŒ“κΈ€