javascript - 如何禁用多个滚动功能?

标签 javascript jquery html css

我正在制作一个页面,向下滚动时,页面左侧会向上滑动,页面右侧会向下滑动。 向上滚动时,页面左侧会向下滑动,页面右侧会向上滑动。

如果用户只滚动一次(使用鼠标滚轮),当你滚动多次时(在功能完成之前,左侧和右侧将继续滚动并弄乱),我有这个工作。有没有办法禁用多个卷轴?

我已经在下面添加了我的代码和 fiddle 。

<div class="left">
    <div id="left1" class="onLeft Side">
    </div>

    <div id="left2" class="onLeft Side">
    </div>

    <div id="left3" class="onLeft Side">
    </div>
</div>

<div class="right">
    <div id="right3" class="onRight Side">
    </div>

    <div id="right2" class="onRight Side">
    </div>

    <div id="right1" class="onRight Side">
    </div>
</div>

    $(document).ready(function(){
        var wholeHeight = jQuery('.right').height();
        var rightLength = jQuery('.onRight').length;
        jQuery('.right').css({top:-((wholeHeight*rightLength)-wholeHeight)});

        var leftHeight = jQuery('.left').height();
        var leftLength = jQuery('.onLeft').length;
        var tot = (leftHeight * leftLength) - leftHeight;
        console.log('tot', tot)
        $('body').bind('mousewheel', function(e){
            var height = jQuery('.left').height();
            var leftTop = jQuery('.left').position().top;
            var rightTop = jQuery('.right').position().top;
            if(e.originalEvent.wheelDelta /120 > 0) {
                if (leftTop != 0) {
                    console.log('scrolling up !');
                    jQuery('.left').animate({top:leftTop + height});
                    jQuery('.right').animate({top:rightTop - height});
                } else {
                    console.log('The up end');
                }
            } else {
                if (leftTop != -tot) {
                    console.log('scrolling down !');
                    jQuery('.left').animate({top:leftTop - height});
                    jQuery('.right').animate({top:rightTop + height});
                } else {
                    console.log('the down end')
                }
            }
        });
    });

https://jsfiddle.net/11pftj26/

谢谢

最佳答案

试试这个:

    $(document).ready(function(){
        var wholeHeight = jQuery('.right').height();
        var rightLength = jQuery('.onRight').length;
        jQuery('.right').css({top:-((wholeHeight*rightLength)-wholeHeight)});scrolling=false;
        var leftHeight = jQuery('.left').height();
        var leftLength = jQuery('.onLeft').length;
        var tot = (leftHeight * leftLength) - leftHeight;
        console.log('tot', tot)
        $('body').bind('mousewheel', function(e){
    if(scrolling)
        return;
      scrolling=true;
            var height = jQuery('.left').height();
            var leftTop = jQuery('.left').position().top;
            var rightTop = jQuery('.right').position().top;
            if(e.originalEvent.wheelDelta /120 > 0) {
                if (leftTop != 0) {
                    console.log('scrolling up !');
                    jQuery('.left').animate({top:leftTop + height},{done:function(){scrolling=false}});
                    jQuery('.right').animate({top:rightTop - height},{done:function(){scrolling=false}});
                } else {
                    console.log('The up end');
            scrolling=false;
                }
            } else {
                if (leftTop != -tot) {
                    console.log('scrolling down !');
                    jQuery('.left').animate({top:leftTop - height},{done:function(){scrolling=false}});
                    jQuery('.right').animate({top:rightTop + height},{done:function(){scrolling=false}});
                } else {
                    console.log('the down end');
        scrolling=false;
                }
            }

        });
    });

Fiddle in Action

关于javascript - 如何禁用多个滚动功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35870327/

相关文章:

javascript - 使用 JavaScript 根据星期几更改消息?

javascript - 带Js的货币转换器

jquery - 当鼠标位于下拉菜单的子菜单中时,如何保持父菜单悬停

javascript - 创建颜色选择器

html - 如何将语义 ui 项目放入 html 中的段中?

javascript - 我想在 onclick 或 onkeypress 事件触发后动态地将脚本附加到 iframe 中

javascript - 递减计数器有奇怪的行为

javascript - 由于标题固定,jQuery 滚动偏移

php - 在 html 页面中显示存储的电子邮件正文内容

javascript - 获取可排序项目来自的列表的 ID?