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

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

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

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

    • index.html

使用方法

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. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 日期时间

jquery轻量级秒表计时器插件

该插件有两种不同的操作方式,一种是可点击复位归零;另一种为从外部更新倒计时。
  日期时间
 3828  0

jquery响应式事件日历插件

一款基于calendar事件日历插件,可对指定日期添加自定义事件,响应式布局设计,适配移动端和电脑端使用。
  日期时间
 9838  0

jquery自动补全日期时间插件

只需输入一个数字,点击按钮或回车自动补全转换成时间格式,非常的实用。
  日期时间
 71027  0

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

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

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

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