javascript 實現(xiàn)動態(tài)側(cè)邊欄實例詳解
更新時間:2016年11月11日 14:10:50 投稿:lqh
這篇文章主要介紹了javascript 實現(xiàn)動態(tài)側(cè)邊欄實例詳解的相關(guān)資料,并附實例代碼,幫助大家學(xué)習(xí)理解,需要的朋友可以參考下
javascript 實現(xiàn)動態(tài)側(cè)邊欄
總的來說就是利用 鼠標(biāo)懸停onmouseover 和 鼠標(biāo)移除onmouseout 這兩個時間來完成的。
首先是HTML 結(jié)構(gòu)
<body> <div id="div1"> <span>側(cè)邊欄</span> </div> </body>
然后是css的樣式:
#div1{ width:150px; height:200px; background:#999999; position:absolute; left:-150px;} span{ width:20px; height:70px; line-height:23px; background:#09C; position:absolute; right:-20px; top:70px;}
默認(rèn)的樣式 側(cè)邊欄是隱藏起來的如圖:
當(dāng)鼠標(biāo)移入以后如圖:
下面是完整代碼:
<!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>無標(biāo)題文檔</title> <style type="text/css"> #div1{ width:150px; height:200px; background:#999999; position:absolute; left:-150px;} span{ width:20px; height:70px; line-height:23px; background:#09C; position:absolute; right:-20px; top:70px;} </style> <script> window.onload=function(){ var odiv=document.getElementById('div1'); odiv.onmouseover=function () { startmove(0,10);//第一個參數(shù)為div left屬性的目標(biāo)值 第二個為 每次移動多少像素 } odiv.onmouseout=function () { startmove(-150,-10); } } var timer=null; function startmove(target,speed) { var odiv=document.getElementById('div1'); clearInterval(timer); timer=setInterval(function (){ if(odiv.offsetLeft==target) { clearInterval(timer); } else { odiv.style.left=odiv.offsetLeft+speed+'px'; } },30) } </script> </head> <body> <div id="div1"> <span>側(cè)邊欄</span> </div> </body> </html>
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:
- 博客側(cè)邊欄模塊跟隨滾動條滑動固定效果的實現(xiàn)方法(js+jquery等)
- javascript實現(xiàn)動態(tài)側(cè)邊欄代碼
- JavaScript實現(xiàn)簡單的隱藏式側(cè)邊欄功能示例
- 利用js編寫響應(yīng)式側(cè)邊欄
- JS實現(xiàn)側(cè)邊欄鼠標(biāo)經(jīng)過彈出框+緩沖效果
- 基于slideout.js實現(xiàn)移動端側(cè)邊欄滑動特效
- JS運動框架之分享側(cè)邊欄動畫實例
- JavaScript中實現(xiàn)無縫滾動、分享到側(cè)邊欄實例代碼
- JS實現(xiàn)京東商品分類側(cè)邊欄
- js實現(xiàn)淘寶固定側(cè)邊欄
相關(guān)文章
javascript實現(xiàn)修改微信分享的標(biāo)題內(nèi)容等
這篇文章主要介紹了javascript實現(xiàn)修改微信分享的標(biāo)題內(nèi)容等,需要的朋友可以參考下2014-12-12淺析Javascript中雙等號(==)隱性轉(zhuǎn)換機(jī)制
這篇文章給大家詳細(xì)介紹了javascript中雙等號(==)隱性轉(zhuǎn)換機(jī)制,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-10-10