SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: how to fix uncaught typeError: cannot read property 'top' of null ?

  1. #1
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to fix uncaught typeError: cannot read property 'top' of null ?

    Hello forum members i have a jquery problem. whenever i tried to apply on click scroll script i got this error "uncaught typeError: cannot read property 'top' of null". Here following is code of Jquery.How can this will be fixed.

    $(document).ready(function($){
    $('a[href^="#"]').bind('click.smoothscroll',function (e) {
    e.preventDefault();

    var target = this.hash,
    $target = $(target);

    $('html, body').stop().animate({
    'scrollTop': $target.offset().top
    }, 900, 'swing', function () {
    window.location.hash = target;
    });
    });

    });

  2. #2
    SitePoint Mentor bronze trophy
    chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,041
    Mentioned
    9 Post(s)
    Tagged
    1 Thread(s)
    I created a demo using your above code and it worked fine so I would suggest you ensure all your anchor hash tags match up to real element id's in your markup as that it what will be causing the error, additionally you should always have some kind of error checking to ensure the hash matches up to something.

    http://jsfiddle.net/2LuNP/
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •