Discuz教程网

jQuery animate()方法设计倒计时

[复制链接]
authicon dly 发表于 2011-4-22 02:17:07 | 显示全部楼层 |阅读模式
jQuery animate()方法设计倒计时,用jquery的animate()方法调用计时;用jquery的stop()方法停止计时;用jquery的data()方法记录一些变量,使之重新开始。
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>jQuery 设计倒计时</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. </head>
  7. <body>
  8. <h5>jQuery 设计倒计时</h5>
  9. <button id="start" >开始</button>
  10. <button id="pause" >暂停</button>
  11. <button id="again" >重新开始</button>
  12. <div id="test" ></div>
  13. <script type="text/javascript" src="/uploads/swfupls/jquery-1.4.2.js"></script>
  14. <script type="text/javascript" >
  15. jQuery.fn.countDown = function(settings,to) {
  16. if(!to && to != settings.endNumber) { to = settings.startNumber; }
  17. this.data("CurrentTime",to);
  18. $(this).text(to).animate({"none":"none"},settings.duration,'',function() {
  19. if(to > settings.endNumber + 1) {
  20. $(this).countDown(settings,to - 1);
  21. }else{
  22. settings.callBack(this);
  23. }
  24. });
  25. return this;
  26. };
  27. //计时&&重新计时
  28. jQuery.fn.CcountDown = function(settings) {
  29. settings = jQuery.extend({
  30. startNumber: 10,
  31. endNumber: 0,
  32. duration: 1000,
  33. callBack: function() { }
  34. }, settings);
  35. this.data("Duration",settings.duration);
  36. this.data("EndNumber",settings.endNumber);
  37. this.data("CallBack",settings.callBack);
  38. return this.stop().countDown(settings);
  39. };
  40. //计时暂停
  41. jQuery.fn.pause = function(settings) {
  42. return this.stop();
  43. };
  44. //暂停后,重新开始
  45. jQuery.fn.reStart = function() {
  46. return this.pause().CcountDown({
  47. startNumber : this.data("CurrentTime"),
  48. duration : this.data("Duration"),
  49. endNumber : this.data("EndNumber"),
  50. callBack : this.data("CallBack")
  51. });
  52. };
  53. /***** 测试 ****/
  54. // 开始
  55. $("#start").click(function(){
  56. $("#test").CcountDown({
  57. startNumber:15,
  58. callBack:test
  59. })
  60. .css("color","red");
  61. });
  62. // 暂停
  63. $("#pause").click(function(){
  64. $("#test").pause();
  65. });
  66. // 重新开始
  67. $("#again").click(function(){
  68. $("#test").reStart();
  69. });
  70. function test(){
  71. $("<p>计时结束!</p>")
  72. .hide()
  73. .fadeIn(1000)
  74. .appendTo("body");
  75. }
  76. </script>
  77. </body>
  78. </html>
复制代码




上一篇:自动向上滚动的文本,鼠标放上去就停止
下一篇:文字上下左右循环连续滚动代码,解决marquee的留白问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 16:15

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表