滚动到页面的顶部(不工作在chrome)

scroll to the top of the page (doesnt work in chrome)

本文关键字:工作 chrome 顶部 滚动      更新时间:2023-09-26

我使用的ui主题是nightsky

滚动到页面底部后,我想回到页面顶部。

我想要一个可以在控制台中执行的单行命令,而不是jsfiddle。

我已经在开发控制台尝试过了:

  1. window.scrollTo(0, $("body").offset().top);
  2. window.scrollTo(0, $("#main").offset().top();
  3. :

    $('html, body').animate({
        scrollTop: $('#main').position().top },
        1000
    );
    

    似乎在firefox中工作,而不是在chrome中

  4. $('html,body').animate({scrollTop: 0});

我正在寻找一个代码,可以在控制台执行!没有点击监听器等等……只需几行代码

$('#header')[0].scrollIntoView()

我认为主题上使用的插件sliderNav取代了滚动条,这就是为什么常规滚动方法不起作用。

试试这个(获取body中的第一个元素显示):

$('body')[0].firstElementChild.scrollIntoView();

你可以使用这个代码

<a href="#home">Home</a>
<a href="#main">Main</a>

<div id="home">
    long Text ........
</div>
<div id="main">
    long Text ........
</div>

<script type="text/javascript">
    $(document).ready(function(){
    $('a[href^="#"]').on('click',function (e) {
        e.preventDefault();
        var target = this.hash,
        $target = $(target);
        $('html, body').stop().animate({
            'scrollTop': $target.offset().top
        }, 900, 'swing', function () {
            window.location.hash = target;
        });
    });
});
</script>

和页面将有平滑滚动