【问题标题】:scrollTop() does this really do anything?scrollTop() 这真的有什么作用吗?
【发布时间】:2014-04-22 11:41:24
【问题描述】:

有没有人让 scrollTop() 在容器中滚动 div?对此有很多疑问。但是使用此代码,容器 div 不会滚动到所选元素。

        $('div',$('#scroll_1')).each(function(){
        var this_text = $(this).text();
        if ( this_text.search(/now/i) != -1 || this_text.search(/soon/i) != -1 ) {
            this_id = $(this).attr('id');
            var scroll_top = $(this).offset().top;
            alert('id '+this_id+' '+scroll_top);
            $('#scroll_1').scrollTop(scroll_top);
            return false;
        }           
        });

div 是用脚本添加的

$('#scroll_1').append('<div id="'+id+'">div html</div>');

无论使用 .position().top 还是 .offset().top,scroll_top 都为 0。该脚本被多次调用。每个调用都有不同的 scroll_top 编号,但 .scrollTop(scroll_top) 不会滚动。

从容器顶部获取元素 div 的偏移量的正确方法是什么?似乎有很多关于在 div 中滚动元素的问题。有没有可行的方法?我更喜欢动画以外的方法。

【问题讨论】:

    标签: javascript


    【解决方案1】:

    scrollTop() 不接受任何参数,您可能的意思是:

    ....
    $('#scroll_1').animate({
        scrollTop: scroll_top
    }, 2000);
    ....
    

    【讨论】:

    • 是否有不同的方式来使用 scrollTop 来滚动 div?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多