【问题标题】:jQuery snap after horizontal scroll水平滚动后的jQuery捕捉
【发布时间】:2013-08-22 19:54:36
【问题描述】:

我有一个使用鼠标滚轮 jQuery 插件的水平滚动站点。滚动有效,但我想将每个“文章”捕捉到文档的左侧,这样一旦滚动停止,它就不会保持中途剪切。

到目前为止我的标记:

CSS

#viewport {
width:100%;
height:100%;
overflow: hidden;
}

#stage {
height:100%;
width:400%;
position: absolute;
display:block;
overflow: hidden;
}

#stage article {
width:25%;
height:100%;
position: relative;
display:block;
float:left;
}

HTML

<div id="viewport">
<section id="stage" class="clearfix">
<article>
This is the block that should snap to the left once scrolling is stopped.
</article>

<article>
This is the block that should snap to the left once scrolling is stopped.
</article>

<article>
This is the block that should snap to the left once scrolling is stopped.
</article>

<article>
This is the block that should snap to the left once scrolling is stopped.
</article>

</section>
</div>

JQUERY

$(function() {
$("html, body").mousewheel(function(event, delta) {
this.scrollLeft -= (delta * 30);
event.preventDefault();
});
});

我尝试使用这个脚本没有好的结果。似乎百分比阻止了知道上一个/下一个位置。

https://stackoverflow.com/a/8170667

提前致谢。

【问题讨论】:

    标签: jquery html css mousewheel


    【解决方案1】:

    我不知道你说的上一个/下一个位置是什么意思。

    但您可以在每次滚动结束时检查屏幕左侧是否位于最近的文章。如果没有,请滚动一点。

    也许……

    $(function() {
        $("html, body").mousewheel(function(event, delta) {
    
            var theBody = $('body');
            theBody.scrollLeft(this.scollLeft() - delta * 30);
    
            /* Snap how close, how many articles, and what direction is the scroll? */
            var tolerance = 10;
            var numberOfArticles = 4;
            var signDelta = number?number<0?-1:1:0;
    
            /* While you're not within an acceptable tolerance, get a little closer */
            while (!((theBody.scollLeft()%(theBody.width() / numberOfArticles))  > tolerance))
            {
                theBody.scrollLeft(theBody.scollLeft() - signDelta * 1);
            }
    
            event.preventDefault();
        });
    });
    

    【讨论】:

    • 大卫,我的位置是指上一篇或下一篇文章。我正在测试的脚本将检测当前距离第一篇文章有​​多远,然后计算到 scrollLeft 的距离。那没有用。我会试试你的建议。谢谢!
    • 如果你的文章宽度相等,你可以找出你是否在它们宽度的倍数,然后朝着最接近的倍数移动。因此,当 ((scrollLeft % articleWidth) == 0) 你在一篇文章上排队时(假设第一篇文章在零处排队。)
    • 大卫,我正在测试你的代码,但返回 scrollLeft() 不是一个函数。我已经修正了拼写错误,但仍然没有骰子。我所需要的是,如果 x 文章可见,则向左滚动直到它的左侧为 0。没想到会这么复杂。
    • 哦,您可能只想滚动body,而不是html。编辑:我编辑了我的答案,希望能说明
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多