简体   繁体   English

jQuery中的ScrollTop问题(主要在Chrome中)

[英]Issues with ScrollTop in jQuery (mostly in Chrome)

So I've been having issues using scrollTop for a website. 所以我在使用scrollTop网站时遇到了问题。 I've made some test code and I've managed to get it working in Firefox, but it still refuses to work in Chrome... I really can't see what is wrong with the code. 我已经编写了一些测试代码,并且设法在Firefox中运行它,但是它仍然拒绝在Chrome中运行...我真的看不到代码有什么问题。 I have added a console.log() to show the value of the offset and this works fine, but the Chrome browser doesn't scroll to the div with id="search". 我添加了console.log()来显示偏移量的值,并且工作正常,但是Chrome浏览器不会滚动到id =“ search”的div。 Can anyone see anything that I'm doing wrong? 谁能看到我做错的任何事情?

<a href="#" id="test-btn" class="darhoudou-button">Test</a>

<div class="test-box">test1</div>
<div class="test-box">test2</div>
<div class="test-box">test3</div>
<div class="test-box">test4</div>
<div class="test-box" id="search">test5</div>
<div class="test-box">test6</div>

<script>
     $(document).ready(function(){

        $('#test-btn').on('click', function(){

            $('html, body').animate({
                scrollTop: $('#search').offset().top
            }, 200);

        });

        console.log($('#search').offset().top);         
    });
</script>

I tested your code and it has no problem. 我测试了您的代码,没有问题。

If your browser has height 500px and page 600px, You can only 100px scroll. 如果浏览器的高度为500px,页面的高度为600px,则只能滚动100px。 And if height of page is less than 500px then there is no Scroll. 并且如果页面的高度小于500px,则没有滚动。

Please change this <div class="test-box">test6</div> to this: 请将此<div class="test-box">test6</div>更改为此:

<div class="test-box" style="height:1000px;">test6</div>

And see the result, Or you can test this snippet with chrome: 并查看结果,或者您可以使用chrome测试此代码段:

  <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> <a href="#" id="test-btn" class="darhoudou-button">Test</a> <div class="test-box">test1</div> <div class="test-box">test2</div> <div class="test-box">test3</div> <div class="test-box">test4</div> <div class="test-box" id="search">test5</div> <div class="test-box" style="height:1000px;">test6</div> <script> $(document).ready(function(){ $('#test-btn').on('click', function(){ $('html, body').animate({ scrollTop: $('#search').offset().top }, 200); }); console.log($('#search').offset().top); }); </script> 

SOLVED 解决了

Turns out that it was my css file causing the issue... If anyone else experiences a similar issue, I would recommend commenting out any js and css files one by one and see if fixes it. 原来这是导致问题的我的css文件...如果其他人遇到类似的问题,我建议一一注释掉所有js和css文件,看看是否可以解决。 That might help you pinpoint which file is causing the issue... 这可能有助于您查明是哪个文件引起了问题...

The problem with my css was the following: 我的CSS的问题如下:

 html { 
    overflow: hidden;
    height: 100%;
}    


body {
    overflow: auto;
    height: 100%;
}

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

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