繁体   English   中英

滚动到div内的目标并溢出

[英]Scroll to target inside div with overflow

您好,我想使用以下选项将滚动条定位到div内:溢出:隐藏,但效果不好。 我不知道为什么我的滚动脚本表现得很奇怪。 它不会滚动到正确的目标,或者当我在一个按钮上单击两次时,它会回到另一个目标。 这有什么问题? 你可以帮帮我吗。

$('a[href^="#"]').on('click',function (e) {
    e.preventDefault();

    //shows what href contains
    var target = this.hash,
    $target = $(target);
    $('.content').stop().animate({
        'scrollTop': $target.offset().top-187 //scroll to top position on href element for example #about
    }, 1000, 'swing');
});

http://jsfiddle.net/PGnZN/1/

当应该使用.offset()时,您正在使用.position() http://api.jquery.com/position/ .offset()获取相对于文档的坐标,其中.position()从偏移量获取坐标父母 在父级上具有相对位置很重要,以便可以正确计算值。 假设您希望它显示在中间,我没有触摸187

http://api.jquery.com/offset/

$('.content').stop().animate({
    'scrollTop': $target.position().top-187 //scroll to top position on href element for example #about
}, 1000, 'swing');

的CSS

.inside{
    max-width: 680px;
    position: relative;
}

请尝试这个

$('a[href^="#"]').on('click',function (e) {
    e.preventDefault();

    //shows what href contains
    var targethash = this.hash,
    $target = $(targethash);
    $('.content').stop().animate({
        'scrollTop': $target.offset().top//-187 //scroll to top position on href element for example #about
    }, 1000, 'swing');
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM