Can someone clarify the following lines of code?

Discussion in 'JavaScript' started by gandalf117, Nov 14, 2011.

  1. #1
    I believe that this is a code that is used for dynamic scrolling of internal links on a page. When clicking on a link the page should slide down or go up to the appropriate position on the page. I have some experience in Jquery and Javascript but I have never seen something like 'a[href*=#]'.

    Most of all I would like to know what should the HTML for this code looks like so that it works. I haven't been able to run it so far.

    $('a[href*=#]').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
    var $target = $(this.hash);
    $target = $target.length && $target || $('[id=' + this.hash.slice(1) + ']');
    if ($target.length) {
    var targetOffset = $target.offset().top;
    $('html,body').animate({scrollTop: targetOffset}, 1000);
    return false;
    }
    }
    });
     
    gandalf117, Nov 14, 2011 IP
  2. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #2
    what it does is it scrolls-up the page slowly, not like when you click on an anchor that jumps top the top..

    a[href*=#] means it will look for anchor tags with href value of #

    just like this..
    
    <a href="#" title="Go to Top of the Page">Go to Top of the Page</a>
    
    HTML:
     
    JohnnySchultz, Nov 18, 2011 IP
  3. gandalf117

    gandalf117 Active Member

    Messages:
    111
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    thanks for replaying even though I have figured it out by now

    it would have been nice if there was a way of marking a thread as solved once you've got the answer
     
    gandalf117, Nov 18, 2011 IP