在WordPress中不起作用的jQuery代码的一部分

part of jquery code that is not working in wordpress

本文关键字:jQuery 一部分 代码 不起作用 WordPress      更新时间:2023-09-26

我开发了一个jquery代码,当我向下滚动时,它应该让菜单隐藏一点,并在我开始向上滚动时立即重新出现。

我在我的静态html网站上完美地工作,但是当我将其迁移到wordpress时,它就停止了工作。我所有其他的js代码都可以完美运行。这是代码的一部分:

$(document).ready(function(){
$(window).scroll(function () {
var prevScroll;
var hidden = false;
    var currentScroll = $(this).scrollTop();
    if($("body").scrollTop() > 492){
    if (prevScroll) {
        console.log(currentScroll + "  " + prevScroll);
        console.log(hidden);
        if (currentScroll < prevScroll && hidden) {
            console.log('show');
            $("#header-wrap").animate({marginTop: '0px'}, 200);
            $("#menu").fadeIn("fast");
            hidden=false;
        } else if (currentScroll > prevScroll && !hidden) {
            console.log(hidden);
            console.log('hiding');
            $("#header-wrap").animate({marginTop: '-60px'}, 200);
             $("#menu").fadeOut("fast");
            hidden=true;
        }
    } else if(!hidden){
        console.log('first time');
        $("#header-wrap").animate({marginTop: '-60px'}, 200);
        $("#menu").fadeOut("fast");
        hidden= true;
    }
    prevScroll = currentScroll;
  }
  else{
    if(hidden){
      console.log('show');
      $("#header-wrap").animate({marginTop: '0px'}, 200);
      $("#menu").fadeIn("fast");
      hidden=false;
    }
  }
});
});

我的代码有什么问题?我将它与脚本.js页面中的所有js代码一起放在一起。

谢谢

编辑:我忘了说菜单是隐藏的,这很好,但是一旦我向上滚动,它就不会重新出现。所以一部分代码有效,另一部分无效!

jQuery和Wordpress之间可能会发生冲突,因为它们都使用符号$请尝试使用jQuery而不是$或将jQuery代码包装在里面:

jQuery(document).ready(function($){
    $(window).scroll(function () {
        // Your code here
    });
});

我做到了,问题是我在函数$(window).scroll(function () {开始之后声明var prevScroll;var hidden = false;,而不是在它之前。无论如何,感谢您的帮助..