javascript - how to solve this Jquery error? -
i have made jquery can see fiddle below: http://madaxedesign.co.uk/dev/test/ http://jsfiddle.net/x82mu/1/
code:
$(document).ready(function() { var $root = $('html, body '); $('.scroll a').click(function(e) { var href = $.attr(this, 'href'); $root.animate({ scrolltop: $(href).offset().top }, 500, function () { window.location.hash = href; }); return false; }); // responsive menu $(function() { var pull = $('#pull'), menu = $('nav ul'), menuheight = menu.height() $(pull).on('click', function(e) { e.preventdefault(); menu.slidetoggle(); }); $(window).resize(function(){ var w = $(window).width(); if(w > 320 && menu.is(':hidden')) { menu.removeattr('style'); } }); }); });
but pulls through error: uncaught typeerror: cannot read property 'top' of undefined
this preventing next piece of jquery work.
i wondering if let me know why, or give me solution?
many thanks
you trying selector href
isn't there in many of menu items.
i.e:
<li><a href="#">home</a></li> <li><a href="#aboutus">about us</a></li> <li><a href="#">portfolio</a></li> <li><a href="#">contact us</a></li>
and
$(href).offset().top //here offset() of empty jquery object undefined.
not issue can this.href
instead $.attr(this, 'href')
try this:
$('.scroll a').click(function(e) { var href = $(this).attr("href"), $el = $(href), top=0; //default top 0 if($el.length) //if there element matching href top = $el.offset().top; //set top $root.animate({ scrolltop: top //now scroll }, 500, function () { window.location.hash = href; }); return false; });
Comments
Post a Comment