简体   繁体   English

使用scrollTop的奇怪行为

[英]Strange behavor using scrollTop

I've a strange behavor using scrollTop JQuery function. 我有一个奇怪的行为,使用scrollTop JQuery函数。 I've a div that contains a complex text divided in different sections like this: 我有一个div,其中包含复杂的文本,分为以下不同部分:

<div id="wrapper" ... >
    <div id="section1">
        <h1>Section 1</h1>
        Lorem ipsum dolor sit amet, consectetuer... 
        ...
    </div>
    <div id="section2">
        <h1>Section 2</h1>
        Lorem ipsum dolor sit amet, consectetuer... 
        ...
    </div>
    <div id="section3">
        <h1>Section 3</h1>
        Lorem ipsum dolor sit amet, consectetuer... 
        ...
    </div>
    ...

a second DIV contains a simple list of sections and when user click on each of them I want wrapper scroll at the right section: 第二个DIV包含一个简单的部分列表,当用户单击每个部分时,我希望包装器滚动到右侧部分:

<li>
   <a href="#" onclick="customScrollTo('section1')">section1</a>
</li>
<li>
   <a href="#" onclick="customScrollTo('section2')">section2</a>
...

and this is simple the JS function: 这是简单的JS函数:

function customScrollTo (section) {

     $('#wrapper').animate({
         scrollTop: $("#wrapper div[id='" + section + "']").offset().top
     }, 200);
};

now if you try to test it in this JSfiddle: http://jsfiddle.net/Ws5F9/4/ 现在,如果您尝试在此JSfiddle中对其进行测试: http : //jsfiddle.net/Ws5F9/4/

you can see two strange behavors: 您会看到两个奇怪的行为:

  1. if you click 'section 1' than 'section 2' than 'section 3', section 3 doesn't work! 如果您单击“第1部分”而不是“第2部分”而不是“第3部分”,则第3部分无效!
  2. if you click twice on the same section link, the second time it scroll at page top 如果您在同一部分链接上单击两次,则第二次在页面顶部滚动

There are two problems with the customScrollTo method. customScrollTo方法有两个问题。

First, you are using .offset() instead of .position() . 首先,您使用.offset()而不是.position() This is returning the position relative to the document rather than the #wrapper div. 这将返回相对于文档而不是#wrapper div的位置。 This is barely noticeable here because the div is close to the top of the document anyway, but you'd run into trouble as you moved it further down the page. 这在这里几乎没有引起注意,因为div无论如何都靠近文档的顶部,但是在将其进一步移至页面下方时会遇到麻烦。

Second, when you call position it is going to return the position of the element taking into account the current scroll position , so if you are scrolled to the top of the document already it will work as expected, but any further scrolling will cause trouble, since the document has been moved. 其次,当您调用position时,它将考虑当前滚动位置返回元素的位置 ,因此,如果您已经滚动到文档的顶部,它将按预期工作,但是任何进一步的滚动都会造成麻烦,由于文档已被移动。 You need to take into account the current scroll position as well by adding it in to the final scroll value so it doesn't get 'lost'. 您还需要考虑当前滚动位置,方法是将其添加到最终滚动值中,这样它就不会丢失。

Here is an example with both issues addressed: 这是解决两个问题的示例:

function customScrollTo (section) {

     $('#wrapper').animate({
         scrollTop: $("#wrapper div[id='" + section + "']").position().top + $("#wrapper").scrollTop()
     }, 200);
 };

Use this: 用这个:

function customScrollTo (sectionId) {
    var $wrapper = $('#wrapper'),
        $section = $('#' + sectionId);
     $wrapper.animate({
         scrollTop: $wrapper.scrollTop() + $section.position().top
     }, 200);
 }

The .offset() function gets the coordinates relative to the document, whereas the .position() function gets the coordinates relative to the parent. .offset()函数获取相对于文档的坐标,而.position()函数获取相对于父文档的坐标。 But the .position().top value changes as the element is scrolled. 但是.position().top值随元素滚动而变化。 Adding the parent's .scrollTop() value can adjust for that. 添加父级的.scrollTop()值可以.scrollTop()进行调整。

jsfiddle jsfiddle

Try this, I have modified your markup as well. 试试这个,我也修改了您的标记。 See this DEMO 看到这个演示

$('#index ul').on('click', 'a', function(e) {
 var section = $(this).prop('href');
 section = section.split('#')[1];
 var top = $("#" + section)[0].offsetTop;

 $('#wrapper').stop().animate({
     scrollTop: top
 }, 200);
 return false;
 });

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

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