2014-02-24 48 views
0

行,WordPress的3.8:jQuery不工作 - 沒有錯誤

所以我有這種奇怪的情況。我試圖在Wordpress 3.8.1上運行的Wordpress單頁頁面中包含Paul Underwood的簡單平滑滾動腳本(http://www.paulund.co.uk/smooth-scroll-to-internal-links-with-jquery)。 但是,平滑滾動不起作用。

該腳本在JFiddle上完美工作,我檢查了它的錯誤,但它是一個簡單的從源代碼複製粘貼,所以不應該是問題。我很確定我已經在functions.php中正確排列了它(是的,我還註冊了jQuery)。它應該在noConflict中工作。

那麼我在這裏錯過了什麼?如果這是一個愚蠢的小錯誤,不會讓我感到吃驚......

無論如何,在此先感謝大家:)

的HTML:

<a href="#main"><img class="arrow" src="<?php bloginfo('stylesheet_directory'); ?>/images/arrow-down.png" alt="scroll down"></a> 

腳本:

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

$(document).ready(function() { 
$('a[href^="#"]').on('click',function (e) { 
    e.preventDefault(); 

var target = this.hash, 
    $target = $(target); 

$('html, body').stop().animate({ 
    'scrollTop': $target.offset().top 
}, 900, 'swing', function() { 
    window.location.hash = target; 
    }); 
}); 
}); 

}); 

the functions.php

function my_scripts() { 
wp_enqueue_script('jquery'); 
wp_enqueue_style('my-style', get_stylesheet_uri()); 
wp_register_script('my-script', get_template_directory_uri().'/js/my-script.js', array('jquery'), '1.0', true); 
wp_enqueue_script('my-script'); 
} 

add_action('wp_enqueue_scripts', 'my_scripts'); 

回答

0

好的,所以我想通了,夥計們。

正如我預測的那樣,這是一些愚蠢的小東西。

有舍利子

body{overflow-x: hidden} 

在我的樣式表。

這是什麼一直在拖我 - 刪除線,現在一切正常。

感謝您的幫助,雖然:-)

0

當你的代碼說,你有兩個doc ready處理程序,並可以去除內doc ready第一行是更好:

jQuery(document).ready(function($) { // keep it, its better. 

    $(document).ready(function() { //<---remove this line and its closing 

所以最終代碼應該是:

jQuery(document).ready(function($) { 
    $('a[href^="#"]').on('click',function (e) { 
     e.preventDefault(); 

     var target = this.hash, 
      $target = $(target); 

     $('html, body').stop().animate({ 
       'scrollTop': $target.offset().top 
     }, 900, 'swing', function() { 
       window.location.hash = target; 
     }); // end of animate 
    }); // end of click 
}); // end of doc ready 
+0

嘿Jai,謝謝你的提示。我嘗試刪除額外的處理程序,但不幸的是,該腳本仍然不工作:( – underscored

0

WordPress的使用jQuery,而不是$默認。嘗試將所有的$替換爲jQuery

jQuery(document).ready(function() { 
    jQuery('a[href^="#"]').on('click',function (e) { 
     e.preventDefault(); 

     var target = this.hash, 
     $target = jQuery(target); 

     jQuery('html, body').stop().animate({ 
      'scrollTop': $target.offset().top 
     }, 900, 'swing', function() { 
      window.location.hash = target; 
     }); // end of animate 
    }); // end of click 
}); // end of doc ready 

試試這個。

編輯:哦,我剛剛看到你給$作爲函數的參數。我的錯。

但你有沒有檢查過,如果你可能包括兩次jQuery?

+0

嘿appnic,我檢查了控制檯和jQuery只包含一次頭。WordPress的確自動包含jQuery的遷移,可以有任何影響? – underscored

+0

我不這麼認爲,因爲jQuery-migrate只是增加了一些不推薦使用的函數,但是在一些較老的插件中實現(以確保兼容性)。 – appnic