如何使用jQuery滚动到页面顶部

How to scroll to the top of a page using jQuery?

本文关键字:顶部 滚动 何使用 jQuery      更新时间:2024-05-06

我找到了一个代码,我正试图在ASP.NET Web窗体页面中做同样的事情。这是代码:JSFiddle中的JSFiddl,它工作得很好。

在HTML中,我尝试了同样的方法:

    <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <link href="cssfile.css" rel="stylesheet" type="text/css" />        
    <script type="text/javascript">
        $(document).ready(function () {
            $(window).scroll(function () {
                if
    ($(this).scrollTop() > 50) { $('.scrollup').fadeIn(); } else {
                    $('.scrollup').fadeOut();
                }
            }); $('.scrollup').click(function () {
                $("html, body").animate({ scrollTop: 0
                }, 600); return false;
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <h1>
        Top of the page</h1>
    <textarea id="TextArea1" cols="100" rows="50"></textarea>
    <a href="#" class="scrollup">Scroll</a>
    </form>
</body>
</html>

当我执行此操作时,编译器不会给我任何错误,但不会出现指向顶部的"框"。

您需要将jquery文件添加到页面的标题部分。JSFIddle的头部分中已经有jquery文件。

 <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js" type="text/javascript">