2012-06-18 5 views
1

마우스가 요소에서 1 초 동안 마우스를 가져간 후에 만 ​​mouseover 이벤트를 트리거하는 방법이 있습니까?jQuery mouseover with timing

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

$(this).animate({opacity:1}, 200); 

}); 
+1

당신은 http://cherne.net/brian/resources/jquery.hoverIntent.html보고 할 수 있습니다 – j08691

+0

다음 링크를 확인하십시오. [마우스 오버시 까다로운 지연] (http://stackoverflow.com/questions/1510594/tricky-delay-on-mouseover) –

답변

1
$("img").mouseover(function() { 
    $(this).delay(1000).animate({opacity:1}, 200); 
}).mouseleave(function() { 
    $(this).clearQueue().stop().animate({opacity:0.2},200); 
});​ 

DEMO

+0

.clearQueue(). stop() 내 문제가 해결되었습니다. 정말 고맙습니다. – Mali

2

당신은 여기에있는 hoverIntent() jQuery 플러그인 사용할 수 있습니다 또한 http://cherne.net/brian/resources/jquery.hoverIntent.html

을, 그들은 모바일 브라우저에서 작동하지 않는 사물의 이러한 종류를 사용하거나 아무것도 터치를 사용할 때 당신이 조심해야 할 화면.

+0

예, 터치 스크린 앱에 마우 버 포인터 또는 호버링 이벤트로 감지 될 수있는 마우스 포인터가 내장되어 있지 않은 경우 –

0

setTimeOut 함수를 사용해야합니다.

setTimeOut(function(){$("img").mouseover(function() { 

     $(this).animate({opacity:1}, 200); 

}); 
},1000); 

밀리 초 단위로 시간이 걸립니다.

0

1 초 후에 이벤트를 처리하는 타이머 기능 ([1] 참조)을 만들 수 있습니다. 그 함수를 배열에 저장하거나 "window"에 직접 저장할 수 있습니다. 타이머 함수가 트리거되기 전에 "mouseout"이 발생하면 취소 할 수 있습니다.

[1] http://www.w3schools.com/js/js_timing.asp

3

http://jsfiddle.net/tqa2J/1/

$("img").on("mouseover mouseout", function() { 
    var tid = 0; 
    return function(e) { 
     var elem = $(this); 
     clearTimeout(tid); 
     if (e.type == "mouseover") { 
      tid = setTimeout(function() { 
       elem.stop(true).animate({ 
        opacity: 1 
       }, 200); 
      }, 1000); 
     } 
     else { 
      console.log(elem); 
      elem.stop(true).animate({ 
       opacity: 0.3 
      }, 200); 
     } 

    }; 
}());​ 
+3

jQuery 응답 코드 20 줄 ??? Pfft .. * 적은 쓰다 * 내 엉덩이. – rlemon

+0

주목해야합니다 : hoverIntent는 ~ 1.5kb의 코드 (축소 된)입니다.이 솔루션은 0.01kb 그대로입니다. – rlemon

+0

@rlemon 이것은 또한 디버깅 모듈이 내장되어 있습니다.'console.log (elem);' – Esailija