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多功能日期选择器插件

一个超轻量级支持自定义的跨浏览器日期选择器插件,功能丰富强大,支持回调函数。
  日期时间
 3664  0

jquery多种模式下日期时间选择器插件

一款layui出品的laydate时间日期选择器,包含多种模式触发弹出选择器窗口。
  日期时间
 8639  0

jquery可自定义的波斯语日历插件

kamaDatepicker是jQuery的一个可自定义的弹出层日历插件,鼠标点击文本框可选择波斯(Jalali)日期,带左右箭头切换月份。
  日期时间
 5656  0

javascript创建的动态的3D模拟时钟特效代码

一款js实现的在线时钟特效,时钟指针动态移动,以准确显示小时、分钟和秒。三根不同颜色指针:黑色(时针)、蓝色(分针)、红色(秒针)。
  日期时间
 4661  0

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

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