0

Afternoon

Problem: Instead of scrolling to the top with animation my "back to the top" button (down-right corner after scrolling down) jumps right to the top.

Example can be found on http://www.pixsters.be

My html:

<a href="#top" id="homebacktothetop"><span>backtothetop</span></a>

My js (jquery):

            // scroll to 0 when clicked
            $('#homebacktothetop').click(function () {
                $('body,html').animate({
                    scrollTop: 0
                }, 800);
                return false;
            });
        });
Warre Buysse
  • 1,335
  • 4
  • 21
  • 39
  • 1
    dont know. But the click event handler has a `preventDefault` method you can use instead of `return false;` if you want to prevent the link doing it's normal job. `.click(function(e){ e.preventDefault(); });` – musefan Aug 02 '12 at 16:02

5 Answers5

2

Change 'body,html' to window

$('#homebacktothetop').click(function (e) {
    $(window).animate({
         scrollTop: 0
     }, 800);
     e.preventDefault();
});
kei
  • 20,157
  • 2
  • 35
  • 62
0

typo: you try to call

function gotoByScroll(id)
{
     $('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}

with

$("#homebacktothetop").click(function(){goToByScroll("#container");});

Which gives you a Uncaught ReferenceError: goToByScroll is not defined in the console, javascript is case sensitive

Roest
  • 826
  • 6
  • 16
0

This works for me:

         $(document).ready(function(){
              $("#GoToTop").click(function()
              {
                 $("html, body").animate({ scrollTop: 0 }, 500);
                return false;
              });
            });
Miljan Puzović
  • 5,840
  • 1
  • 24
  • 30
0

This is a complete shot in the dark, but have you tried removing the return statement? I was looking at a similar post, jquery animate, scroll top top slow, and they got the same script to work, but they have no return statement.

On a completely different topic, you're getting these Javascript errors in your page: enter image description here enter image description here

Might wanna look into that, it seems like therein lies your problem. Good luck!

Community
  • 1
  • 1
Eddie Rivas
  • 335
  • 1
  • 3
  • 11
0

Looks to me like you got your code from Web Designer Wall. I noticed in your code that on line 32 you overwrite the click event for #homebacktothetop with a new function.

Micah Henning
  • 2,125
  • 1
  • 18
  • 26