美文网首页
JS定时器,时钟,倒计时

JS定时器,时钟,倒计时

作者: 啊烟雨 | 来源:发表于2018-11-01 18:57 被阅读0次

<!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>

相关文章

  • 定时器

    定时器弹窗 定时器基本用法 定时器动画 时钟 倒计时 变量的作用域

  • js定时器

    定时器弹窗 定时器的基本用法 定时器动画 时钟 倒计时 变量的作用域

  • JS定时器,时钟,倒计时

    定时器弹框 .pop{ width: 400px; height: 300px; ...

  • JS学习笔记105-113

    1.定时器。 2.设置和清除定时器。 3.倒计时鲜花表白。 4.自定义现在的时间 5.放假倒计时。 6.时钟案例。...

  • stm32学习记录

    定时器相关 定时器的时钟来源这里,定时器的时钟来源有 4 个:1) 内部时钟(CK_INT)2) 外部时钟模式 1...

  • 2018-12-08

    定时器的基本用法 定时器动画 时钟 倒计时 变量的作用域 封闭函数 用变量的方式定义函数 闭包 闭包存循环的索引值

  • 定时器弹框、定时器基本用法、定时器动画、时钟

    定时器弹框: 定时器基本用法: 定时器动画: 时钟:

  • Linux C/C++定时器的实现原理和使用方法

    定时器的实现原理 定时器的实现依赖的是CPU时钟中断,时钟中断的精度就决定定时器精度的极限。一个时钟中断源如何实现...

  • 2018-07-13

    Date 日期对象的API 时间定时器 现在时间实例 开始、停止倒计时实例 HTML代码 js代码

  • 12.18

    SYSTick定时器(滴答时钟):Timer定时器;时基分三大模块:时钟源(最小单位)、重载计数、计数增/减(起始...

网友评论

      本文标题:JS定时器,时钟,倒计时

      本文链接:https://www.haomeiwen.com/subject/xktfxqtx.html