简体   繁体   中英

What could be causing an issue with my scrollTo function?

I have a button that resides at the bottom of my page that is supposed to scroll to the top of the page when clicked.

I used identical code from another site that I've worked on to implement this feature, but for some reason nothing happens within the current site with the scrollTop feature.

I've tested the effect with another element on the page and it works if I set the 'button' as a different div, but when I revert it back to the div I want to be able to click from, again the feature no longer works.

Here is my code if anyone might be able to tell what is wrong.

Here is a fiddle of the actual page http://jsfiddle.net/6p6MZ/4/

What I have in my head tag:

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

<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">&nbsp;&nbsp;&nbsp;&copy;&nbsp;<script type="text/javascript">
    var dteNow = new Date();
    var intYear = dteNow.getFullYear();
    document.write(intYear);
</script> 3Elements Literary Review, Chicago, IL&nbsp;&nbsp;<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------------------------------->

Live demo example

Why not simply scrollTop to 0 ?

$("#nav-arrow").click(function() {
    $('html, body').animate({scrollTop: 0}, 750);
});

Any why in your code you have twice:

$(document).ready(function(){
    $(this).scrollTop(0);
});

?

Why you have 4 times document.ready at all?

Your example works, you just need to make sure jQuery is loaded. Here is your example with jquery loaded, done from the top left dropdown in jsfiddle.

http://bit.ly/1bJ8DXG

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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