简体   繁体   English

jQuery无法在WordPress中工作?

[英]Jquery not working in wordpress?

I have created a parallex scrolling effect in jquery on a static html/ css website, im now trying to copy this over to a wordpress theme. 我在静态html / css网站上的jquery中创建了parallex滚动效果,即时通讯现在试图将其复制到wordpress主题。

This is how i imported the 2 javascript files (jquery-11.1.3.min.js, parallex.js) 这就是我导入2个JavaScript文件(jquery-11.1.3.min.js,parallex.js)的方式

add_action( 'wp_enqueue_scripts', 'praid_load_js_files' );
function praid_load_js_files(){
     wp_register_script( 'jquery', get_template_directory_uri() . '/js/jquery-1.11.3.min.js', array('jquery'), '1.11.3', true );
     wp_register_script( 'parallex', get_template_directory_uri() . '/js/parallex.js', array('jquery'), '1.0', true );

     //Adds to every page
     wp_enqueue_script('jquery');
     wp_enqueue_script( 'parallex' );
}

This is what is in my parllex file 这就是我的parllex文件中的内容

  var pContainerHeight = $('.welcoming').height();

$(window).scroll(function(){

  var wScroll = $(this).scrollTop();

  if (wScroll <= pContainerHeight) {

    $('.welcomeText').css({
      'transform' : 'translate(0px, '+ wScroll /2 +'%)'
    });

    $('.welcoming').css({
      'transform' : 'translate(0px, -'+ wScroll /25 +'%)'
    });

  }
});

For some reason this works perfectly fine on a static html/ css page, but when its on word press it doesn't work. 由于某种原因,这在静态html / css页面上可以很好地工作,但是在按单词的情况下却不起作用。

Any help would be appreciated. 任何帮助,将不胜感激。 :) :)

As @vinayakj suggested in the comments, I wrapped the code in the parralex.js folder with 正如@vinayakj在评论中建议的那样,我将代码包装在parralex.js文件夹中,

$(document).ready(function(){ //parralax code here });

And it worked. 而且有效。

PLUS: @HareeshSivasubramanian pointed out that I don't need to add the jquery myself as WordPress bundles it automatically. 另外:@HareeshSivasubramanian指出,我不需要自己添加jquery,因为WordPress自动将其捆绑了。

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

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