jquery 1.9.1和macbook air上的safari,.on()事件处理程序不起作用

时间:2013-12-01 17:29:18

标签: jquery safari mousewheel jquery-1.9 touchmove

我使用以下代码:

$jq191('html, body').on('DOMMouseScroll mousewheel touchmove', '.fancybox-overlay', function( e ){ e.preventDefault(); e.stopPropagation(); });

我也尝试过:

$jq191('html, body').on('DOMMouseScroll mousewheel touchmove', function( e ){ e.preventDefault(); e.stopPropagation(); });

但事件处理程序永远不会在带有safari的macbook air上触发。我不知道我做错了什么。如果我设置html,body是溢出的:隐藏,主体不会滚动,但是我不希望花式框在移除/添加垂直滚动条时移动主体的内容。感谢。

修改 在文档的jquery中它说: 如果省略selector或为null,则事件处理程序称为直接或直接绑定。每次在所选元素上发生事件时都会调用该处理程序,无论它是直接发生在元素上还是来自后代(内部)元素的气泡。

为什么然后这不会阻止冒泡的事件将fancybox一直滚动到身体? (使用:$ jq191('html,body')。on('DOMMouseScroll mousewheel touchmove',function(e){e.preventDefault(); e.stopPropagation();});)

另一个奇怪的事情是,当滚动叠加层时,当调试器打开时,它会在事件中断开,但在打破事件后,主体立即滚动。可能是一个错误。

修改 做了一个jsfiddle @ http://jsfiddle.net/Sa7AP/如果你在chrome和safari中滚动,在粉红色的盒子里面,一旦你到达粉红色盒子的末端,并继续滚动,背景也会滚动。我试图阻止在fancybox内滚动时滚动背景。

2 个答案:

答案 0 :(得分:1)

试试这个

$jq191('body').on('DOMMouseScroll mousewheel touchmove', '.fancybox-overlay', function( e ){ e.preventDefault(); e.stopPropagation(); });

$jq191('html body').on('DOMMouseScroll mousewheel touchmove', '.fancybox-overlay', function( e ){ e.preventDefault(); e.stopPropagation(); });

答案 1 :(得分:1)

已编辑(javascript逻辑已更改 - 正在工作)

好吧我发现了另一个SO问题,并使用了他们的略微修改版本:jsfiddle:http://jsfiddle.net/Sa7AP/20/

HTML:

<div class="container">
    <div class="fancybox-wrap">
       inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text inner pink text 
    </div>
    random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random text random textrandom text random text random text random text random text random text random text random text random text random text random text random text random text random text random textrandom text random text random text random text random text random text random text random text random text random text random text random text random text random text random textrandom text random text random text random text random text random text random text random text random text random text random text random text random text random text random textrandom text random text random text random text random text random text random text random text random text random text random text random text random text random text random textrandom text random text random text random text random text random text random text random text random text random text random text random text random text random text random textrandom text random text random text random text random text random text random text random text random text random text random text random text random text random text random textrandom text random text random text random text random text random text random text random text random text random text random text random text random text random text random textrandom text random text random text random text random text random text random text random text random text random text random text random text random text random text random textrandom text random text random text random text random text random text random text random text random text random text random text random text random text random text random textrandom text random text random text random text random text random text random text random text random text random text random text random text random text random text random textrandom text random text random text random text random text random text random text random text random text random text random text random text random text random text random textrandom text random text random text random text random text random text random text random text random text random text random text random text random text random text random textrandom text random text random text random text random text random text random text random text random text random text random text random text random text random text random text
</div>

的javascript:

function isBeyond( element, event )
{
    var el = $( element );
    var scrollTopHeight= el[0].scrollHeight - el[0].clientHeight;
    var delta= event.originalEvent.wheelDelta || -event.originalEvent.detail;
    if ( ( el.scrollTop() <= 0 &&  delta > 0 ) || ( el.scrollTop() >= scrollTopHeight && delta < 0 ) ) {
      return true;
    }
    return false;
}

jQuery('.fancybox-wrap').on('DOMMouseScroll mousewheel touchmove', function(e) { if( isBeyond( this, e) )
{
     e.preventDefault();
} });