jquery动画

jQuery animate() 方法允许您创建自定义的动画。


语法:

$(selector).animate({params},speed,callback);

注意:

必需的 params 参数定义形成动画的 CSS 属性。
可选的 speed 参数规定效果的时长。它可以取以下值:"slow"、"fast" 或毫秒。
可选的 callback 参数是动画完成后所执行的函数名称。


提示:

默认地,所有 HTML 元素都有一个静态位置,且无法移动。

如需对位置进行操作,要记得首先把元素的 CSS position 属性设置为 relative、fixed 或 absolute!


实例1:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js">
</script>
<script> 
$(document).ready(function(){
  $("button").click(function(){
    $("div").animate({left:'250px'});
  });
});
</script> 
</head>
 
<body>
<button>开始动画</button>
<div style="background:gold;height:100px;width:100px;position:absolute;">
</div>

</body>
</html>


实例2:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js">
</script>
<script> 
$(document).ready(function(){
  $("button").click(function(){
    $("div").animate({
      left:'250px',
      opacity:'0.5',
      height:'150px',
      width:'150px'
    });
  });
});
</script> 
</head>
 
<body>

<button>开始动画</button>
<div style="background:gold;height:100px;width:100px;position:absolute;">
</div>

</body>
</html>



实例3:


<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
<script> 
$(document).ready(function(){
  $("button").click(function(){
    var div=$("div");  
    div.animate({left:'100px'},"slow");
    div.animate({fontSize:'3em'},"slow");
  });
});
</script> 
</head>

<body>

<button>开始动画</button>
<div style="background:gold;height:100px;width:200px;position:absolute;">HELLO</div>

</body>
</html>


当然你也可以使用callback函数,

Callback 函数在当前动画 100% 完成之后执行,比如说动画结束后弹出一个警告框:


<html>
<head>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
  $("div").animate({
      left:'0px',
      opacity:'0.5',
      height:'700px',
      width:'1336px',
   fontSize:'36em'
    },1000,function(){

  alert("animation end");
  
  var div=$("div");
 div.animate({left:'10px',height:'100px',width:'100px'},"slow");
div.animate({fontSize:'3em'},"slow");

    });
  });
});
</script>
</head>

<body>
<button type="button">click me</button>
<div style="color:red;background:gold;height:100px;width:100px;position:absolute;">shit</div>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值