Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何在我的案例中滚动div?_Jquery_Html_Css - Fatal编程技术网

Jquery 如何在我的案例中滚动div?

Jquery 如何在我的案例中滚动div?,jquery,html,css,Jquery,Html,Css,我试图根据用户单击的链接滚动我的div内容 当我没有向下滚动时,我的代码工作正常。但是,如果我向下滚动并单击链接,内容将滚动到顶部,而不是当前的顶部位置 比如说 //works fine without scrolling the windows. ------------------------------------ <- top of my browser test1 | test1 test2 | test3 |

我试图根据用户单击的链接滚动我的div内容

当我没有向下滚动时,我的代码工作正常。但是,如果我向下滚动并单击链接,内容将滚动到顶部,而不是当前的顶部位置

比如说

        //works fine without scrolling the windows.
 ------------------------------------ <- top of my browser
    test1    | test1
    test2    |
    test3    |
             |
             |
             |
             | test2
//无需滚动窗口即可正常工作。

------------------------------------我希望我能正确理解你的问题。如果是这样,此小更新将起作用:


通过减去
$(window).scrollTop()
值,我们可以找到动画的真实位置,同时考虑到窗口的当前滚动位置。

您已经将高度设置为900px。这比浏览器的高度要高,因此您现在有2个滚动条。如果您向下滚动了浏览器滚动条(而不仅仅是
#items
滚动条),则该div的顶部将隐藏在页面顶部。没错-这就是他询问的情况,以及此处问题的目的。好的。我把它解释为当你向下滚动溢出的auto div时发生的事情。我添加了注释,以防OP不知道浏览器本身也在滚动。这正是我需要的。谢谢
-------------------------------   <- top of my browser.
test9     | 
test10    |
test11    |test10
test12    |
          |
          |
          | 
$('#items').animate({
   scrollTop: $('#'+id+'-test').offset().top - $(window).scrollTop() + $('#items').scrollTop()
}, 700);