๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
javascript/๐Ÿ‘€ tips

mouseover / mouseenter ์ฐจ์ด, mouseleaver / mouseout ์ฐจ์ด

by HomieKim 2022. 3. 28.

์ตœ๊ทผ ์ด๋ฒคํŠธ๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉฐ ํ—ท๊ฐˆ๋ฆฌ๋Š” ๋ถ€๋ถ„์ด ์žˆ์–ด ์ •๋ฆฌํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

์ด๋ฒคํŠธ์— ๊ด€ํ•œ ๊ธฐ๋ณธ์ ์ธ ์ด๋ก ์€ ๋‹ค์Œ ๊ธ€์—์„œ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

https://hoime.tistory.com/76

 

[9์ฃผ์ฐจ ์Šคํ„ฐ๋””]40์žฅ-์ด๋ฒคํŠธ

์ด๋ฒคํŠธ ๋ธŒ๋ผ์šฐ์ €๋Š” ์ฒ˜๋ฆฌํ•ด์•ผ ํ•  ํŠน์ • ์‚ฌ๊ฑด์ด ๋ฐœ์ƒํ•˜๋ฉด ์ด๋ฒคํŠธ( event )๋ฅผ ๋ฐœ์ƒ ์‹œํ‚ต๋‹ˆ๋‹ค. ํŠน์ • ํƒ€์ž…์˜ ์ด๋ฒคํŠธ์— ๋Œ€ํ•ด ํ˜ธ์ถœ ๋  ํ•จ์ˆ˜๋ฅผ ๋ธŒ๋ผ์šฐ์ €์—๊ฒŒ ์•Œ๋ ค ํ˜ธ์ถœ์„ ์œ„์ž„ ํ•œ๋‹ค. ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ

hoime.tistory.com

 

๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ๋ฅผ ๋ณด๋ฉด mouseenter ์ด๋ฒคํŠธ์™€ mouseover ์ด๋ฒคํŠธ๋Š” ์œ ์‚ฌํ•˜๊ฒŒ ๋™์ž‘ ํ•ฉ๋‹ˆ๋‹ค.

๋ฐ˜๋Œ€๋˜๋Š” ๋™์ž‘์ธ mouseleave์™€ mouseout ์ด๋ฒคํŠธ ์—ญ์‹œ ์œ ์‚ฌํ•˜๊ฒŒ ๋™์ž‘ ๋ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด ๋‘ ์ด๋ฒคํŠธ์— ์ฐจ์ด๋ฅผ ๊ฒ€์ƒ‰ํ•ด ๋ณด๋ฉด ์ž์‹ ์š”์†Œ์—์„œ ์˜ํ–ฅ์„ ์ฃผ๋Š”์ง€ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ‹€๋ฆฐ ์„ค๋ช…์€ ์•„๋‹ˆ์ง€๋งŒ, ์ •ํ™•ํ•œ ๋™์ž‘์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์บก์ฒ˜๋ง๊ณผ ๋ฒ„๋ธ”๋ง์„ ๊ฐœ๋…์„ ์•Œ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ฐ„๋‹จํ•˜๊ฒŒ ์ •์˜ํ•ด ๋ณธ๋‹ค๋ฉด

 

capturing

์บก์ฒ˜๋ง์ด๋ž€ ์ด๋ฒคํŠธ๊ฐ€ ์ƒ์œ„ ์š”์†Œ์—์„œ ํ•˜์œ„ ์š”์†Œ ๋ฐฉํ–ฅ์œผ๋กœ ์ „ํŒŒ๋˜๋Š” ๊ณผ์ •์„ ๋งํ•ฉ๋‹ˆ๋‹ค.

์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ด๋ฒคํŠธ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  ์ตœ์ƒ์œ„(๋ธŒ๋ผ์šฐ์ €์˜ ์ตœ์ƒ์œ„ ๊ฐ์ฒด๋Š” window) ๋ถ€ํ„ฐ ํ•ด๋‹น ์ด๋ฒคํŠธ ํƒ€๊ฒŸ ๋ฐฉํ–ฅ์œผ๋กœ ์ „ํŒŒ ๋˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค.

bubbling

๋ฒ„๋ธ”๋ง์ด๋ž€ ์บก์ฒ˜๋ง ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์ณ ์ด๋ฒคํŠธ ํƒ€๊ฒŸ์—์„œ ์ƒ์œ„์š”์†Œ๋กœ ์ด๋ฒคํŠธ๊ฐ€ ์ „ํŒŒ๋˜๋Š” ๊ณผ์ •์„ ๋งํ•ฉ๋‹ˆ๋‹ค.

