繁体   English   中英

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

[英]How to scroll to the top of a page using jQuery?

我找到了一个代码,我试图在ASP.NET Web窗体页面中执行相同的操作。 这是代码: JSFiddle中的JSFiddle可以正常工作。

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

暂无
暂无

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

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