jquery動畫1.載入指標

來源:互聯網
上載者:User

該系列文章是我閱讀《jQuery 1.4 Animation Techniques》後的總結,有興趣的朋友可以去看原版書籍。
  動畫效果對於豐富網站的互動體驗起到了不可或缺的作用,他可以讓你的網站更酷,更吸引人。

什麼時候使用動畫:
  1.當顯示或隱藏頁面、彈出框或內容地區的時候;
  2.當一些內容從頁面的一個地方移動到其他地方的時候;
  3.當頁面中的一些內容,應使用者的操作而產生狀態改變的時候;
  4.當一些內容在幾種狀態間轉變的時候;
  5.引導使用者去執行某些操作,或者引起他們對某些很重要訊息注意的時候。

什麼時候不應使用動畫:
  1.當某些操作是需要使用者頻繁重複操作的時候;
  2.當裝置無法充分顯示動畫效果的時候(也就是說該動畫會佔用電腦的很多資源,會影響到效能);
  3.即時操作。

  當然這些規定都不是絕對的,你需要根據你的實際情況來決定是否使用動畫。下面是一些checklist(驗證列表),只要滿足下面幾條選項,你的動畫就是有價值的。
  1.動畫是否適合你的勘探;
  2.動畫是否實用;
  3.動畫是否增強了使用者體驗;
  4.動畫是否以合適的速度運行。

  下面我們開始我們的第一個例子,建立一個動畫載入指標,我直接把源碼貼出來,上面有注釋。
複製代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
//建立loader div對象
//這裡使用jQueryjQuery(html,[ownerDocument])方法,不懂得朋友去看下api
var loader = $("<div></div>", {
id: 'loader'
}).css('display', 'none');
//建立顯示條
var bar = $('<span></span>').css('opacity', 0.2);
//時間間隔對象
var loadingInterval = null;
//建立三個顯示條,並添加到loader對象中
for (var i = 0; i < 3; i++) {
bar.clone()//使用clone的可以提高效能,不用每次都去重新建立對象,clone比建立來的快
.addClass('bar-' + i).appendTo(loader);
}
//將loader添加到go按鈕後面
loader.insertAfter('#go');
function runLoader() {
//擷取三個顯示條
var firstBar = loader.children(':first'),
secondBar = loader.children().eq(1),
thirdBar = loader.children(':last');
//分別對三個顯示條執行動畫效果
//總的效果就是依次讓三個顯示條,透明度變為1,再變為0.2
firstBar.fadeTo('fast', 1, function () {
firstBar.fadeTo('fast', 0.2, function () {
secondBar.fadeTo('fast', 1, function () {
secondBar.fadeTo('fast', 0.2, function () {
thirdBar.fadeTo('fast', 1, function () {
thirdBar.fadeTo('fast', 0.2)
});
});
});
});
});
};
//對go按鈕設定toggle事件
$('#go').toggle(function () {
//第一次點擊的時候,顯示loader,建立setInterval,每1200毫秒執行一次runLoader
loader.show();
runLoader();
loadingInterval = setInterval(runLoader, 1200);
}, function () {
//再次點擊的時候,隱藏loader,刪除setInterval
loader.hide();
clearInterval(loadingInterval);
});
});
</script>
<style type="text/css">
#loader
{
margin: 10px 0 0 36px;
}
#loader span
{
display: block;
width: 6px;
float: left;
margin-right: 6px;
border: 1px solid #336633;
position: relative;
background-color: #ccffcc;
}
#loader .bar-0
{
height: 15px;
bottom: -20px;
}
#loader .bar-1
{
height: 25px;
bottom: -10px;
}
#loader .bar-2
{
height: 35px;
margin-right: 0;
}
</style>
</head>
<body>
<input id="go" type="button" value="Initiate the action" />
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.