์ด ๋‹จ๊ณ„์—์„œ ์ƒ์œ„ ์š”์†Œ์—์„œ ์ด๋ฒคํŠธ๊ฐ€ ์บ์น˜ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

capturing ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ๋“ฑ๋ก ์‹œ ์บก์ฒ˜๋ง ์—ฌ๋ถ€๋ฅผ ์„ค์ • ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

bubbling ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ์œ„์š”์†Œ๋กœ ์ด๋ฒคํŠธ๊ฐ€ ์ „ํŒŒ๋˜๋Š” ๊ณผ์ •์—์„œ ์‹คํ–‰ ๋ฉ๋‹ˆ๋‹ค.

 

 

์œ„ ์ด๋ก ์„ ์ดํ•ดํ•˜๊ณ  ๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ์˜ ๋™์ž‘๊ณผ ์ฐจ์ด๋ฅผ ์•Œ์•„๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

mouse ์ด๋ฒคํŠธ 

mouseenter : ๋งˆ์šฐ์Šค ์ปค์„œ๋ฅผ HTML ์š”์†Œ ์•ˆ์œผ๋กœ ์ด๋™ํ–ˆ์„ ๋•Œ ๋ฐœ์ƒ (๋ฒ„๋ธ”๋ง ๋˜์ง€ ์•Š๋Š”๋‹ค)

mouseover : mouseenter ์™€ ๋™์ผํ•˜๊ฒŒ ๋งˆ์šฐ์Šค ์ปค์Šค๋ฅผ HTML ์š”์†Œ ์•ˆ์œผ๋กœ ์ด๋™ํ–ˆ์„ ๋•Œ ๋ฐœ์ƒ (๋ฒ„๋ธ”๋ง ๋œ๋‹ค)

mouseleave : ๋งˆ์šฐ์Šค ์ปค์„œ๋ฅผ HTML ์š”์†Œ ๋ฐ–์œผ๋กœ ์ด๋™ํ–ˆ์„ ๋•Œ ๋ฐœ์ƒ (๋ฒ„๋ธ”๋ง ๋˜์ง€ ์•Š๋Š”๋‹ค)

mouseout : mouseleave ์™€ ๋™์ผํ•˜๊ฒŒ ๋งˆ์šฐ์Šค ์ปค์„œ๋ฅผ HTML ์š”์†Œ ๋ฐ–์œผ๋กœ ์ด๋™ํ–ˆ์„ ๋•Œ ๋ฐœ์ƒ (๋ฒ„๋ธ”๋ง ๋œ๋‹ค)

 

์ฆ‰, ์ •๋ฆฌํ•˜์ž๋ฉด mouseover ์™€ mouseleave๋Š” ๋ฒ„๋ธ”๋ง ๋‹จ๊ณ„์—์„œ ์บ์น˜ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ•˜์œ„ ์š”์†Œ์—์„œ ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ ์ƒ์œ„ ์š”์†Œ์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ƒํ–‰ํ•ด ๋ณด๋ฉด ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<!DOCTYPE html>
<html>
<style>
  .over{
    display: flex;
    background-color: antiquewhite;
    width: 300px;
    height: 300px;
    align-items: center;
    justify-content: center;

  }
  .out {
    background-color: aqua;
    width: 150px;
    height: 150px;
  }
  .enter{
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: lightcoral;
    width: 300px;
    height: 300px;
  }
  .leave {
    background-color: firebrick;
    width: 150px;
    height: 150px;
  }
</style>
<body>
  <div class="over">
    <div class="out"></div>
  </div>
  <hr/>
  <div class="enter">
    <div class="leave"></div>
  </div>
  <script>
    const over = document.querySelector('.over');
    const out = document.querySelector('.out');
    const enter = document.querySelector('.enter');
    const leave = document.querySelector('.leave');

    over.addEventListener('mouseover',()=>{
      console.log('outerDiv ์—์„œ mouseover ๋ฐœ์ƒ');
    });

    out.addEventListener('mouseout', ()=>{
      console.log('innerDiv ์—์„œ mouseout ๋ฐœ์ƒ');
    });
    enter.addEventListener('mouseenter', ()=>{
      console.log('outerDiv ์—์„œmouseenter ๋ฐœ์ƒ');
    });
    leave.addEventListener('mouseleave', ()=>{
      console.log('innerDiv ์—์„œ mouseleave ๋ฐœ์ƒ');
    }); 
  </script>
</body>
</html>

๋Œ“๊ธ€