標籤:style class blog code java ext
1.setInterval(function(){},[time])
樣本:這是一個類比進度條的代碼,結束後轉向另一頁面。
<script type="text/javascript"> $(function () { var i = 0; var t = 0; setInterval(function () { if (i < 100) { i = i + 5; t++; $("#linepercent").css("height", i + "%"); $("#linetime").text(t + "‘"); } else { window.location.href = "succ.html"; } }, 1000); });</script>
<style type="text/css"> .progress { position: absolute; z-index: 200; right: 20px; top: 20px; } .line { width: 13px; height: 140px; margin: 0 auto; background-color: #d6d6d6; border-radius: 20px; } .round { width: 60px; line-height: 60px; background-color: #2bc4c2; border-radius: 50%; font-weight: bold; color: #fff; margin-top: -5px; } .line-now { background-color: #2bc4c2; border-radius: 20px; /*height: 50%;*/ bottom: 0px; } .font-nr { font-size: 15px; line-height: 45px; } </style> <div id="progressbar" class="progress"> <ul class="line"> <div id="linepercent" class="line-now"></div> </ul> <ul id="linetime" class="round font-nr"> 0‘ </ul> </div>
2.在某個操作後後面的代碼順延強制
設定一個延時來延遲執行隊列中之後的項目。
delay(duration, [queueName])
duration (Integer) 以毫秒為單位的整數,用於設定隊列延遲執行的時間。
queueName (String) 選擇性參數,隊列名的字串。預設是動畫隊列 fx 。
.delay() 用於將隊列中的函數延時執行。它既可以延遲動畫隊列中函數的執行,也可以用於自訂隊列。只有在隊列中的連續事件可以被延時,因此不帶參數的 .show() 和 .hide() 就不會有延時,因為他們沒有使用動畫隊列。
毫秒為單位的延時,數字越大,動畫越慢。字串 ‘fast‘ 和 ‘slow‘ 分別代表200和600毫秒的延時。
例如,我們可以在 <div id="foo"> 的 .slideUp() 和 .fadeIn() 動畫之間添加800毫秒的延時 :
$(‘#foo‘).slideUp(300).delay(800).fadeIn(400);
當這句語句執行後,元素會有300毫秒的捲起動畫,接著暫停800毫秒,再出現400毫秒淡入動畫。
.delay() 用在jQuery動畫或者類似隊列中是再好不過的了。但是,由於其本身的限制,比如無法取消延時,所以不應完全用來替代 JavaScript 原生的 setTimeout 函數,後者更適用於通常情況。
3.setTimeout(function(){})
setTimeout()
從載入後延遲指定的時間去執行一個運算式或者是函數;
僅執行一次 ;和window.clearTimeout一起使用.
如果用這個函數,那麼第一個進度條的例子可以改成下面的範例程式碼,
demoProcess(); function demoProcess() { if (i < 100) { i = i + 5; t++; $("#linepercent").css("height", i + "%"); $("#linetime").text(t + "‘"); } else { window.location.href = "succ.html"; } setTimeout(demoProcess, 1000); }
以上都用了jquery類庫。