我试图在每个具有.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?它可以让事情变得更简单和更短。如果它已经在网站上,那么不使用它是一种浪费:) –
这只是为了学习的目的。 –