今天寫一個小實例,用js和css寫一個可以自動隱藏的懸浮框。css肯定是用來控制樣式的,js用來控制器顯示與隱藏的。顯示與隱藏通常有兩種方法實現:1,用js控制其顯示屬性;2,用js控制其大小。
今天要說的就是通過控制其大小來實現元素的顯隱,原理:為其注冊鼠標移入、移出的事件,當鼠標移出對象范圍,將其寬度設為1,當鼠標再次移入該對象,將其寬度還原。很簡單,我們一起看看吧!
隱藏狀態:
復制代碼 代碼如下:
<style>
* { font-size:12px; font-family:Verdana,宋體; }
html, body { margin:0px; padding:0px; overflow:hidden; }
.b { margin:0px; padding:0px; overflow:auto; }
.line0 { line-height:20px; background-color:lightyellow; padding:0px 15px; }
.line1 { line-height:18px; background-color:lightblue; padding:0px 10px; }
.w { position:absolute; lift:10px; top:10px; width:1px; height:300px; overflow:hidden; border:2px groove #281; cursor:default; -moz-user-select:none; }
.t { line-height:20px; height:20px; width:160px; overflow:hidden; background-color:#27C; color:white; font-weight:bold; border-bottom:1px outset blue; text-align:center; }
.winBody { height:270px; width:160px; overflow-x:auto; overflow-y:auto; border-top:1px inset blue; padding:10px; background-color:white; }
</style>
復制代碼 代碼如下:
<script type="text/javascript">
function myshow(){
//document.getElementById('mydiv').style.display = "none";
document.getElementById('mydiv').style.width = "160px";
} //block
function myhide(){
//document.getElementById('mydiv').style.display = "block";
document.getElementById('mydiv').style.width="1px";
}
//測試用,隨機生成一些內容,便于測試效果。
for(var i=0; i<400; i++)document.write("<div class=/"line"+(i%2)+"/">"+(new Array(20)).join((Math.random()*1000000).toString(36)+" ")+"<//div>");
new function(w,b,c,d,o){
d=document;b=d.body;o=b.childNodes;c="className";
b.appendChild(w=d.createElement("div"))[c]= "b";
for(var i=0; i<o.length-1; i++)if(o[i][c]!="w")w.appendChild(o[i]),i--;
(window.onresize = function(){
w.style.width = d.documentElement.clientWidth + "px";
w.style.height = d.documentElement.clientHeight + "px";
})();
<span> </span>}
</script>
復制代碼 代碼如下:
<body >
<div onmousemove="myshow()">
<div>學生信息</div>
<div>
學號:<label>0123456789 </label><br><br>
姓名:<label>小明 </label><br><br>
學院:<label>軟件學院 </label><br><br>
專業:<label>軟件工程</label><br><br>
班級:<label>一班</label><br><br>
</div>
</div>
</body>
新聞熱點
疑難解答
圖片精選