ボディに mousemove イベントを追加したい要素が 2 つありますが、これらの要素を除外したいと考えています。jQueryでこれを達成するにはどうすればよいですか?
$("body").on("mousemove",function(){
//dont run following code if mouse is on these elements
})
ボディに mousemove イベントを追加したい要素が 2 つありますが、これらの要素を除外したいと考えています。jQueryでこれを達成するにはどうすればよいですか?
$("body").on("mousemove",function(){
//dont run following code if mouse is on these elements
})
除外する要素を選択してキャッシュし、オブジェクトの jQuery.index()
メソッドとtarget
プロパティを使用できevent
ます。
var $blackList = $('#elem, #elem2, #elem3'),
timeout = '';
$(document).on("mousemove", function(e) {
// Removing last timout using ID returned by setTimeout(if any)
clearTimeout(timeout);
// Setting timeout using setTimeout function,
// so the handler is executed once during each specified duration
timeout = setTimeout(function() {
// event.target returnes target element of the event
// if the index of the element in jQuery collection(an array of elements) is -1
// execute the specified code
if ($blackList.index(e.target) === -1) {
// ...
}
}, 50); // change the duration according to your needs
});