简体   繁体   English

jQuery在我的WordPress网站上不起作用

[英]jQuery is not Working on my WordPress Site

I have tried everything to get my jQuery codes to work and none of it seems to work. 我已经尽一切努力使我的jQuery代码正常工作,但似乎都没有工作。 I have checked to make sure the JavaScript script is enqueuing and loading in the browser source code and it's. 我已经检查以确保JavaScript脚本正在排队并且正在浏览器源代码中加载。 I also looked at the JavaScript console for errors and no errors were found. 我还查看了JavaScript控制台中的错误,没有发现错误。 Site is located at " http://twps.psadeaf.org/v3/ ". 该站点位于“ http://twps.psadeaf.org/v3/ ”。 The script I am loading is "init.js". 我正在加载的脚本是“ init.js”。

I am enqueuing the script with code bellow: 我使用下面的代码使脚本入队:

// Adding init file in the footer
wp_enqueue_script( 'init-js', get_template_directory_uri() . '/assets/js/init.js', array( 'jquery' ), '', true );

Then the jQuery code that does not work is: 然后无效的jQuery代码是:

jQuery(window).scroll(function() {
   if (scroll >= 50) {
     jQuery('.psad-nav-button').show()
     jQuery(".psad-nav").hide()
   }
   if (scroll < 50) {
     jQuery(".psad-nav").show()
     jQuery('.psad-nav-button').hide()
   } 
});

Bellow is the code that should be effected by it: 贝娄是应受其影响的代码:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div data-sticky-container> <div data-sticky data-options="marginTop:0;" data-sticky-on="small" style="width:100%"> <div class="title-bar psad-nav" data-responsive-toggle="top-bar-menu" data-hide-for="<?php echo $breakpoint ?>"> <button class="menu-icon" type="button" data-toggle></button> <div class="title-bar-title"> <?php _e( 'Menu', 'jointswp' ); ?> </div> </div> <div class="top-bar psad-nav" id="top-bar-menu"> <div class="top-bar-left show-for-<?php echo $breakpoint ?>"> <?php do_action( 'psad_site_logo', '200px' ); ?> </div> <div class="top-bar-right"> <?php joints_top_nav(); ?> </div> </div> <button class="psad-nav-button expanded button" type="button"> ^ Navigation ^ </button> </div> </div> 

I am using foundation 6 by Zurb for Sites in my theme. 我在主题中使用Zurb的Foundation 6 for Sites。 The original theme is JOINTSWP. 原始主题是JOINTSWP。 I am using the the sticky plugin to make the navigation/header to stick to the top of the page as the user scrolls down the page. 当用户向下滚动页面时,我正在使用粘性插件来使导航/标题停留在页面顶部。 I am using the ".scroll() api" to modify it with the "psad-nav" and "psad-nav-button" class. 我正在使用“ .scroll()api”通过“ psad-nav”和“ psad-nav-button”类对其进行修改。 Using the ".show()" and ".hide()" api to show and hide the classes after scrolling down the page. 在向下滚动页面后,使用“ .show()”和“ .hide()” API来显示和隐藏类。

I have tried using other api's and nothing works on my site. 我尝试使用其他API,但在我的网站上没有任何效果。

Your conditions are comparing the value of scroll but scroll is a function in the global namespace. 您的条件正在比较scroll的值,但是scroll是全局命名空间中的一个函数。 Checking if a function is more than or less than an integer makes no sense and returns false. 检查函数是否大于或小于整数是没有意义的,并返回false。 Your if statements are always false and never happen. 您的if语句始终为假,永远不会发生。

You haven't defined the variable scroll used in the if conditions. 您尚未定义if条件中使用的变量scroll The value of scroll should be document.body.scrollTop to get the current scroll position in the document. scroll的值应为document.body.scrollTop以获取document.body.scrollTop中当前的滚动位置。

jQuery(window).scroll(function() {
   var scroll = document.body.scrollTop;
   if (scroll >= 50) {
     jQuery('.psad-nav-button').show()
     jQuery(".psad-nav").hide()
   }
   if (scroll < 50) {
     jQuery(".psad-nav").show()
     jQuery('.psad-nav-button').hide()
   } 
});

Thank you everyone for their responses, I feel real smart! 谢谢大家的回应,我真的很聪明! ;) haha You have to forgive me it's been awhile since I have been working with JavaScript. ;)哈哈您必须原谅我,因为我一直在使用JavaScript。 I am also surprised it didn't throw any errors in the JavaScript console. 我也很惊讶它没有在JavaScript控制台中引发任何错误。 I was taking the code from a tutorial on tuts+. 我是从tuts +教程中获取代码的。 I didn't catch that they left out the declaration of the variable "scroll". 我没有发现他们忽略了变量“ scroll”的声明。 I fixed that and it works great but then also realized I am naming a variable the same name as a global function in jQuery. 我修复了该问题,效果很好,但后来又意识到我要为变量命名与jQuery中的全局函数同名。 I wounder if that is why it didn't throw an error for an undefined variable in the JavaScript console. 我很伤心,如果这就是为什么它没有在JavaScript控制台中为未定义的变量引发错误。 I ended up changing the variable name also to avoid conflict. 我最终也更改了变量名,以避免冲突。

Bellow is the code I ended up going with: 贝娄是我最终使用的代码:

jQuery(window).scroll(function() {
    var psad_scroll = jQuery(this).scrollTop();
    var psad_nav = jQuery( ".psad-nav" );
    var psad_nav_button = jQuery( ".psad-nav-button" );

    function psad_show_nav() {
        if (Foundation.MediaQuery.atLeast( "medium" )) {
            psad_nav.show();
            psad_nav_button.addClass( "hide" ) ;
        }
    }

    function psad_hide_nav() {
        if (Foundation.MediaQuery.atLeast( "medium" )) {
            psad_nav_button.removeClass( "hide" );
            psad_nav.hide();
        }
    }

    if (psad_scroll >= 30) {
        psad_hide_nav();
        psad_nav_button.on({
            click: psad_show_nav,
            mouseenter: psad_show_nav
        });

    }
    if (psad_scroll < 30) {
        psad_show_nav();
    }
});

This now works flawlessly for me, thank you all that helped me out. 现在这对我来说是完美无缺的,谢谢所有帮助我的人。 Would this be the best way to achieve what I was looking for? 这是实现我所寻找的最佳方法吗? If not, how would you accomplish this? 如果没有,您将如何实现?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM