我已经按照本教程介绍了如何编写一点jQuery以在我的网站上获得很好的平滑滚动。这里是你参考的啧啧https://www.youtube.com/watch?v=S6pzabpUmocjQuery平滑滚动问题
但是在我的jQ代码的某个地方似乎存在问题,并且动画无法正常工作......如果有一组新的目光观看它会很好我错了,如何解决它。
随着这一刻的控制台告诉我,我有这个问题TypeError: Cannot read property 'top' of undefined
这是我当前构建 http://kapena.github.io/pp_web/#services-pp
这里是我的JQ代码
$(function() {
// catch all clicks on page
$('a').click(function() {
// check if has hash
if(this.hash) {
// get rid of the # sign
var hash = this.hash.substr(1);
//get the position of the <a name>
var $toElement = $("a[name="+hash+"]");
var toPosition = $toElement.position().top;
// scroll/animate that element
$ ('body,html').animate({
scrollTop : toPosition
},2000,"easeOutExpo");
// don't do the jump
return false;
}
});
if(location.hash) {
var hash = location.hash
window.scroll(0,0);
$('a[href='+hash+"]").click();
}
});
你可以控制台登录var hash,并在这里添加 –
你可以添加HTML吗? –