基于JavaScript實(shí)現(xiàn)鼠標(biāo)懸浮彈出跟隨鼠標(biāo)移動的帶箭頭的信息層
很多網(wǎng)站,當(dāng)鼠標(biāo)懸浮在一個(gè)元素上的時(shí)候能夠彈出一個(gè)信息說明層,并且此層能夠跟隨鼠標(biāo)移動,同時(shí)彈出的層帶有箭頭,此箭頭指向鼠標(biāo)懸浮的元素,下面就通過實(shí)例代碼簡單介紹一下如何實(shí)現(xiàn)此效果。
代碼實(shí)例如下:
<!DOCTYPE html> <html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="author" content="http://chabaoo.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>
以上代碼實(shí)現(xiàn)了我們的要求,當(dāng)鼠標(biāo)放在div中的時(shí)候能夠彈出一個(gè)信息層,并且能夠跟隨鼠標(biāo)移動,彈出層的帶有指示的箭頭,代碼非常的簡單這里就不多介紹了,如有任何疑問可以跟帖留言或者參閱相關(guān)閱讀。
相關(guān)文章
JS循環(huán)中正確使用async、await的姿勢分享
async?/?await是ES7的重要特性之一,也是目前社區(qū)里公認(rèn)的優(yōu)秀異步解決方案,下面這篇文章主要給大家介紹了關(guān)于JS循環(huán)中正確使用async、await的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12javascript制作坦克大戰(zhàn)全紀(jì)錄(1)
本文寫作的目的是鞏固一下自己最近學(xué)習(xí)的js知識, 這個(gè)教程適合熟悉js基本語法和面向?qū)ο笳Z法的小伙伴學(xué)習(xí)。由于自己也是剛學(xué)js不久,所以難免出現(xiàn)錯(cuò)誤。如果發(fā)現(xiàn)希望給予指正。2014-11-11JS實(shí)現(xiàn)點(diǎn)擊文本框改變背景顏色
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)點(diǎn)擊文本框改變背景顏色,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08JS 循環(huán)li添加點(diǎn)擊事件 (閉包的應(yīng)用)
這篇文章主要介紹了js循環(huán)li添加點(diǎn)擊事件 (閉包的應(yīng)用)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12