我有一个位于页面底部的按钮,该按钮应该在单击时滚动到页面顶部。
我使用了来自其他网站的相同代码来实现此功能,但由于某些原因,当前网站中没有任何使用scrollTop功能的情况。
我已经在页面上测试了另一个元素的效果,如果我将“按钮”设置为不同的div,它会起作用,但当我将其恢复为div时,我希望能够点击它,再次功能不再有效。
如果有人能说出错误的话,这是我的代码。
这是实际页面http://jsfiddle.net/6p6MZ/4/
的小提琴我头上的标签:
<link rel="shortcut icon" href="images/favi.ico" />
<link href="css/about-3elements.css" rel="stylesheet">
<link href="css/pages.css" rel="stylesheet">
<link href="css/page-transitions.css" rel="stylesheet">
<link href="css/footer.css" rel="stylesheet">
<link href="css/responsive-mobile.css" rel="stylesheet">
<link href="css/responsive-tablet.css" rel="stylesheet">
<link href="css/responsive-1025-1500px.css" rel="stylesheet">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script type="text/javascript" src="js/modernizr.custom.28468.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
脚本:
<script><!-----------------------------CONTROLS FOOTER BUTTON THAT GOES TO THE TOP OF THE PAGE----------------------------------->
$(document).ready(function(){
$(this).scrollTop(0);
});
$("#nav-arrow").click(function() {
$('html, body').animate({
scrollTop: $("html").offset().top
}, 750);
});
</script>
HTML:
<footer class="bottom-footer">
<div id="nav-arrow"><img src="images/nav-arrow.svg" width="35%"><br><span class="arrow-text" style="width:100px; color:#f8f4ec; font-family:myriad pro, arial, sans-serif; font-size:1.25em;">Back to top</span></div>
<section class="footer-section">
<ul>
<li><a href="faq.html">FAQ</a></li>
<li><a href="terms-of-use.html">TERMS</a></li>
<li><a href="usage-rights.html">USAGE RIGHTS</a></li>
<li><a href="privacy-policy.html">PRIVACY POLICY</a></li>
<li><a href="contact.html">CONTACT</a></li>
<li><a href="the-editors.html">MEET THE STAFF</a></li>
</ul>
<div id="social-links-container">
<ul class="social" style="height:40px;">
<li><a href="https://twitter.com/3ElementsReview" title="Follow @3ElementsReview" target="_blank"><img src="images/twitter.svg" width="25%" alt="If you like our literary journal, follow 3Elements Review on Twitter" border="none"></a></li>
<li><a href="https://www.facebook.com/pages/3Elements-Review/165239120323115" title="Like us on Facebook" target="_blank"><img src="images/facebook.svg" width="25%" alt="If you find our literary journal interesting, like 3Elements Review on Facebook" border="none"></a></li>
<li><a href="https://plus.google.com/102175306984563852786" rel="publisher" title="+1 our page on Google+" target="_blank"><img src="images/google-plus.svg" width="25%" alt="Recommend our literary journal 3Elements Review on Google+" border="none"></a></li>
<li><a href="http://www.stumbleupon.com/stumbler/3elementsreview" title="Come say hello!" target="_blank"><img src="images/stumble.svg" width="25%" alt="If you like our literary journal, you can find our 3Elements Review page on StumbleUpon" border="none"></a></li>
</ul>
</div><!---------------SOCIAL LINKS CONTAINER END-------------------->
<h1 class="site-design"> © <script type="text/javascript">
var dteNow = new Date();
var intYear = dteNow.getFullYear();
document.write(intYear);
</script> 3Elements Literary Review, Chicago, IL <a href="http://www.marlonfowler.com" target="_blank">Site design by Marlon Fowler</a></h1>
</section>
</footer>
</div><!------------------------CONTAINER THAT SURROUNDS ALL CONTENT BELOW THE NAVIGATION BAR END------------------------------->
答案 0 :(得分:1)
为什么不简单scrollTop
到0
?
$("#nav-arrow").click(function() {
$('html, body').animate({scrollTop: 0}, 750);
});
为什么你的代码中有两次:
$(document).ready(function(){
$(this).scrollTop(0);
});
为什么你有4次document.ready
?
答案 1 :(得分:1)
您的示例有效,您只需确保加载jQuery即可。这是你加载jquery的例子,从jsfiddle的左上角下拉列表完成。