【问题标题】:Scrolling to anchor in a div jquery滚动到 div jquery 中的锚点
【发布时间】:2015-08-02 03:46:55
【问题描述】:

我在平滑滚动到具有固定导航的视差 div 中的锚点时遇到了一些问题。

该脚本从页面顶部运行良好,但一旦进入内容,链接就会驱动到错误的锚点......

我尝试了几个脚本,但遇到了同样的问题(从顶部工作正常,但从内容失败)。

$(document).ready(function() {
$(".scroll").click(function (e) {
    e.preventDefault();
    if (this.hash) {
        //get rid of hash
        var hash = this.hash.substr(1);

        //get the position of the <a name> 
        var $toElement = $("[id=" + hash + "]");
        var toPosition = $toElement.offset().top;

        //scroll/animate that element
        $(".parallax").animate({
            scrollTop: toPosition
        }, 500);
    }
});

});

这里是代码:http://jsfiddle.net/63hy5urr/1/

请帮忙。

谢谢。

【问题讨论】:

    标签: jquery scroll anchor parallax smooth


    【解决方案1】:

    您必须使用scrollTop() 方法检查.parallax 当前滚动到的位置,然后在制作动画时将此值添加到toPosition

    $(".parallax").animate({
        scrollTop: $(".parallax").scrollTop() + toPosition
    }, 500);
    

    工作小提琴:http://jsfiddle.net/63hy5urr/3/

    【讨论】:

    • 太棒了。谢谢! = )
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多