Wordpress插件中的jQuery

时间:2014-04-30 10:18:21

标签: javascript jquery wordpress

我正在制作一个wordpress插件,在其中我使用jQuery,但它不起作用。
我的代码如下:
script.js:

$(document).ready(function(){

    $(window).scroll(function() {

    if($(this).scrollTop() != 0) {
        $("#toTop").fadeIn("slow");    
    }

    else {
        $("#toTop").fadeOut("slow");
    }

  });

  $("#toTop").click(function() {
    $("body,html").animate({scrollTop:0},1000);

  });

});

3 个答案:

答案 0 :(得分:0)

美元符号在WordPress中为原型库保留,因此请使用jQuery代替$

jQuery(document).ready(function(){

    jQuery(window).scroll(function() {

    if(jQuery(this).scrollTop() != 0) {
        jQuery("#toTop").fadeIn("slow");    
    }

    else {
        jQuery("#toTop").fadeOut("slow");
    }

  });

  jQuery("#toTop").click(function() {
    jQuery("body,html").animate({scrollTop:0},1000);

  });

});

答案 1 :(得分:0)

当你一起使用jQuery和php时,你应该使用jQuery $ no.conflict()。 你的代码看起来像这样:

jQuery(document).ready(function($){

    jQuery(window).scroll(function() {

    if(jQuery(this).scrollTop() != 0) {
        jQuery("#toTop").fadeIn("slow");    
    }

    else {
        jQuery("#toTop").fadeOut("slow");
    }

  });

  jQuery("#toTop").click(function() {
    jQuery("body,html").animate({scrollTop:0},1000);

  });

});

答案 2 :(得分:0)

像这样使用jQuery: -

jQuery(document).ready(function($){

    $(window).scroll(function() {

        if($(this).scrollTop() != 0) {
            $("#toTop").fadeIn("slow");
        } else {
            $("#toTop").fadeOut("slow");
        }
    });

    $("#toTop").click(function() {

        $("body,html").animate({scrollTop:0},1000);

    });
});

希望它可以奏效。