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

原生js实现的支持自定义分秒倒计时插件

所属分类: 网页特效-日期时间    2023-12-03 11:43:47

原生js实现的支持自定义分秒倒计时插件 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

原生js实现的支持自定义分秒倒计时插件(共3个文件)

    • index.html

使用方法

// Create the universal variable 'countdown' that lives in the window. 
let countdown;

const timerDisplay = document.querySelector('.display__time-left');
const endTime = document.querySelector('.display__end-time');

// Get all data-keys with built-in timer settings
const buttons = document.querySelectorAll('[data-time');


// This is our main function
function timer(seconds) {
    //If any timers are already going, clear them
    clearInterval(countdown);

    // Date.now is a new JS function, will give time in MS.
    const now = Date.now();

    // Find time in SECONDS by multiplying default MS by 1000
    const then = now + seconds * 1000;

    // Run another function, defined below, as soon as this function is invoked
    displayTimeLeft(seconds);

    // Show the end time, another function defined below. 
    displayEndTime(then);

    // Set this function to the variable that lives in the browser. Set interval is a function that runs every 1000 ms 
    countdown = setInterval(() => {
        const secondsLeft = Math.round((then - Date.now()) / 1000);
      
        // Check when timer is done. 
        if(secondsLeft < 0){
            clearInterval(countdown);
            return;
        }
      
        //display it
        displayTimeLeft(secondsLeft);
      
      
// Run this function every 1000 ms
    }, 1000);
}


//Convert seconds to the formatted display value
function displayTimeLeft(seconds) {

    // Round seconds to whole numbers
    const minutes = Math.floor(seconds / 60);

    // Get the number of whole seconds remaining
    const remainderSeconds = seconds % 60;

    // Check if display needs a leading 0, if there is less than 10 seconds. so, '9' will display as '09'
    const display = `${minutes}:${remainderSeconds < 10 ? '0' : ''}${remainderSeconds}`;

    //Change title of document to be the seconds left
    document.title = display;
    timerDisplay.textContent = display;

}

// Show the static, unchanging END time
function displayEndTime(timestamp) {
  
    // Pass in the timestamp, which has all of the info below built in. This is a default JS method
    const end = new Date(timestamp);

    // Extract hours and minutes from the timestamp
    const hour = end.getHours();
    const minutes = end.getMinutes();

    // Display the time.
    // Check if past 12 noon, subtract 12 hours (not military time)
    // Check if less than 10 minutes. '9' becomes '09'
    endTime.textContent = `${hour > 12 ? hour - 12 : hour}:${minutes < 10 ? '0' : ''}${minutes} 回来`;
}

// Get data from the data attribute buttons, and set them as the timer
function startTimer(){

    // ParseInt to only get whole number
    const seconds = parseInt(this.dataset.time);
    timer(seconds);
}

// Function to get data from pre-set button in data attributes
buttons.forEach(button => button.addEventListener('click', startTimer));

// If you give your form a custom name (name="minutes" in our HTML in this case), you can select it this way
document.customForm.addEventListener('submit', function(e){

    //prevent default browser behavior of reloading the page on time form submit
    e.preventDefault();

    //Get the number of minutes from the input field
    const mins = this.minutes.value;

    // Convert the minutes to seconds, which is what our timer uses
    timer(mins * 60);
    this.reset();

})


/*
todo/ ideas to enhance this timer: 
add timer sound when timer ends

add transitions to the countdown. 

add a "pause" button

*/

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

jquery日历记事本app界面模板

一款日历日记模板,可根据日期添加日记事件,每个日期可添加多个事件,很实用。
  日期时间
 9136  0

jquery圆形环状进度条倒计时插件

一款支持自定义的倒计时计时器插件,可自定义配置:完成后回调、设置圆形边框宽度、颜色等。
  日期时间
 8286  0

jquery时钟罗盘转动特效

一款罗盘转动效果,时分秒三层环形圈循环转动,实时显示当前日期和时间。
  日期时间
 581  0

jquery带事件管理的动画日历插件

类似手机端很多日历带的事件提醒,响应式布局,非常的实用!
  日期时间
 3557  0

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

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