在前端开发中,难免会用到倒计时。如做的双十一活动,在距活动开始的半个月前需要做些宣传工作,需要告知用户优惠活动什么时候开始。这个时候就要用到倒计时,如在整站的某个页面提醒用户活动什么时候开始等。而在活动的后期,特别是在距活动结束仅有1天左右时,就要用到弹窗倒计时。这个倒计时设置在整站的首页顶部(当然也可以设置在其它地方,如首页中部等),并设置弹窗弹出10秒后自动消失,由用户决定是否点击到相应的活动页面,购买产品。

需要的技术支持:CSS3,jQuery库;

HTML代码如下:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
<section class="the_body">
    <div class="countdown">
        <h3>距中国雄于地球之日还有</h3>
        <div class="countdown_time">
          <span class="the_days"><i>0</i><i>3</i></span>
          <i class="date_text">天</i>
          <span class="the_hours"><i>0</i><i>7</i></span>
          <i class="date_text">时</i>
          <span class="the_minutes"><i>4</i><i>7</i></span>
          <i class="date_text">分</i>
          <span class="the_seconds"><i>1</i><i>1</i></span>
          <i class="date_text">秒</i>
        </div>
    </div>
</section>

css代码如下:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
.the_body{width: 100%;max-width: 640px;min-width: 320px;margin: 0 auto;}
.countdown{background:#ffec20;padding: 10px 0;}
.countdown h3{margin:0;padding:5px 0;color:#f53544;text-align:center;font-size:14px;}
.countdown .countdown_time{display:block;width:100%;text-align:center;}
.countdown .countdown_time i{display:inline-block;position:relative;padding:0 3px;font-style:normal;background:#fff;
margin:0 2px;border-radius:3px;box-shadow:0px 1px 1px #ccc;border-bottom:1px solid #cfcfcf;font-weight
:bold;}
.countdown .countdown_time i:after{content:"";width:100%;border:1px solid #cfcfcf;border-width:1px 0 0;position:absolute;
bottom:1px;left:0;}
.countdown .countdown_time i:before{content:"";width:100%;border:1px solid #cfcfcf;border-width:1px 0 0;position:absolute;
bottom:3px;left:0;}
.countdown .countdown_time .date_text{background:transparent;font-weight:bold;box-shadow:none;
border-bottom:none;text-decoration:none;padding: 0;}
.countdown .countdown_time .date_text:after{content:"";border:none;}
.countdown .countdown_time .date_text:before{content:"";border:none;}

JavaScript代码如下:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<script>
function remaintime() {
  var date = new Date("Jan 1,2015 00:00:00");//设置倒计时结束时间
  var nowdate = new Date();//获取当前日期
  var remaintime = date.getTime() - nowdate.getTime();//获取现在到倒计时结束时间的毫秒数
  var remainday = Math.floor(remaintime / (1000 * 60 * 60 * 24));//计算求得剩余天数
  var remainhour = Math.floor((remaintime - remainday * 1000 * 60* 60 * 24)/ (1000 * 60 * 60));//计算求得剩余小时数
  var remainminute = Math.floor((remaintime - remainday * 1000 * 60* 60 * 24 - remainhour * 1000 * 60 * 60)/ (1000 * 60));//计算求得剩余分钟数
  var remainsecond = Math.floor((remaintime - remainday * 1000 * 60 * 60 * 24- remainhour * 1000 * 60 * 60 - remainminute *
        1000 * 60) / (1000));//计算求得剩余秒数
  //当剩余天数小于10时,就在其前加一个0,以下剩余小时数、分钟数与秒数与此相同
  if (remainday < 10) {
    remainday = "0" + remainday;
  }else{remainday+="";
  //当剩余天数大于10时,剩余天数为数值,这是需要将该值转换为字符串,以下的剩余小时数、分钟数与秒数与此相同
}
  if (remainhour < 10) {
    remainhour = "0" + remainhour;
  }else{remainhour+="";}
  if (remainminute < 10) {
    remainminute = "0" + remainminute;
  }else{remainminute+="";}
  if (remainsecond < 10) {
    remainsecond = "0" + remainsecond;
  }else{remainsecond+="";}
  $(".the_days i:first-child").html(remainday.substr(0, 1));
  $(".the_days i:last-child").html(remainday.substr(1, 2));
  $(".the_hours i:first-child").html(remainhour.substr(0, 1));
  $(".the_hours i:last-child").html(remainhour.substr(1, 2));
  $(".the_minutes i:first-child").html(remainminute.substr(0, 1));
  $(".the_minutes i:last-child").html(remainminute.substr(1, 2));
  $(".the_seconds i:first-child").html(remainsecond.substr(0, 1));
  $(".the_seconds i:last-child").html(remainsecond.substr(1, 2));
  setTimeout("remaintime()",1000);//设置1秒后调用remaintime函数
}
remaintime();
setTimeout(function(){$(".countdown").hide();},10000);//在首页设置的弹窗效果,在分页这段代码可以不设置
</script>

这是我自己写的倒计时效果,当然每个人都可以根据自己的爱好,设置倒计时的效果。如你可以只显示“几天几时几分”,但个人觉得没有设置到“几天几时几分几秒”够气氛。这里的样式也都可以根据自己的喜好改动,但最终的效果都是制造活动开始前的火热氛围。

至于这里的html代码、css代码及JavaScript代码需要注意的也说下:

1.html代码就不多说,主要就是怎么设置dom,以易于JavaScript操作;

2.css代码,这里主要用了:before与:after伪类,设置倒计时数值的立体效果;

3.JavaScript代码也是很简单的一个函数,这里你需要将得到的剩余时间转换为字符串,以便于字符串的截取显示等。另外,用setTimeout函数设置隔1秒执行一次函数,以动态显示剩余时间,当然也可以用setInterval函数,这两个函数设置的效果基本相同。

至此,一个简单的倒计时效果就做出来了。如果要在首页设置弹窗倒计时,你可以把背景设置的更炫酷一点,这样可以吸引用户点击,并设置10秒后弹窗自动消失(或者设置一个关闭按钮等)。

倒计时的实现可以有很多种方式,在这里也就先介绍这一种,以后有时间将会继续总结。

javascript倒计时代码及倒计时弹窗的更多相关文章

  1. javascript 倒计时代码

    <script language="javascript" type="text/javascript"> var interval = 1000; ...

  2. JS倒计时代码

    第一种:精确到秒的javascript倒计时代码 HTML代码: <form name="form1"> <div align="center" ...

  3. JS倒计时 代码

    JS倒计时 代码 <div> <span id="KSD">3</span>天 <span id="KSH">1 ...

  4. 活动倒计时代码(精确到毫秒)jquery插件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. 原生js 当前时间 倒计时代码

    源:https://www.oschina.net/code/snippet_2318153_54763 <!DOCTYPE html> <html> <head> ...

  6. jquery倒计时代码

    jquery倒计时代码<pre> <span id="day_show">0天</span> <strong id="hour_ ...

  7. javascript 使用 setInterval 实现倒计时

    javascript 使用 setInterval 实现倒计时 var timer = setInterval(function () { console.log(valid_time); if (v ...

  8. js倒计时,秒倒计时,天倒计时

    按天倒计时 HTML代码1: <Script Language="JavaScript"> <!-- Begin var timedate= new Date(& ...

  9. Javascript特效代码大全(420个)(转)

    转载自:Javascript特效代码大全(420个) 收集资料,以便使用+面试+学习  ├ Cookie脚本 ├ 随访问次数变提示 ├ 集成Cookies ├ 使窗口仅弹出一次 ├ 签名提示程序 ├ ...

随机推荐

  1. _bzoj1798 [Ahoi2009]Seq 维护序列seq【线段树 lazy tag】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1798 注意,应保证当前节点维护的值是正确的,lazy tag只是一个下传标记,在下传时应即时 ...

  2. Jewel Magic UVA - 11996 || bzoj1014: [JSOI2008]火星人prefix

    Jewel Magic UVA - 11996 这是一道用splay/非旋treap做的题(这里用的是非旋treap) 1/2/3是splay/非旋treap的常规操作.对于操作4,可以用哈希法求LC ...

  3. 题解报告:hdu 1754 I Hate It(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某 ...

  4. UML 用例图(转载)

    UML是系统架构设计师考试的一个重要考点,需要考生掌握.但是有些考生,在学习的过程中会有这样的疑问,在敏捷开发时代,UML还有没有必要去学习? UML还是有用的,主要用在设计和分析阶段,但是UML不适 ...

  5. [转]Visual F# Samples and Walkthroughs

    本文转自:http://msdn.microsoft.com/en-US/library/vstudio/ee241126.aspx This topic provides links to samp ...

  6. 工作记录 SQL prompt .net平台版本安装

    昨天泡脚了,虽然依然睡的很晚,但是身体很舒服,尽量坚持把 上午尝试用一根网线,链接服务器和笔记本,但是设置好了,Ping不通. 下午安装SQL插件SQL prompt https://www.cnbl ...

  7. 手写一套迷你版HTTP服务器

    本文主要介绍如何通过netty来手写一套简单版的HTTP服务器,同时将关于netty的许多细小知识点进行了串联,用于巩固和提升对于netty框架的掌握程度. 服务器运行效果 服务器支持对静态文件css ...

  8. Linux之测试服务器和端口连通

    目录 wget工具 telnet工具 ssh工具 wget工具: 该工具是网络自动下载工具,如果linux或centos中不存在,需要先安装,支持http.https.ftp协议,wget名称的由来是 ...

  9. asterisk-java ami3 属性改变监听

    asteriskServer.addAsteriskServerListener(new AsteriskListenerInit());//服务属性监听会自动连接服务 实现AsteriskServe ...

  10. opencv实现人脸,人眼,微笑检测

    1.首先实现人脸检测 import cv2 img = cv2.imread("people.jpg",1) #读入图像 #导入人脸级联分类器引擎,“.xml”文件里包含了训练出来 ...