JS定时器,时钟,倒计时

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>定时器弹框</title>

<style type="text/css">

.pop{

width: 400px;

height: 300px;

background-color: #fff;

border: 1px solid #000;

/*固定定位*/

position: fixed;

/*左上角位于页面中心*/

left: 50%;

top: 50%;

/*让div向左偏移半个宽度、向上偏移半个高度,使div位于页面中心*/

margin-left: -200px;

margin-top: -150px;

/*弹窗在最上面*/

z-index: 9999;

}

/*遮罩样式*/

.mask{

position: fixed;

width: 100%;

height: 100%;

background-color: #000;

left: 0;

top: 0;

/*设置透明度30%*/

opacity: 0.3;

filter: alpha(opacity=30);/*兼容IE6、7、8*/

/*遮罩在弹窗的下面,在网页所有内容的上面*/

z-index: 9990;

}

.pop_con{

display: none;/*默认不显示,用定时器显示*/

}

</style>

<script type="text/javascript">

/*

setTimeout 只执行一次的定时器

clearTimeout 关闭只执行一次的定时器

setInterval 反复执行的定时器

clearInterval 关闭反复执行的定时器

*/

window.onload = function(){

var oPop = document.getElementById('pop');

var oShut = document.getElementById('shutOff');

/*setTimeout(showPop, 3000);//开启定时器,3秒后调用函数showPop()弹框

function showPop(){

oPop.style.display = 'block';//显示弹框和遮罩

}*/

//开启定时器的简写方式:调用匿名函数

setTimeout(function(){

oPop.style.display = 'block';

}, 3000);

oShut.onclick = function(){

oPop.style.display = 'none';//关闭弹框和遮罩

}

}

</script>

</head>

<body>

<h1>欢迎,我的小公主!</h1>

<p>

        *  *  *  *   

      *    * *    *   

      *  ★  *  ★  *   

      *      ★    *   

>>>------I love you!  ---->

        *        *     

          *  ★ *     

            *  *         

            * *         

              *  </p>

<a href="https://www.sohu.com/a/232195311_140886" title="小猪佩奇,我配你,高山流水,我留你,落叶归根,我归你,唐僧取经,我娶你,">mua</a>

<div class="pop_con" id="pop">

<div class="pop">

<h3>提示信息</h3>

<a href="#" id="shutOff">我也爱你</a>

</div>

<div class="mask"></div>

</div>

</body>

</html>






JS时钟:


<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>时钟</title>

<style type="text/css">

</style>

<script type="text/javascript">

window.onload = function(){

var oBox = document.getElementById('box');

function timeGo(){

var now = new Date();

// alert(now);//弹出美式时间:Thu Nov 01 2018 14:55:35 GMT+0800 (中国标准时间)

var year = now.getFullYear();//2018年

var month = now.getMonth() + 1;//6月弹出5//范围0-11

var date = now.getDate();//1号

var week = now.getDay();//4//星期几,西半球时间,范围0-6,星期日为一周的第一天,为0

var hour = now.getHours();

var minute = now.getMinutes();

var second = now.getSeconds();

// alert(hour + ":" + minute + ":" + second);//14:56:45

oBox.innerHTML = '当前时间是:' + year + '年' + toDouble(month) + '月' + toDouble(date) + '日 ' + toWeek(week) + ' ' + toDouble(hour) + ":" + toDouble(minute) + ":" + toDouble(second);

}

timeGo();

setInterval(timeGo, 1000);

}

//此函数将星期的数字转为汉字表示

function toWeek(num){

switch(num){

case 0:

return '星期天';

break;

case 1:

return '星期一';

break;

case 2:

return '星期二';

break;

case 3:

return '星期三';

break;

case 4:

return '星期四';

break;

case 5:

return '星期五';

break;

case 6:

return '星期六';

break;

}

}

//此函数将不足两位的数字前面补0

function toDouble(num){

if(num < 10){

return '0' + num;

}else{

return num;

}

}

</script>

</head>

<body>

<div id="box"></div>

</body>

</html>






JS倒计时:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>倒计时</title>

<script type="text/javascript">

window.onload = function(){

//活动第二天要将页面下线,直接跳转到其它页面,不会走后面的代码了

// window.location.href = "http://www.baidu.com";

var oDiv = document.getElementById('div1');

function timeLeft(){

//实际开发中此时间从服务器获取,避免客户端调整时间

var now = new Date();

var future = new Date(2018,10,10,24,00,00);//就是2018年11月11号

0

// alert(future - now);//弹出与当前时间相差的毫秒数:12469935436

var milli = parseInt((future - now)/1000);

//活动当天页面下线,避免倒计时到点后继续计负时

// if(milli <= 0){

// //页面跳转,不执行下面的代码了

// window.location.href = "http://www.baidu.com";

// }

var day = parseInt(milli / 86400);

var hour = parseInt(milli % 86400 / 3600);

var minute = parseInt(((milli % 86400) % 3600) / 60);

var second = milli % 60;

oDiv.innerHTML = '距离2018年11月11日00时00分00秒还有' + day + '天' + toDouble(hour) + '时' + toDouble(minute) + '分' + toDouble(second) + '秒';//距离2018年11月11日00时00分00秒还有10天08时48分00秒

}

timeLeft();

setInterval(timeLeft, 1000);

}

function toDouble(num){

if(num < 10){

return '0' + num;

}else{

return num;

}

}

</script>

</head>

<body>

<div id="div1"></div>

</body>

</html>



©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容