简体   繁体   English

我试图了解为什么此JS代码段无法正常工作,可以使用一些可添加到WP中的指南

[英]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.

相关问题 为什么此代码段无效? - Why this snippet isn't working? 为什么在尝试对列表进行排序时单击按钮时list.js不能正常工作 - Why isn't list.js working when I click the button when I am trying to sort my list 我正在尝试检查用户是否使用 Next.js 和 Supabase 登录,但它不起作用 - I am trying to check if a user is logged in using Next.js and Supabase but it isn't working 我正在尝试从 mongodb 连接我的 server.js express api 但它显示一些错误我不明白 - I am trying to connect my server.js express api from mongodb but it showing some error i don't understand it 为什么递归不起作用? 我正在尝试显示 n 到 1 个系列 - why isn't recursion working? i am trying to display n to 1 series 为什么我的 JS 代码不工作? 我正在尝试获取此代码以生成随机密码 - Why isn't my JS code working? I'm trying to get this code to generate a random password 试图了解CGI和AJAX的工作原理-为什么此演示无效? - Trying to understand how CGI and AJAX work — why isn't this demo working? 我正在尝试制作一个程序来让我的课程剩余时间,但更改后的时间表不起作用,你能告诉我为什么吗? - I am trying to make a program to get the time left in my classes but the altered schedule isn't working can you tell me why? 我不明白为什么我的nodejs / javascript正则表达式无法正常工作 - I don't understand why my nodejs/javascript regex isn't working 不明白为什么我的 for 循环代码不起作用 - Don't understand why my for loop code isn't working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM