我已經按照本教程介紹瞭如何編寫一點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嗎? –