主页js与其他页面插件的js冲突

时间:2018-05-10 作者:Khushbu Vaghela

我已经下载了Awesome Filterable Portfolio plugin , 它工作得很好。。但当我在主页上添加了一个用于滚动旋转木马的js文件时,该插件的js停止了工作。。

我已经添加了cr-scroll.js 文件位于functions.php 如下所示:

wp_register_script(\'java1\', get_template_directory_uri() . \'/js/cr-scroll.js\', array( \'jquery\' ));
wp_enqueue_script( \'java1\' );

What I\'ve Tried:

  • $.noConflict();

    $ 具有jquery

    将代码包装在

     (function($){
    
        $(document).ready(function(){
    
        });
    
     })(jQuery);
    
    但什么都没用。

    我的js文件是cr-scroll.js

      (function($){
            $(document).ready(function($) {
             $.fn.make_carousel = function() {
                var speed = 0;
                var scroll = 0;
                var con = $(this);
                var con_w = con.width();
                var max_scroll = con[0].scrollWidth - con.outerWidth();
                var prev_frame = new Date().getTime();
                con.on(\'mousemove\', function(e) {
                    var mouse_x = e.pageX - con.offset().left;
                    var mouseperc = 100 * mouse_x / con_w;
                    speed = mouseperc - 50;
                }).on ( \'mouseleave\', function() {
                    speed = 0;});
    
                function updatescroll() {
                    var cur_frame = new Date().getTime();
                    var time_elapsed = cur_frame - prev_frame;
                    prev_frame = cur_frame;
                    if (speed !== 0) {
                        scroll += speed * time_elapsed / 50;
                        if (scroll < 0) scroll = 0;
                        if (scroll > max_scroll) scroll = max_scroll;
                        con.scrollLeft(scroll);
                    }
                    window.requestAnimationFrame(updatescroll);
                }
                window.requestAnimationFrame(updatescroll);
            }
            $("#carousel1").make_carousel();
            $("#carousel2").make_carousel();
    
            function reset(){
                $(\'.maincontent\').find(\'*\').removeAttr(\'class\');
                document.getElementById(\'step1\').setAttribute("class", "visible");
            }   
            function back(){
                var previous_class = $(\'.visible\').data(\'previous\');
                if(previous_class != \'\'){
                    var current_class = $(\'.visible\').attr(\'id\');
                    document.getElementById(current_class).setAttribute("class","");
                    document.getElementById(previous_class).setAttribute("class","visible");
                }
            }
            function show_next(current,next) {
                document.getElementById(current).setAttribute("class", "hidden");
                document.getElementById(next).setAttribute("class", "visible");
            }
            
            function show_hide(show_ele,hide_ele) {
                document.getElementById(show_ele).style.display = "block";
                document.getElementById(hide_ele).style.display = "none";
            }
            function load_after_sec(id) {
                count = 0;
                wordsArray = ["5", "4", "3", "2", "1"];
                var timerID = setInterval(function () {
                    count++;
                    if(count == 5){
                        $("#"+id).show();
                        $("#seconds_counter").hide();
                        clearInterval(timerID);
                    } else {
                        $("#num_sec").fadeOut(400, function () {
                            $(this).text(wordsArray[count % wordsArray.length]).fadeIn(400);
                        });
                    }                               
                }, 2000);
            }
            function showButton(){
                document.getElementById("btn_repeat").style.display=\'block\';
            }
    });
         
        })(jQuery);      
    
    控制台对此给出了一个错误cr-scroll.js 归档该文件con[0] is undefined.

    如果我删除了这个文件,插件就可以正常工作了,为什么会有冲突呢?

    如有任何帮助,我们将不胜感激,并提前向您表示感谢。

1 个回复
最合适的回答,由SO网友:D. Dan 整理而成

将其放在if子句中,以便仅在首页加载:

if (is_front_page()){ // you could also try is_home() 
//or is_page($the_id_of_home_page = 555) If you know the id of the home page

        wp_register_script(\'java1\', get_template_directory_uri() . \'/js/cr-scroll.js\', array( \'jquery\' ));
wp_enqueue_script( \'java1\' );
        }
或在特定页面上删除它:

function remove_scroll_js(){
    if (is_page($the_id_of_that_page_lets_say = 1001)){
    wp_dequeue_script( \'java1\' );
    wp_deregister_script( \'java1\' );
}
}

add_action( \'wp_enqueue_scripts\', \'remove_scroll_js\' );

结束

相关推荐

WP_LOCALIZE_SCRIPT()和JavaScript命名空间

如你所知,wp_localize_script() 允许从PHP对象创建JavaScript对象。因此,在运行下面示例中的代码之后:wp_localize_script(\'my-script\', \'var_name\', $var); // This works 我们将以一个名为var_name 可在脚本中访问的my-script. 此JavaScript变量是从原始PHP变量复制而来的$var.目前一切正常。我面临的问题是:我喜欢能够分配var_name 到JavaScript命名空间