js定時(shí)器

定時(shí)器彈框:

.pop{

width: 400px;

height: 300px;

background-color: #fff;

border: 1px solid #000;

/*固定定位*/

position: fixed;

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

left: 50%;

top: 50%;

/*讓div向左偏移半個(gè)寬度、向上偏移半個(gè)高度,使div位于頁面中心*/

margin-left: -200px;

margin-top: -150px;

/*彈窗在最上面*/

z-index: 9999;

}

/*遮罩樣式*/

.mask{

position: fixed;

width: 100%;

height: 100%;

background-color: #000;

left: 0;

top: 0;

/*設(shè)置透明度30%*/

opacity: 0.3;

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

/*遮罩在彈窗的下面,在網(wǎng)頁所有內(nèi)容的上面*/

z-index: 9990;

}

.pop_con{

display: none;/*默認(rèn)不顯示,用定時(shí)器顯示*/

}

</style>

<script type="text/javascript">

/*

setTimeout 只執(zhí)行一次的定時(shí)器

clearTimeout 關(guān)閉只執(zhí)行一次的定時(shí)器

setInterval 反復(fù)執(zhí)行的定時(shí)器

clearInterval 關(guān)閉反復(fù)執(zhí)行的定時(shí)器

*/

window.onload = function(){

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

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

/*setTimeout(showPop, 3000);//開啟定時(shí)器,3秒后調(diào)用函數(shù)showPop()彈框

function showPop(){

oPop.style.display = 'block';//顯示彈框和遮罩

}*/

//開啟定時(shí)器的簡(jiǎn)寫方式:調(diào)用匿名函數(shù)

setTimeout(function(){

oPop.style.display = 'block';

}, 3000);

oShut.onclick = function(){

oPop.style.display = 'none';//關(guān)閉彈框和遮罩

}

}

</script>

</head>

<body>

<h1>首頁標(biāo)題</h1>

<p>頁面內(nèi)容</p>

<a >百度網(wǎng)</a>

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

<div class="pop">

<h3>提示信息!</h3>

<a href="#" id="shutOff">關(guān)閉</a>

</div>

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

</div>

</body>

定時(shí)器的基本用法:

<head>

<meta charset="UTF-8">

<title>定時(shí)器的基本用法</title>

<script type="text/javascript">

//單次定時(shí)器

var timer = setTimeout(function(){

alert('hello!');

}, 3000);

//清除單次定時(shí)器

clearTimeout(timer);

//反復(fù)循環(huán)定時(shí)器

var timer2 = setInterval(function(){

alert('hi~~~');

}, 2000);

//清除反復(fù)循環(huán)定時(shí)器

clearInterval(timer2);

</script>

</head>

定時(shí)器動(dòng)畫:

<style type="text/css">

.box{

width: 100px;

height: 100px;

background-color: gold;

position: fixed;

left: 20px;

top: 20px;

}

</style>

<script type="text/javascript">

window.onload = function(){

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

var left = 20;

//反復(fù)循環(huán)定時(shí)器,每30毫秒修改一次盒子的left值

var timer = setInterval(function(){

left += 2;

oBox.style.left = left + 'px';

//當(dāng)left值大于700時(shí)停止動(dòng)畫(清除定時(shí)器)

if(left > 700){

clearInterval(timer);

}

},30);

}

</script>

</head>

<body>

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • <!DOCTYPE html> 定時(shí)器彈框 .pop{ width: 400px; height: 300px; ...
    啊煙雨閱讀 1,827評(píng)論 0 0
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些閱讀 2,132評(píng)論 0 2
  • 前言:在引用開發(fā)中,我們經(jīng)常需要在頁面中執(zhí)行一些周期性的操作,比如每隔一段時(shí)間就執(zhí)行某一固定的操作。而對(duì)于這樣的操...
    帥帥噠小白閱讀 5,417評(píng)論 1 3
  • 定時(shí)器彈框 setTimeout 只執(zhí)行一次的定時(shí)器clearTimeout 關(guān)閉只執(zhí)行一次的定時(shí)器set...
    我本無常閱讀 222評(píng)論 0 0
  • 使用定時(shí)器實(shí)現(xiàn)倒計(jì)時(shí) setInterval()和setTimeout() 區(qū)別原文地址 setTimeout()...
    浪客行1213閱讀 2,444評(píng)論 0 0

友情鏈接更多精彩內(nèi)容