繁体   English   中英

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

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

我正在使用对象标记代码创建产品页面,但是每次单击“查看”按钮时,下一页将停留在与上一页相同的位置。 如何添加每次单击“查看”按钮时都可以从页面顶部查看的功能?

<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 -->

当我在此处的特定位置“ x”上单击“查看详细信息”按钮时: http : //postimg.org/image/vgs0lwhtr/

下一页显示相同的位置“ x”,但我希望它跳至页面顶部: http : //postimg.org/image/vn80e2lph/

使用Javascript:

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

使用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;
    });
});

请参考这个 ,对您有帮助

有时,如果您当前的内容是通过jQuery生成的,则将滚动到正文不起作用(在我的情况下)。 在这种情况下,您可以执行以下操作。

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

Subhash的jQuery解决方案的一个小问题是,您必须在$(document).ready()中调用此代码,才能使$('body')选择器正常工作。 在将部分页面呈现到屏幕之前, ready事件可能不会触发。

更好的方法是简单地修改用户的位置,以解决此浏览器“功能”:

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

用于页面部分之间跳转的简单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> 

返回页首按钮,在所有浏览器中都有效。要更改滚动速度,只需在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> 

<body>标记中分配一个id =“#body”和tabindex

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

并使用jquery focus()

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

您可以使用以下方法:

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