当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jQuery中animate()的使用方法及解决$(”body“).animate({“scrollTop”:top})不被Firefox支持的问题

jQuery中animate()的使用方法及解决$(”body“).animate({“scrollTop”:top})不被Firefox支持的问题

2019年03月18日  | 移动技术网IT编程  | 我要评论

jquery中animate()的方法

用于创建自定义动画的函数。

返回值:jquery animate(params, [duration], [easing], [callback])

如果使用的是“hide”、“show”或“toggle”这样的字符串值,则会为该属性调用默认的动画形式。paramsoptions一组包

含作为动画属性和终值的样式属性和及其值的集合

params 对象{},注意:所有指定的属性必须用骆驼形式,比如用marginleft代替margin-left,如果使用的是“hide”、

“show”或“toggle”这样的字符串值,则会为该属性调用默认的动画形式。

duration (可选)三种预定速度之一的字符串("slow", "normal", or "fast")或表示动画时长的毫秒数值(如:1000)

easing (可选)string要使用的擦除效果的名称(需要插件支持).默认jquery提供"linear" 和 "swing"

callback (可选)function在动画完成时执行的函数

animate实例:

1、点击按钮后div元素的几个不同属性一同变化

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

 $("#block").animate({

  width: "90%",

  height: "100%",

  fontsize: "10em",

  borderwidth: 10

 }, 1000);

}); 

2、让指定元素左右移动

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

 $(".block").animate({ left: '+50px' }, "slow");

});

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

 $(".block").animate({ left: '-50px' }, "slow");

}); 

3、在600毫秒内切换段落的高度和透明度

$("p").animate({

 height: 'toggle', opacity: 'toggle'

}, "slow"); 

4、用500毫秒将段落移到left为50的地方并且完全清晰显示出来(透明度为1)

$("p").animate({

 left: 50, opacity: 'show'

}, 500); 

5、切换显示隐藏

$(".box h3").toggle(function(){ 

 $(this).next(".text").animate({height: 'toggle', opacity: 'toggle'}, "slow"); 

  $(this).addclass("arrow"); 

  return false; 

 

 },function(){ 

  $(this).next(".text").animate({height: 'toggle', opacity: 'toggle'}, "slow"); 

  $(this).removeclass("arrow"); 

  return false; 

 }); 

}); 
//滚动焦点

$(window).scroll(function () {    //当前窗口的滚动事件

var wintop = $(window).scrolltop();  //获取当前窗口的大小

var objtop = $("#obj1").offset().top; //获取当前对象的x坐标

}); 

下面着重说一下$(”body“).animate({“scrolltop”:top})不被firefox支持的问题

$("body").animate({"scrolltop":top})

不被firefox支持问题的解决。

其实是因为使用了body的:

$("body").animate({"scrolltop":top})

只被chrome支持,而不被firefox支持。

而使用html的:

$("html").animate({"scrolltop":top})

只被firefox支持,而不被chrome支持。

如果想让这段js被chrome和firefox都支持的话,应该这样:

$("html,body").animate({"scrolltop":top})

看到了吗,就是将html和body这两者都加上就可以了。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网