jquery animate方法實現緩衝效果詳解

來源:互聯網
上載者:User

animate() 方法

改變 "div" 元素的高度:

 代碼如下 複製代碼

$(".btn1").click(function(){
  $("#box").animate({height:"300px"});
});

完整執行個體

 代碼如下 複製代碼

<html>
<head>
<script type="text/javascript" src="/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function()
  {
  $(".btn1").click(function(){
    $("#box").animate({height:"300px"});
  });
  $(".btn2").click(function(){
    $("#box").animate({height:"100px"});
  });
});
</script>
</head>
<body>
<div id="box" style="background:#98bf21;height:100px;width:100px;margin:6px;">
</div>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>


一個執行個體(動畫函數和動畫隊列)

 代碼如下 複製代碼

CSS部分:

<style type="text/css">

*{margin:0;padding:0;}

ul li{ list-style:none;}

img {border:0 none;}

body { padding:100px;}

input[name='show']{ position:fixed;right:50%; top:50px;}

input[name='hide']{ position:fixed;right:40%; top:50px;}

div.test{ width:100px;position:relative;border:1px blue solid;}

</style>

HTML部分:

<input name="show" type="button" value="點擊測試"/>

<div>內容填充內容填</div>

jQuery部分:

<script type="text/javascript">

$(function(){

$('div.test').css("opacity","0.5");

$('input').focus()

.click(function(){

$('div.test').animate({left:'400px',height:'100px'},200)

.animate({top:'200px',},200)

.animate({left:0},200)

.animate({top:0},{duration:200,step:cb,queue:true})//將queue值改為false,可使該動畫不加入動畫隊列,即該動畫不按預設的先後順序執行; step為動畫函數的callback函數

})

function cb(){

$(this).css({

border:'2px red solid',

opacity:1

})

} })

</script>

這種效果就很牛了,如果使用js不相容不說還很麻煩,有了它緩衝效果一二三就搞定。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.