• 회원가입
  • 로그인
  • 구글아이디로 로그인

[Event] JQ - mouseover() 메서드 - 마우스오버이벤트 (= mouseover이벤트 = mouseover메서드 마우스오버 메서드) ※ 이벤트 발생 시 실행함수 적을 때 주의사항.

목차

  1. mouseover() 예제 - 마우스커서가 요소 위에 위치 시 배경색 변경
  2. mouseover() 정의
  3. mouseover() 구문
  4. mouseover() 예제 - cf. mouseenter(), mousemove()
  5. 이벤트 발생 시 실행 함수 적을 때 주의사항

 

mouseover() 예제 - 마우스커서가 요소 위에 위치 시 배경색 변경

 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<script>

$(document).ready(function(){

  $("p").mouseover(function(){

    $("p").css("background-color", "yellow");

  });

  $("p").mouseout(function(){

    $("p").css("background-color", "lightgray");

  });

});

</script>


<p>홈짱닷컴 Homzzang.com</p>

 

결과보기

 

mouseover() 정의

 

mouseover 이벤트는 마우스커서가 선택 요소 위에 있을 때 발생.

※ mouseover() 메서드는 2가지 작동 가능.

① mouseover 이벤트 촉발.

② mouseover 이벤트 발생 시 실행할 함수 연결.

 


 

1. cf.

 

mouseover() 메서드

: 마우스커서가 선택 요소 및 자식 요소에 들어가면 이벤트 발생.

 

mouseenter() 메서드

: 마우스커서가 선택 요소에 들어갈 때만 이벤트 발생.

 

2.

mouseout() 메서드와 주로 함께 사용됨.

 

 

mouseover() 구문

 

mouseover 이벤트 촉발.

$(selector).mouseover()

 

mouseover 이벤트에 함수 연결.

$(selector).mouseover(function)

 


[매개변수]

 

function

선택. mouseover 이벤트 발생 시 실행할 함수 지정.

 

 

mouseover() 예제 - cf. mouseenter(), mousemove()

 

<style>

div {

  width: 200px;

  height: 100px;

  border: 1px solid black;

  margin: 10px;

  float: left;

  padding: 30px;

  text-align: center;

  background-color: lightgray;

}

h3 {background-color: white; padding:20px;}

span {display;block; color:red;}

</style>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script>

var x = 0;

var y = 0;

var z = 0;

$(document).ready(function(){

  $("div.over").mouseover(function(){

    $(".over span").text(x += 1);

  });

  $("div.enter").mouseenter(function(){

    $(".enter span").text(y += 1);

  });

  $("div.move").mousemove(function(){

    $(".move span").text(z += 1);

  });

});

</script>



<div class="over">

  <h3>Mouseover 횟수: <span></span></h3>

</div>


<div class="enter">

  <h3>Mouseenter 횟수: <span></span></h3>

</div>


<div class="move">

  <h3>Mousemove 횟수: <span></span></h3>

</div>

 

결과보기


PS.

 

mouseover 이벤트

: 마우스커서가 div, h3 각각 들어갈 때 이벤트 발생.

 

mouseenter 이벤트

: 마우스커서가 div 들어갈 때만 이벤트 발생.

 

mousemove 이벤트

: 마우스커서가 div 위에서 1px 움직일 때마다 이벤트 발생.

 

 

이벤트 발생 시 실행 함수 적을 때 주의사항

 

지역함수 방식으로 실행 함수 기재하면, 정상 작동 X.

※ 지역함수 : 함수(=메서드) 안에 함수를 정의하는 형태.



[방법1]

 

<script src="http://code.jquery.com/jquery-latest.js"></script>


<style>

.hz {width:100px; height:100px; }

.r {background: red;}

.b {background: blue; display: none;}

</style>


<div class="hz r"></div>

<div class="hz b"></div>


<button onmouseover="r()">red</button>

<button onmouseover="b()">blue</button>


<script>

