我很难让jQuery
特殊事件hoverintent
与mouseleave
函数一起使用。 (我也尝试用mouseout
代替mouseleave
)
我需要使用相同的功能,以便只有当用户的鼠标减慢到灵敏度阈值以下时才会触发mouseleave
事件。
我已经在下面添加了该脚本,并且还将一个工作示例上传到http://click2fit.com/test_files/accordion_hoverintent.html
$(function () {
$(".accordion_close_leave").accordion({
event: "click hoverintent",
collapsible: true,
active: false,
autoHeight: false,
}).mouseleave(function() {
$(this).accordion({ active: false});
});
var cfg = ($.hoverintent = {
sensitivity: 100,
interval: 500
});
$.event.special.hoverintent = {
setup: function() {
$( this ).bind( "mouseover", jQuery.event.special.hoverintent.handler );
},
teardown: function() {
$( this ).unbind( "mouseover", jQuery.event.special.hoverintent.handler );
},
handler: function( event ) {
var that = this,
args = arguments,
target = $( event.target ),
cX, cY, pX, pY;
function track( event ) {
cX = event.pageX;
cY = event.pageY;
};
pX = event.pageX;
pY = event.pageY;
function clear() {
target
.unbind( "mousemove", track )
.unbind( "mouseout", arguments.callee );
clearTimeout( timeout );
}
function handler() {
if ( ( Math.abs( pX - cX ) + Math.abs( pY - cY ) ) < cfg.sensitivity ) {
clear();
event.type = "hoverintent";
event.originalEvent = {};
jQuery.event.handle.apply( that, args );
} else {
pX = cX;
pY = cY;
timeout = setTimeout( handler, cfg.interval );
}
}
var timeout = setTimeout( handler, cfg.interval );
target.mousemove( track ).mouseout( clear );
return true;
}
};
答案 0 :(得分:1)
事实证明,jQuery的hoverintent特殊事件代码在这里不起作用,因为它是专门为包含Accordion的事件选项而设计的(它被定义为手风琴标题响应以激活相关面板的事件)。 / p>
好消息是Brian Cherne's hoverintent plugin确实: - 我已经包含了下面的脚本,这里有一个工作小提琴:http://jsfiddle.net/chayacooper/GZV5V/26/
重要的是要记住将mouseleave绑定到手风琴本身,以便在用户将鼠标从整个手风琴上移开之前不会触发它。如果用户在移动时立即点击标题,则会出现一个小问题,但是我愿意接受这一点,以便能够在手风琴中使用Select元素。
$(document).ready(function() {
$("#Trigger2").accordion({
active: false,
collapsible: true
}).hoverIntent({
over: function() {},
out: function() {
$('.ui-accordion-header-active', this).trigger('click').blur();
},
timeout: 1000
})
.children('h3').hoverIntent({
over: function() {
$(this).not('.ui-accordion-header-active').trigger('click');
},
out: function() {},
timeout: 1000
});
});