-5

i would like to have smooth scroll effect on my page. And iv found this code

jQuery(function($) {
  $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});

This is ok, but is not working with my "go top" link. So i check other pages with tutorial and do this:

jQuery(function($) {
  $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
  $('a.go-top').click(function() {
    $('html, body').animate({scrollTop:0}, 'slow');
    return false;
  });
});

And all is working fine now. but I have no idea about jQuery, could You tell me is this correct code, or if You can, what should i change here? Thanks!

IamNaN
  • 6,654
  • 5
  • 31
  • 47
KamilT
  • 11
  • 2
  • I think that this question should have been posted on [**Code Review**](http://codereview.stackexchange.com/) – Artur Filipiak Apr 19 '15 at 11:31
  • @ArturFilipiak To be on-topic for Code Review, the question must be posed by the author or maintainer of the code. You can't just ask for code reviews of random code that you found. – 200_success Apr 19 '15 at 11:34
  • @ArturFilipiak It must be by the author. Artur, have a look at [this](http://codereview.stackexchange.com/help/on-topic). Near the bottom of the page is a little checklist - if you answer all the questions with a yes, it should be on CodeReview. One of the questions asks for it to be by the author – blaizor Apr 19 '15 at 11:35
  • Or maintainer. whoops. – blaizor Apr 19 '15 at 11:36

1 Answers1

1

It did not work with your go top link because you are looking only for a link with a id attributes to be clicked: $('a[href*=#]:not([href=#])'). Your go-top is a class not an id, if you were to change it to an id you would not need the code you added.

Tucker
  • 659
  • 4
  • 16