简体   繁体   English

如何在单击按钮时滚动回到页面顶部?

[英]How to scroll back to the top of page on button click?

I am creating my product pages by using the object tag code, but every time I click the "view" button, the next page is staying at the same position of previous page which I just viewed. 我正在使用对象标记代码创建产品页面,但是每次单击“查看”按钮时,下一页将停留在与上一页相同的位置。 How can I add functionality that will let me view from the top of page every time I click the "view" button? 如何添加每次单击“查看”按钮时都可以从页面顶部查看的功能?

<div id="container" class="clearfix"><!--! end of #sidebar -->
    <div class="holder"></div>
    <div id="content" class="defaults"><!-- navigation holder -->
        <div id="title2">Office Section</div>

        <!-- item container -->
        <ul id="itemContainer">
            <li>
                <div id="product">
                    <div id="title">FuBang®</div>
                    <div class="imageOuter" style="margin:0">
                        <a class="image" href="officesection010.html">
                            <span class="rollover" ></span>
                            <img class="imgborder" src="product/officesection/010.jpg" width="165" height="165" />
                        </a>
                    </div><br />
                    <div id="text">Sofa </div><br />
                    <div id="button">
                        <a href="officesection010.html">View Details</a>
                    </div>
                </div>
            </li>
        </ul>
        <br />
        <div id="title2"></div>
        <div class="holder"></div>
    </div>
    </div> <!--! end of #content -->
</div> <!--! end of #container -->

When I click the "View Details" button at a specific position "x" here: http://postimg.org/image/vgs0lwhtr/ 当我在此处的特定位置“ x”上单击“查看详细信息”按钮时: http : //postimg.org/image/vgs0lwhtr/

The next page shows the same position "x", but I want it to jump to the top of page: http://postimg.org/image/vn80e2lph/ 下一页显示相同的位置“ x”,但我希望它跳至页面顶部: http : //postimg.org/image/vn80e2lph/

Using Javascript: 使用Javascript:

document.body.scrollTop = document.documentElement.scrollTop = 0;

Using jQuery: 使用jQuery:

$(function() {
   $('body').scrollTop(0);
});
<a href="#" ID="backToTop"></a>

jQuery(document).ready(function($){
    $(window).scroll(function(){
        if ($(this).scrollTop() > 50) {
            $('#backToTop').fadeIn('slow');
        } else {
            $('#backToTop').fadeOut('slow');
        }
    });
    $('#backToTop').click(function(){
        $("html, body").animate({ scrollTop: 0 }, 500);
        //$("html, body").scrollTop(0); //For without animation
        return false;
    });
});

please refere this , may this help 请参考这个 ,对您有帮助

Sometimes placing scroll to body doesn't work if your current content is generated through jQuery (as it was in my case). 有时,如果您当前的内容是通过jQuery生成的,则将滚动到正文不起作用(在我的情况下)。 In such situation you can just do following. 在这种情况下,您可以执行以下操作。

$(function() {
   $('html').scrollTop(0);
});

A small issue with Subhash's jQuery solution is that you must call this code within $(document).ready() in order for your $('body') selector to work. Subhash的jQuery解决方案的一个小问题是,您必须在$(document).ready()中调用此代码,才能使$('body')选择器正常工作。 The ready event may not fire before parts of your page have been rendered to the screen. 在将部分页面呈现到屏幕之前, ready事件可能不会触发。

An better approach is to simply modify the user's location as a work-around to this browser 'feature': 更好的方法是简单地修改用户的位置,以解决此浏览器“功能”:

//Above all of your $(document).ready(...) code
document.location = "#";

Simple HTML solution for jumping between page parts 用于页面部分之间跳转的简单HTML解决方案

// Place a tag like this where you would like to go
// in your case at the top
<a name="top"></a>

// you will reach there by click of this link  better use an image reach their by clicking on this we can also use an image, align in right 

<a href="#top">last</a> 

Back to top button, works in all browsers.To change the scroll speed simply change the x in counter -= x here x = 10 返回页首按钮,在所有浏览器中都有效。要更改滚动速度,只需在counter -= x更改x counter -= x x = 10

 function scrollToTop(){ var myBody = document.body; var id = setInterval(secondFunction,1); var height = window.pageYOffset; var counter = height; function secondFunction(){ if(window.pageYOffset == 0){ clearInterval(id); } else { if(!!navigator.userAgent.match(/Trident/g) || !!navigator.userAgent.match(/MSIE/g)){ counter -= 10; counter--; document.documentElement.scrollTop = counter; } else { counter -= 10; counter--; myBody.scrollTop = counter; } } } } 
 body { height: 5000px; margin: 0; padding: 0; } .backToTop { position: fixed; /* Fixed at page */ top: auto; bottom: 20px; left: auto; right: 20px; background-color: crimson; color: white; padding: 5px; cursor: pointer; } header { position: relative; top: 0; left: 0; width: 100%; height: 100px; background-color: black; } 
 <!-- back to top button --> <span class="backToTop" onclick="scrollToTop()">TOP</span> <!-- Header --> <header> </header> 

Assign an id="#body" and tabindex in the <body> tag <body>标记中分配一个id =“#body”和tabindex

<body id="#body" tabindex="1">

and use jquery focus() 并使用jquery focus()

$(function() {
    $("#body").attr("tabindex",-1).focus();
}

You can use this method: 您可以使用以下方法:

function gototop() {
    if (window.scrollY>0) {
        window.scrollTo(0,window.scrollY-20)
        setTimeout("gototop()",10)
    }
}

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

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