[英]I am trying to understand why this JS snippet isn't working, could use some guidance adding to WP
I am trying to mess around with some JS and using snippets trying to get them to work on clean WP installs so that I understand how to add them Wordpress properly. 我试图弄乱一些JS,并使用片段试图使它们在干净的WP安装中正常工作,以便我了解如何正确添加Wordpress。
I am working with JSFiddle 我正在与JSFiddle合作
This is the link that im testing it on 这是即时通讯对其进行测试的链接
(function() {
var delay = false;
$(document).on('mousewheel DOMMouseScroll', function(event) {
event.preventDefault();
if(delay) return;
delay = true;
setTimeout(function(){delay = false},200)
var wd = event.originalEvent.wheelDelta || -event.originalEvent.detail;
var a= document.getElementsByTagName('a');
if(wd < 0) {
for(var i = 0 ; i < a.length ; i++) {
var t = a[i].getClientRects()[0].top;
if(t >= 40) break;
}
}
else {
for(var i = a.length-1 ; i >= 0 ; i--) {
var t = a[i].getClientRects()[0].top;
if(t < -20) break;
}
}
$('html,body').animate({
scrollTop: a[i].offsetTop
});
});
})();
Here is the fiddle I am trying to implement. 这是我正在尝试实现的小提琴。
It seems to be trying to do something on scroll and gets stuck. 它似乎试图在滚动上做某事并且被卡住。
Steps I have taken: 我已采取的步骤:
Added HTML to page Added CSS to style.css Added link to Jquery in header Added the JS snippet in a tag before 在页面中添加HTML在style.css中添加CSS在标头中添加到Jquery的链接在标签中添加JS片段之前
I'm thinking maybe it has something to do with the fact that its got more elements on the page than just a and tag maybe? 我在想这可能与它在页面上包含的元素不仅仅是a和tag有关吗? Seems to be getting caught on the header.
似乎被头文件捕获了。 This JS is just kind of hard for me to reverse engineer with the little that I know unfortunately.
不幸的是,对于我来说,使用此JS有点困难。
Thanks. 谢谢。
The javascript
listens to your mouse wheel event. javascript
侦听您的鼠标滚轮事件。 If you mouse wheel up or down, it will first check if you're currently "wheeling" too fast. 如果您将鼠标滚轮向上或向下滚动,它将首先检查您当前“滚轮”的速度是否太快。 If you are, it will delay you.
如果是,它将延迟您。 If you're not, then it will scroll (animate) to your next anchor tag and that anchor tag will be up top.
如果您不在,则它将滚动(动画)到您的下一个锚标记,并且该锚标记将位于最上方。
EDIT: I see you're getting a js error on your site. 编辑:我看到你在您的网站上收到一个js错误。 Remove the
$
from the 从中删除
$
$(function() {
var delay = false;
//blah blah
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.