我試圖在每個具有.smoothScroll類的按鈕上添加一個smoothScroll動畫。點擊監聽器與jQuery和Javascript之間的區別
,所以我這樣做:
// SmoothSCroll
var smoothScroll = document.querySelectorAll('.smoothScroll');
[].forEach.call(smoothScroll, function (el) {
el.addEventListener('click', function() {
var offset = $(this.hash).offset().top;
$('html, body').stop().animate({
scrollTop: offset - (window.innerHeight/2) + ($(this.hash).innerHeight()/2)
}, 400);
return false;
});
});
https://jsfiddle.net/jt2jo3pt/2/
,如果你注意到發生了什麼,但有一點閃光燈時,點擊觸發我不知道(滾動條下降一點點前smoothscrolling)
但是當我試圖用jQuery的全是這樣的:
$('.smoothScroll').click(function(e){
var offset = $(this.hash).offset().top;
$('html, body').stop().animate({
scrollTop: offset - (window.innerHeight/2) + ($(this.hash).innerHeight()/2)
}, 400);
return false;
});
我沒有這個冒泡的效果:https://jsfiddle.net/34w72v1v/
你知道什麼可能會導致這個問題與querySelectorAll方法嗎?
我儘量不使用jQuery,所以我需要知道發生了什麼,瞭解你的時間:)
感謝。
'$( 'HTML,身體')停止()動畫({scrollTop的:。偏移 - (window.innerHeight/2)+($(this.hash).innerHeight( )/ 2)},400);'看起來不像_I儘量不使用jQuery_。 – Regent
爲什麼你會嘗試*不*使用jQuery?它可以讓事情變得更簡單和更短。如果它已經在網站上,那麼不使用它是一種浪費:) –
這只是爲了學習的目的。 –