基于JavaScript實現(xiàn)鼠標懸浮彈出跟隨鼠標移動的帶箭頭的信息層
更新時間:2016年01月18日 09:06:39 投稿:mrr
這篇文章主要介紹了基于JavaScript實現(xiàn)鼠標懸浮彈出跟隨鼠標移動的帶箭頭的信息層 的相關資料,需要的朋友可以參考下
很多網(wǎng)站,當鼠標懸浮在一個元素上的時候能夠彈出一個信息說明層,并且此層能夠跟隨鼠標移動,同時彈出的層帶有箭頭,此箭頭指向鼠標懸浮的元素,下面就通過實例代碼簡單介紹一下如何實現(xiàn)此效果。
代碼實例如下:
<!DOCTYPE html> <html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="author" content="http://www.dbjr.com.cn/" /> <title>腳本之家</title> <style type="text/css"> #content { width:100px; height:100px; background:green; position:relative; margin:100px; } #inform { width:200px; height:200px; border:1px solid #ccc; background:white; display:none; position:absolute; } #inform span { width:0px; height:0px; border-width:10px; border-style:none solid solid none; position:absolute; } #inform .tb-border { left:-10px; border-color:transparent #ccc transparent transparent; top:-1px; } #inform .tb-background { left:-9px; border-color:transparent white transparent transparent; } </style> <script type="text/javascript"> window.onload=function() { var content=document.getElementById("content"); var inform=document.getElementById("inform"); content.onmouseover=function(ev) { var ev=ev||event; inform.style.display="block"; inform.style.left=(ev.clientX-this.offsetLeft+20)+"px"; inform.style.top=(ev.clientY-this.offsetTop-20)+"px"; } content.onmousemove=function(ev) { var ev=ev||event; inform.style.left=(ev.clientX-this.offsetLeft+20)+"px"; inform.style.top=(ev.clientY-this.offsetTop-10)+"px"; } content.onmouseout=function(ev){inform.style.display="none";} } </script> </head> <body> <div id="content"> <div id="inform"> <span class="tb-border"></span> <span class="tb-background"></span> </div> </div> </body> </html>
以上代碼實現(xiàn)了我們的要求,當鼠標放在div中的時候能夠彈出一個信息層,并且能夠跟隨鼠標移動,彈出層的帶有指示的箭頭,代碼非常的簡單這里就不多介紹了,如有任何疑問可以跟帖留言或者參閱相關閱讀。
相關文章
JS循環(huán)中正確使用async、await的姿勢分享
async?/?await是ES7的重要特性之一,也是目前社區(qū)里公認的優(yōu)秀異步解決方案,下面這篇文章主要給大家介紹了關于JS循環(huán)中正確使用async、await的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2021-12-12