[英].animate() not working on Wordpress site
我正在尝试在与页面上的hashbang链接相关联的滚动上使用动画效果。
当我在常规网站上使用它时,它可以完美运行。
一旦我尝试在wordpress网站上使用它,它就没有动画,它只是跳转到DIV而不是滚动。
jQ代码(尝试将其放在头部,身体和脚注中(没有区别):
<script type="text/javascript">
jQuery.noConflict();
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;
});
});
});
</script>
这是我当前的JQuery版本,以防出现问题?
jquery.js?ver = 1.11.0
jquery-migrate.min.js?ver = 1.2.1
难道是wordpress进入脚本的顺序?
任何想法,因为我在这里拔头发!
更改为此:
<script type="text/javascript">
jQuery(document).ready(function($){ // pass $ as an arg here
您需要在ready回调中传递$
作为参数,并且不需要jQuery.noConflict();
所以删除它。
因为wordpress使用jQuery
而不是$
所以它不会与其他使用$
作为别名的库发生冲突,因此您可以做两件事
$
出现。 尝试使用wrap (function($){ //your content })(jQuery);
(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;
});
});
});
})(jQuery);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.