定時(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>