2014-11-04 73 views
0

我是一個WordPress的新手。我遇到連接js/jQ的問題。我沒有閱讀關於如何安裝jQuery的文章。jQuery(scrolltop)在wordpress中不工作

我想要做的是做一個按鈕,單擊時滑動。

而我的問題是,我用wp_enqueue_script加載腳本,並分別在</head></body>之前放置wp_head,wp_footer。但是,Wordpress會將所有腳本放在主體的底部。

<?php 

add_action('wp_enqueue_scripts', 'script_loader'); 


function script_loader() { 

    //not sure if I should load the wp jquery fuction// 

    wp_enqueue_script('jquery'); 

    wp_enqueue_script('jquery-ui-core'); 

    wp_enqueue_script('jquery-effects-core'); 


    //Here's the three scripts that I want to hook// 

    wp_enqueue_script('skrollr', get_template_directory_uri() . '/js/skrollr.js', array(), NULL, true); 

    wp_enqueue_script('init', get_template_directory_uri() . '/js/init.js', array(), NULL, true); 

    //This is the jQuery one 
    wp_enqueue_script('slide', get_template_directory_uri() . '/js/slide.js', array('jquery'), NULL, false); 
} 

假設我是正確的,jQuery的假設要加載的頭部沒有結束身體, 所以我也試圖用打電話給我slide.js在頭(我知道這是不正確的方式),才調用,但slide.js仍然沒有被解僱

這裏的幻燈片代碼:

jQuery(document).ready(

    function($) { 

    $('.down').click(function() { 

     var x= $(window).scrollTop(); 
     var s; 

     if (x < 500){ 
     s = 500; 
     }else if(500 <= x && x <700){ 
     s = 700; 
     }else if(700 <= x && x < 1000){ 
     s=1000; 
     }else{ 
     s=1020; 
     }; 

     $('html, body').animate(
     {scrollTop: s}, 1000); 

    }); 
    }; 

); 

這是非常令人沮喪,因爲沒有任何反饋,當你做錯事的時候,任何人都可以請幫幫我 ? :(

+0

是否有任何錯誤打印在控制檯上? – Jesse 2014-11-04 03:18:27

+0

不!它很好地加載了頁面,點擊時按鈕沒有響應。更準確地說,我試圖在codepen它工作正常:http://codepen.io/iamkaikai/pen/hIyrc 但不是在我的WordPress的網站: http://rocknshin.net84.net/wordpress/ – 2014-11-04 03:54:54

+0

嘗試用'jQuery'替換'$'符號,因爲'$'有時與另一個庫衝突。 還要確保您包含所有必需的js和css文件。 希望這有助於。乾杯!!! – 2014-11-04 04:30:33

回答

0

我發現了問題的所在。原來,我的按鈕被其他層重疊,所以當我點擊該按鈕時,我真的沒有任何點擊。

只需簡單改變的z-index以更大的價值和一切運作良好。沒有錯jQuery :)