简体   繁体   English

使用 JQuery 滚动到页面顶部

[英]Scroll to page top using JQuery

Here is the existing script that I am using to filter some product details in my page.这是我用来过滤页面中一些产品详细信息的现有脚本。 Currently, on filtering it loads a "loading" GIF animation.目前,在过滤时,它会加载“加载”GIF animation。 Instead of that I want it to scroll to the top using simple div id function.相反,我希望它使用简单的 div id function 滚动到顶部。 consider the page URL is http://websitename.com/folder/index.php , I am able to scroll to the top using考虑页面 URL 是http://websitename.com/folder/index.php ,我可以使用滚动到顶部

<a href="#top" class="nav-link">Go to top</a>

How can I automatically scroll to the top by changing this JQuery script?如何通过更改此 JQuery 脚本自动滚动到顶部? I want to use this function instead of <div id="loading" style="" ></div> , how can I do that?我想用这个 function 而不是<div id="loading" style="" ></div> ,我该怎么做?

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

    filter_data();

    function filter_data()
    {
        $('.filter_data').html('<div id="loading" style="" ></div>');
        var action = 'fetch_data';
        var minimum_price = $('#hidden_minimum_price').val();
        var maximum_price = $('#hidden_maximum_price').val();
        var brand = get_filter('brand');
        var ram = get_filter('ram');
        var storage = get_filter('storage');
..... 

Thanks to @Bryn . $('.filter_data').html(window.scrollTo(1000, 0));感谢@Bryn . $('.filter_data').html(window.scrollTo(1000, 0)); . $('.filter_data').html(window.scrollTo(1000, 0)); worked for me.为我工作。

You can try the following code:您可以尝试以下代码:

$("a[href='#top']").on('click', function() {
  $("html, body").animate({ scrollTop: 0 }, "slow");
  return false;
});

Instead of代替

$('.filter_data').html('<div id="loading" style="" ></div>');

(or even $('.filter_data').html(window.scrollTo(1000, 0)); ) (甚至 $('.filter_data').html(window.scrollTo(1000, 0)); )

You should be able to just use你应该可以使用

window.scrollTo(1000, 0);

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

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