js寫出遮罩層登陸框和對聯廣告並自動跟隨捲軸滾動

來源:互聯網
上載者:User

用JS寫出遮罩層登陸框和對聯廣告並自動跟隨捲軸滾動保持讓使用者一直可以看到

好了,天色已晚廢話不多說,代碼特別詳細 有注釋,請看代碼。
複製代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>越獄的囚徒</title>
<style type="text/css">
#zhezhao{ /*遮罩層 先隱藏起來 後面我會通過JS修改display: 為block 讓他顯示出來*/
position:absolute;
z-index:1000;
background:#ccc;
width:100%;
height:100%;
opacity: 0.8;
filter:alpha(opacity=80);
display:none;
}
#denglu{ /*登陸層 先隱藏起來 後面我會通過JS修改display: 為block 讓他顯示出來*/
position:absolute;
z-index:2000;
width:100%;
height:200px;
margin:150px auto;
display:none;
}
#denglu table{ /*置中顯示*/
margin:auto;
}
.content{
width:800px;
margin:0 auto;
}
#zuo{ /*這個是兩邊浮動的廣告對聯,一(左)一右*/
position:absolute;
left:3px;
top:100px;
border:5px solid purple;
background:#ccc;
color:purple;
font-size:50px;
text-align:center;
height:200px;
width:85px;
z-index:2000;
}
#you{ /*這個是兩邊浮動的廣告對聯,一左一(右)*/
position:absolute;
right:3px;
top:100px;
border:5px solid purple;
background:#ccc;
color:purple;
font-size:50px;
text-align:center;
height:200px;
width:85px;
z-index:2000;
}
</style>
<script type="text/javascript">
var h=0;
function fun1(){ //這裡讓登陸框 和 廣告框 隨著捲軸也自動滾動 保持一直可以讓使用者看到
h=document.body.clientHeight;
document.getElementById('zhezhao').style.height=h+"px";
window.onscroll=function(){
//改變登入框的位置
//擷取捲軸離開上方的距離
var t=document.body.scrollTop+document.documentElement.scrollTop;//相容Google和Firefox瀏覽器
//把這個值,交給denglu這div的margin-top
document.getElementById('denglu').style.marginTop=150+t+"px";

document.getElementById('zuo').style.top=100+t+"px";
document.getElementById('you').style.top=100+t+"px";
}
}
function show(){ //啟用他們
//遮罩層
document.getElementById('zhezhao').style.display='block';
//登入層
document.getElementById('denglu').style.display='block';
}
function hide(){ //隱藏他們
//遮罩層
document.getElementById('zhezhao').style.display='none';
//登入層
document.getElementById('denglu').style.display='none';
}

</script>
</head>
<body style="margin:0;" onload="fun1();">
<div id="zuo">美<br/>女</div>
<div id="you">帥<br/>哥</div>
<div id="zhezhao"></div>
<div id="denglu">
<div>
<table bgcolor="#ccc" width="350">
<tr>
<td>登入</td><td><a href="javascript:hide();">關閉</a></td>
</tr>
<tr>
<td>帳號</td>
<td><input type="text"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登入"/></td>
</tr>
</table>
<a id="a-1" href="javascript:void();">test</a>
</div>
</div>
<a href="javascript:show();">登入</a>
<div class="content" style="background:red;">
<br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/>
</div>
<div class="content" style="background:green;">
<br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/>
</div>
<div class="content" style="background:blue;">
<br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/>
</div>
<br/>
<br/>
</body>
</html>

可以自己試一試 多完善下,這個還是比較好玩的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.