本文實(shí)例講述了JS實(shí)現(xiàn)跟隨鼠標(biāo)的鏈接文字提示框效果。分享給大家供大家參考。具體如下:
這里使用JavaScript與CSS實(shí)現(xiàn)鏈接提示效果,不會(huì)改變你原來的鏈接結(jié)構(gòu),使用鏈接原有的title標(biāo)簽來實(shí)現(xiàn),如果之前你使用有title標(biāo)簽,那你幾乎只需把JS代碼拷貝到你的網(wǎng)頁(yè)中即可。你會(huì)發(fā)現(xiàn),運(yùn)行本效果后,鼠標(biāo)在鏈接上移動(dòng)的話,文字提示框會(huì)跟隨鼠標(biāo)而移動(dòng)位置。
運(yùn)行效果如下圖所示:

具體代碼如下:
<!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><title>跟隨鼠標(biāo)的文字提示框</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><style type="text/css">body{font:12px/1.8 arial;}a,a:visited{color:#3366cc;text-decoration:none;}a:hover{color:#f60;text-decoration:underline;}.tip{width:200px;border:2px solid #ddd;padding:8px;background:#f1f1f1;color:#666;}img{border:none;}</style><script type="text/javascript"> var tip={$:function(ele){ if(typeof(ele)=="object") return ele; else if(typeof(ele)=="string"||typeof(ele)=="number") return document.getElementById(ele.toString()); return null; }, mousePos:function(e){ var x,y; var e = e||window.event; return{x:e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft,y:e.clientY+document.body.scrollTop+document.documentElement.scrollTop}; }, start:function(obj){ var self = this; var t = self.$("mjs:tip"); obj.onmousemove=function(e){ var mouse = self.mousePos(e); t.style.left = mouse.x + 10 + 'px'; t.style.top = mouse.y + 10 + 'px'; t.innerHTML = obj.getAttribute("tips"); t.style.display = ''; }; obj.onmouseout=function(){ t.style.display = 'none'; }; } }</script></head><body><ol><li><a href="#" target="_blank" onmouseover="tip.start(this)" tips="2013年12月14日,嫦娥3號(hào)衛(wèi)星登上月球,激動(dòng)人心的一切終于到來了……">中國(guó)嫦娥飛天的一些感想</a></li><li><a href="#" target="_blank" onmouseover="tip.start(this)" tips="中國(guó)未來一定是世界上最強(qiáng)大的國(guó)家,你相信么?">中國(guó)是世界上最強(qiáng)大的國(guó)家</a></li></ol><div id="mjs:tip" class="tip" style="position:absolute;left:0;top:0;display:none;"></div></body></html>希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
|
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注