jquery支持在线自定义日期时间的倒计时插件

所属分类: 网页特效-日期时间    2023-09-01 10:09:29

 41.4K  2  查看评论 (0)
jquery支持在线自定义日期时间的倒计时插件 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

jquery支持在线自定义日期时间的倒计时插件(共3个文件)

    • index.html

使用方法

$(document).ready(function () {

  $(".limit-button").on("click", function () {
    $('.limit').css("display", "none");
    $(".repeat").css("display", "none");
    $("form").append(`<input class="datetime-local" type="datetime-local">`);
    $(".datetime-local").insertBefore(".begin");
    $(".begin").css("display", "flex");
  });
  $(".repeat-button").on("click", function () {
    $('.repeat').css("display", "none");
    $(".limit").css("display", "none");
    $("form").append(`<input class="text" type="text" placeholder="Please type the number of days to countdown.">`);
    $(".text").insertBefore(".begin");
    $(".begin").css("display", "flex");
  });

  $("form").on("submit", function (e) {
    e.preventDefault();

    const typeInput = $("input")[0].type;
    const userInput = $("input").val();
    let seconds;

    if (typeInput == "text") {
      if (userInput == "" || isNaN(userInput)) {
        alert("请输入有效的日期时间");
      } else if (!isNaN(userInput)) {
        seconds = parseInt(userInput) * 86400;
        console.log(seconds);
      }
    } else {
      if (userInput == "") {
        alert("请输入有效的日期时间");
      } else {
        const date = new Date(userInput);
        const now = new Date();
        seconds = parseInt((date.getTime() - now.getTime()) / 1000);
        console.log(seconds);
      }
    }

    if (!seconds || seconds < 0) {
      alert("请输入有效的日期时间");
    } else {
      let days, hours, minutes;

      if (typeInput == "text") {
        days = Math.floor(seconds / (3600 * 24)) - 1;
        hours = 23;
        minutes = 59;
        seconds = 59;
      } else {
        days = Math.floor(seconds / (3600 * 24));
        seconds -= days * 3600 * 24;
        hours = Math.floor(seconds / 3600);
        seconds -= hours * 3600;
        minutes = Math.floor(seconds / 60);
        seconds -= minutes * 60;
      }

      $(`.${typeInput}`).remove();
      $("button.begin").css("display", "none");
      $("form").append(`<button class="stop">停止倒计时</button>`);

      const validationPrint = (timeUnit) => {
        return timeUnit < 10 ? `0${timeUnit}` : timeUnit;
      };

      $("#seconds").text(validationPrint(seconds));
      $("#minutes").text(validationPrint(minutes));
      $("#hours").text(validationPrint(hours));
      $("#days").text(validationPrint(days));

      const changeTimeWithLimit = setInterval(() => {
        seconds -= 1;
        $("#seconds").text(validationPrint(seconds));
        if (seconds === 0 && minutes > 0) {
          seconds = 60;
          minutes -= 1;
          $("#minutes").text(validationPrint(minutes));
        }
        if (seconds === 0 && minutes === 0 && hours > 0) {
          seconds = 60;
          minutes = 60;
          hours -= 1;
          $("#hours").text(validationPrint(hours));
        }
        if (seconds === 0 && minutes === 0 && hours === 0 && days > 0) {
          seconds = 60;
          minutes = 60;
          hours = 24;
          days -= 1;
          $("#days").text(validationPrint(days));
        }
        if (seconds === 0 && minutes === 0 && hours === 0 && days === 0) {
          clearInterval(changeTimeWithLimit);
        }
      }, 1000);

      $(".stop").on("click", function () {
        clearInterval(changeTimeWithLimit);
        console.log(`stop`);
        $(".limit").css("display", "block");
        $(".repeat").css("display", "block");
        $(this).remove();

        $("#seconds").text("00");
        $("#minutes").text("00");
        $("#hours").text("00");
        $("#days").text("00");
      });
    }
  });
});

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

像素方块数字时钟CSS3特效代码

一款像素方块堆砌的时钟特效,单色的像素方块会跟随秒数进行填充,当把小时和分钟的数字方块填充完之后,刚好是60秒,非常的有趣!
  日期时间
 91736  0

jquery带升降按钮的日期时间控制器插件

两种格式:天/时/分;时/分,按升降按钮自动增加或递减,非常实用。
  日期时间
 3792  0

基于AngularJS的日期和时间选择组件

一款基于AngularJS的日期时间选择组件,支持年月日的快速选择定位,当前这款AngularJS日期选择组件还支持时分秒时间选择,可精确到秒。
  日期时间
 5722  0

jquery带星期几的多语言日期选择器插件

一个黑色风格日期选择器插件,可选择诸如dd-mm-yyyy格式的日期。提供超过80种多个国家语言包,可自定义设置界面语言模板,并且选中指定日期后会自动alert弹出框显示选中的日期。
  日期时间
 8776  0

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

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