Click here to Skip to main content
15,888,008 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
Hello, im starting study Jquery and im trying to do a basic example.
I have a html file index.html and other functions.js.
In the index.html i have some menus, content and text.
In the functions.js i have this function above.
But when i click in my link "footer-top" is not working.
What should be the problem?


    <section id="footer1">
           <a href="#" class="footer-top">Go To Top</a>
    </section>


    $(document).ready(function() {
       $('.top').click(function(){
       $('html,body').animate({ scrollTop: 0 }, 'slow');
        })
     });
Posted

should be:

$('.footer-top')


not

$('.top')
 
Share this answer
 
Comments
Mitchell J. 12-Dec-13 20:32pm    
+5, and I added to this.
Peter Leow 12-Dec-13 20:42pm    
Good advice, +5 too.
Karthik_Mahalingam 12-Dec-13 20:58pm    
right.
Adding to Peter Leow's solution, you should also use event.preventDefault()
JavaScript
$(document).ready(function() {
   $('.footer-top').click(function(event){
      event.preventDefault();
      $('html,body').animate({ scrollTop: 0 }, 'slow');
   });
});


You should use this because the link has a href of #, which will make the browser leap to the top of the page without animation. event.preventDefault() cancelles the default behaviour.
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 12-Dec-13 22:05pm    
Good info craig, but anyhow we should use $('.footer-top') to select the anchor element.
Mitchell J. 12-Dec-13 22:07pm    
Oops! Forgot to change it when I copy/pasted. I'd already noticed Peter's solution :-)
Karthik_Mahalingam 12-Dec-13 22:20pm    
cool :)
mibetty 13-Dec-13 10:21am    
thanks for the answers but still dont work! and i have everythin like you said :/
Mitchell J. 13-Dec-13 16:32pm    
Take a look at this article on codeproject :-)
http://www.codeproject.com/Articles/540858/Smooth-Scroll-to-the-Top-of-the-Page-jQuery

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900