//$(document).ready(function() { <-- 이 부분 있으면 작동 X

  function r(){

     $('.hz').css('display','none'); $('.r').css('display','block');

  }

  function b(){

     $('.hz').css('display','none'); $('.b').css('display','block');

  }

//}); <-- 이 부분 있으면 작동 X

</script>


결과보기


[방법2] 

아래처럼 전역함수 형태로 정의 후, 호출할 때만 함수 안에 넣어야 함.

 

<script src="http://code.jquery.com/jquery-latest.js"></script>

<style>

.hz {width:100px; height:100px; }

.r {background: red;}

.b {background: blue; display: none;}

</style>

<div class="hz r"></div>

<div class="hz b"></div>

<button onmouseover="r()">red</button>

<button onmouseover="b()">blue</button>

<script>

$(document).ready(function() {

  r();  //굳이 부르겠다면 이렇게

  b();

});

function r(){

    $('.hz').css('display','none'); $('.r').css('display','block');

}

function b(){

    $('.hz').css('display','none'); $('.b').css('display','block');

}

</script>

 

결과보기 

균이 님, 플래토 님 (190918) https://sir.kr/qa/323817


방문 감사합니다. (즐겨찾기 등록: Ctrl + D)

분류 제목
Effect JQ - slideToggle() 메서드 ★★★ - slideUp() 메서드와 slideDown() 메서드를…
Effect JQ - slideUp() 메서드 - 선택요소를 위로 슬라이드해 숨기기. (= slideUp메서드 = 슬라이…
Effect JQ - stop() 메서드 - 선택요소에 대해 현재 실행중인 애니메이션 중지 (= stop메서드 = 스톱메…
Effect JQ - toggle() 메서드(effect경우) ★★★★★ - 보이기/숨기기 상호전환. (= toggle메…
HTML_CSS JQ - addClass() 메서드 ★ - 선택요소에 하나 또는 그 이상의 class 명을 추가
HTML_CSS JQ - after() 메서드 - 선택요소 뒤에 내용 추가. (= after메서드 = 애프터메서드)
HTML_CSS JQ - append() 메소드 ★★★ - 선택요소 안 끝부분에 내용 삽입 (= append메서드 = 어펜드…
HTML_CSS JQ - appendTo() 메소드 - 선택요소 안 끝에 HTML 요소삽입 (= appendTo메서드 = 어…
HTML_CSS JQ - attr() 메서드 ★★★ - 속성값 설정/반환 (= attr메서드 = 어트르 메서드 = 어트리…
HTML_CSS JQ - before() 메서드 - 선택요소 앞에 내용 삽입
HTML_CSS JQ - clone() 메서드 - 선택요소 복사본 만들기 (= 선택요소복사 = clone메서드 = 클론메서드…
HTML_CSS JQ - detach() 메서드 - 선택요소제거. (※ 데이터 및 이벤트는 유지됨).
HTML_CSS JQ - empty() 메서드 - 선택요소의 모든 자식노드와 내용 제거. (= 비우기 = empty메서드 =…
HTML_CSS JQ - hasClass() 메서드 ★★★ - 선택요소가 지정 클래스명 가지고 있는지 검사. (= 지정 클래…
HTML_CSS JQ - height() 메서드 - 높이 설정/반환. (= height메서드 = 하이트메서드)
HTML_CSS JQ - html() 메서드 ★ - 선택요소의 내용설정/내용변경/내용반환 (= html메서드 = 에이치티엠엘…
HTML_CSS JQ - innerHeight() 메서드 - 높이 반환 (height + padding) (= innerHe…
HTML_CSS JQ - innerWidth() 메서드 - 선택요소 너비 반환 (※ padding 포함 O , border …
HTML_CSS JQ - insertAfter() 메서드 ★ - 선택요소 뒤에 요소삽입/요소추가. (= insertAfter…
HTML_CSS JQ - insertBefore() 메서드 - 선택요소 앞에 요소삽입/요소추가. (= insertBefore…
9/15
목록
찾아주셔서 감사합니다. Since 2012