限时优惠活动
亲爱的苦力吧用户,我们为了回馈新老用户一直以来的鼎力支持,即日起(2025-02-06至2025-03-06)凡是购买苦力吧VIP/充值K币的新老用户,都直接可获得买一送一的优惠馈赠。例如:购买一个月的VIP会员可直接获得两个月的VIP会员;充值100K币可直接获得200K币,以此类推!有任何疑问可联系在线客服,感谢各位用户对苦力吧素材的信任与厚爱,我们将一如既往的给大家上新更多优质的素材源码,祝大家开工大吉、工作顺利、心想事成。

javascript创建的基于canvas的倒计时计时器插件

所属分类: 网页特效-日期时间    2023-12-03 04:57:40

javascript创建的基于canvas的倒计时计时器插件 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

javascript创建的基于canvas的倒计时计时器插件(共3个文件)

    • index.html

使用方法

  • code
  • source
  1. class Countdown {
  2. /**
  3. * @param {Date} dateTo
  4. */
  5. constructor(itemSelector, dateTo) {
  6. this.itemSelector = itemSelector;
  7. let $ = el => document.querySelector(el);
  8. this.dateSelectors = {
  9. d: $('.days').getContext('2d'),
  10. h: $('.hours').getContext('2d'),
  11. m: $('.minutes').getContext('2d'),
  12. s: $('.seconds').getContext('2d')
  13. };
  14. this.dateTo = dateTo;
  15. requestAnimationFrame(() => this.set());
  16. }
  17. /**
  18. * Main function
  19. */
  20. set() {
  21. let today = new Date();
  22. for (let selector in this.dateSelectors) {
  23. let s = this.dateSelectors[selector];
  24. this.clear(s);
  25. this.setTime(s, this.dateDiff(selector, today, this.dateTo));
  26. }
  27. requestAnimationFrame(() => this.set());
  28. }
  29. /**
  30. * Clear canvas
  31. * @param {canvas} ctx
  32. */
  33. clear(ctx) { ctx.clearRect(0, 0, 60, 60); }
  34. setTime(ctx, until) {
  35. ctx.font = 'bold 2rem sans-serif';
  36. ctx.shadowColor = "#000";
  37. ctx.shadowOffsetX = 0;
  38. ctx.shadowOffsetY = 1;
  39. ctx.shadowBlur = 2;
  40. ctx.fillStyle = "#ccc";
  41. ctx.textAlign = "center";
  42. ctx.fillText(until, 25, 40);
  43. }
  44. /**
  45. *
  46. * @param {String} datepart enum {'m', 'd', 'h', 's'}
  47. * @param {Date} fromdate
  48. * @param {Date} todate
  49. */
  50. dateDiff(datepart, fromdate, todate) {
  51. datepart = datepart.toLowerCase();
  52. let diff = todate - fromdate;
  53. let divideBy = {
  54. d: 86400000,
  55. h: 3600000,
  56. m: 60000,
  57. s: 1000,
  58. ms: 1
  59. };
  60. let modulo = {
  61. d: 1,
  62. h: divideBy['d'],
  63. m: divideBy['h'],
  64. s: divideBy['m'],
  65. ms: divideBy['s']
  66. }
  67. return Math.floor(diff % modulo[datepart] / divideBy[datepart]);
  68. }
  69. }
  70. //set tomorrow date
  71. new Countdown('.item-time', new Date(new Date().getTime() + 24 * 60 * 60 * 1000));
class Countdown {

    /**
     * @param {Date} dateTo
     */
    constructor(itemSelector, dateTo) {
        this.itemSelector = itemSelector;
        let $ = el => document.querySelector(el);
        this.dateSelectors = {
            d: $('.days').getContext('2d'),
            h: $('.hours').getContext('2d'),
            m: $('.minutes').getContext('2d'),
            s: $('.seconds').getContext('2d')
        };
        this.dateTo = dateTo;
        requestAnimationFrame(() => this.set());
    }

    /**
     * Main function
     */
    set() {

        let today = new Date();

        for (let selector in this.dateSelectors) {
            let s = this.dateSelectors[selector];
            this.clear(s);
            this.setTime(s, this.dateDiff(selector, today, this.dateTo));
        }
        requestAnimationFrame(() => this.set());
    }

    /**
     * Clear canvas
     * @param {canvas} ctx 
     */
    clear(ctx) { ctx.clearRect(0, 0, 60, 60); }

    setTime(ctx, until) {
        ctx.font = 'bold 2rem sans-serif';
        ctx.shadowColor = "#000";
        ctx.shadowOffsetX = 0;
        ctx.shadowOffsetY = 1;
        ctx.shadowBlur = 2;
        ctx.fillStyle = "#ccc";
        ctx.textAlign = "center";
        ctx.fillText(until, 25, 40);
    }

    /**
     * 
     * @param {String} datepart  enum {'m',  'd', 'h', 's'}
     * @param {Date} fromdate 
     * @param {Date} todate 
     */
    dateDiff(datepart, fromdate, todate) {
        datepart = datepart.toLowerCase();
        let diff = todate - fromdate;
        let divideBy = {
            d: 86400000,
            h: 3600000,
            m: 60000,
            s: 1000,
            ms: 1
        };

        let modulo = {
            d: 1,
            h: divideBy['d'],
            m: divideBy['h'],
            s: divideBy['m'],
            ms: divideBy['s']
        }

        return Math.floor(diff % modulo[datepart] / divideBy[datepart]);
    }

}

//set tomorrow date
new Countdown('.item-time', new Date(new Date().getTime() + 24 * 60 * 60 * 1000));

站长提示:
1. 苦力吧素材官方QQ群:950875342
2. 平台上所有素材资源,需注册登录会员方能正常下载。
3. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励2K币
4. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
5. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 日期时间

原生js实现的每星期和每月的工作时长插件

一款可在线输入计算每星期每月的工作时间插件,它使用moment.js进行时间操作,通过输入开始和结束时间以及休息时间,从而计算每天总工作小时数,并提供整个星期的小计总和。
  日期时间
 3353  0

原生js编写的input弹出层jeDate日期控件源码

一款可自定义的日期选择空间弹出层插件,兼容电脑端和手机端,非常实用!
  日期时间
 1470  0

jquery多种展现风格的活动日历插件

使用了Bootstrap框架和FontAwesome搭建、可自定义、功能齐全的活动时间日历。
  日期时间
 2426  0

javascript实现的5秒后重新加载页面

一款定时加载筛选页面脚本示例,使用JavaScriptset Timeout()方法来设置重新加载页面的计时器,每隔5000毫秒(5秒钟)重新加载一次页面。
  日期时间
 8621  0

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论