純JS實現(xiàn)的超漂亮Tips文字提示框,可關(guān)閉
當(dāng)前位置:點晴教程→知識管理交流
→『 技術(shù)文檔交流 』
<!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>Tips文字提示</title> </head> <body> <style type="text/css"> body{ } .lj-tipsWrap,.lj-in,.lj-close{display:inline-block;} .lj-tipsWrap{background:#F4FBFF;line-height:1.5em;padding:5px 15px;border:1px solid #2192D3;position:absolute;text-align:left;} .lj-in{position:absolute;zoom:1;border:10px dashed transparent;width:0px;height:0px;} .lj-in span{zoom:1;width:0px;height:0px;overflow:hidden;} .lj-close{position:absolute; text-decoration:none;color:#000; height:9px; width:9px; overflow:hidden;line-height:0.5em; right:0; top:0;} .lj-close:hover{color:#39F;} .lj-top{border-top:10px solid #2192D3;bottom:-20px; left:3px;} .lj-top span{border-top:10px solid #F4FBFF;margin:-11px 0 0 -10px;} .lj-right{border-right:10px solid #2192D3;left:-20px; top:3px;} .lj-right span{border-right:10px solid #F4FBFF;margin:-10px 0 0 -9px;} .lj-bottom{border-bottom:10px solid #2192D3;top:-20px; left:3px;} .lj-bottom span{border-bottom:10px solid #F4FBFF;margin:-9px 0 0 -10px;} .lj-left{border-left:10px solid #2192D3;right:-20px; top:3px; } .lj-left span{border-left:10px solid #F4FBFF;margin:-10px 0 0 -11px;} .lj-left ~ .lj-close{left:0;} /*css 3漸強*/ .lj-tipsWrap{-webkit-border-radius:3px;-moz-border-radius:3px;-webkit-box-shadow:2px 2px 3px #eee;-moz-box-shadow:2px 2px 3px #eee;} </style> <script type="text/javascript"> window.ljtips=function(){var i=document.body,j=document.documentElement,n=/msie\s([\d\.]+)/.test(navigator.userAgent.toLowerCase()),k=window;return function(l){var c=l?typeof l=="string"?document.getElementById(l):l:this,d=null,s=c.offsetWidth,t=c.offsetHeight,m=0,g=null,e={},o={left:function(a){return{top:d.top,left:d.left-a-15}},top:function(a,h){return{top:d.top-h-15,left:d.left}},right:function(){return{top:d.top,left:d.left+s+15}},bottom:function(){return{top:d.top+t+15,left:d.left}}};e.show= function(a){if(c.lock)c.lock=false;else{c.lock=true;d=c.getBoundingClientRect();var h=i.scrollTop+j.scrollTop,f=i.scrollLeft+j.scrollLeft;a.p=a.p||"right";var b=e.append(a.p,a.closeBtn||false);b.getElementsByTagName("DIV")[0].innerHTML=a.content;var p=o[a.p](b.offsetWidth,b.offsetHeight);b.style.top=p.top+h+"px";b.style.left=p.left+f+"px";a.time&&setTimeout(function(){e.clear(document.getElementById("lj"+m))},a.time);a.fn&&a.fn.call(c,document.getElementById("lj"+m));g=function(w,q){return function(){var u= i.scrollTop+j.scrollTop,v=i.scrollLeft+j.scrollLeft;d=c.getBoundingClientRect();var r=o[a.p](b.offsetWidth,b.offsetHeight);q.style.top=r.top+u+"px";q.style.left=r.left+v+"px"}}(c,b);n?k.attachEvent("onresize",g):k.addEventListener("resize",g,false)}};e.append=function(a,h){var f=m=Math.floor(Math.random()*1E4),b=document.createElement("DIV");b.id="lj"+f;b.innerHTML=''<div class="lj-tipsWrap" id="tipsWrap-<%=r%>"><div></div><span class="lj-in lj-<%=p%>"><span class="lj-in"></span></span><a href="javascript:void(0)" id="ljClose<%=r%>" class="lj-close">x</a></div>''.replace("<%=p%>", a).replace(/<%=r%>/g,f);document.body.appendChild(b);if(h)document.getElementById("ljClose"+f).onclick=e.hide;else document.getElementById("ljClose"+f).style.display="none";return document.getElementById("tipsWrap-"+f)};e.clear=function(a){a&&a.parentNode&&a.parentNode.removeChild(a);n?k.detachEvent("onresize",g):k.removeEventListener("resize",g,false);c.lock=false};e.hide=function(){e.clear(document.getElementById("lj"+m))};return e}}(); </script> <h2>鼠標(biāo)滑過或點擊下面帶方框的文字可看到效果:</h2> <span style="display:inline-block; padding:5px; border:1px solid #CCC; margin:30px 30px 30px 100px;" id="test1">看我這里一個DIV</span><br/> <input style="display:inline-block; padding:5px; border:1px solid #CCC; margin:30px 30px 30px 100px;" value="點擊我" id="test2"><br/> <span style="display:inline-block; padding:5px; border:1px solid #CCC; margin:30px 30px 30px 100px;" id="test3">提示在我的下面出現(xiàn)</span><br/> <span style="display:inline-block; padding:5px; border:1px solid #CCC; margin:30px 30px 30px 100px;" id="test4">提示在左邊出現(xiàn)</span><br/> <span style="display:inline-block; padding:5px; border:1px solid #CCC;margin:30px 30px 30px 100px;" id="test5">提示出現(xiàn)2秒后會自動消失</span><br/> <span style="display:inline-block; padding:5px; border:1px solid #CCC; margin:30px 30px 30px 100px;" id="test6">提示可以有關(guān)閉按鈕</span><br/> <div style="text-align:center;"> <span style="display:inline-block; padding:5px; border:1px solid #CCC; margin:30px 30px 30px 100px;" id="test7">不同擔(dān)心頁面放大縮小會位置會偏移哦</span><br/> </div> <h2>優(yōu)點</h2> <ul> <li>1:兼容 各種瀏覽器</li> <li>2:自己定義TIPS樣式(顏色),CSS3漸進增強</li> <li>3:多方向彈出提示</li> <li>4:純CSS+javascript(不包含任何圖片)</li> <li>5:體積超級小 CSS+js = 3k</li> </ul> <h2>參數(shù)配置</h2> <ul> <li>ljtips(object).show(JSON)</li> <li>object: DOM對象ID(test1), DOM對象</li> <li><pre> json:{ content:提示的消息內(nèi)容(必須), p:提示框位置(可選值top left bottom right),默認為right(可選), closeBtn:是否有關(guān)閉按鈕(true false) 默認值(false)(可選), time:多少毫秒提示框消失(可選) } </pre></li> </ul> <h3>使用說明</h3> <ul> <li>主要方法 .show(); .hide();.clear(); 一般你只需要使用show,hide;</li> <li>使用1</li> <li>首先實例化一個ljtips(var tips=ljtips(''test1'')); 然后使用tips.show(JSON)</li> <li>使用2</li> <li>在行內(nèi)調(diào)用提示框“<div onclick=" ljtips(this).show(JSON); "></div>”</li> <li>使用3</li> <li>在JS里調(diào)用document.getElementById("test6").onmouseover = function(){ljtips(this).show({content:"這個框你關(guān)不掉了吧!沒有關(guān)閉按鈕!",p:''right''})};</li> </ul> <script type="text/javascript"> var test1 = ljtips(''test1''); document.getElementById("test1").onmouseover = function(){test1.show({content:"鼠標(biāo)劃過時候的TIPS...<br/>兼容主流瀏覽器 IE 6 7 8 chorme Firefox Opera Safari"})}; document.getElementById("test1").onmouseout = function(){test1.hide();}; document.getElementById("test2").onfocus = function(){ljtips(this).show({content:"這個提示會在<br/>input的上方!有個關(guān)閉按鈕哦",p:''top'',closeBtn:true})}; document.getElementById("test3").onmouseover = function(){ljtips(this).show({content:"我在下面出現(xiàn)了!不要認為會用到圖片哦!",p:''bottom'',closeBtn:true})}; document.getElementById("test4").onmouseover = function(){ljtips(this).show({content:"我可以在4個方向上",p:''left'',closeBtn:true})}; document.getElementById("test5").onmouseover = function(){ljtips(this).show({content:"提示出現(xiàn)2秒后會自動消失",p:''bottom'',time:2000})}; document.getElementById("test6").onmouseover = function(){ljtips(this).show({content:"這個框你關(guān)不掉了吧!沒有關(guān)閉按鈕!",p:''right''})}; document.getElementById("test7").onmouseover = function(){ljtips(this).show({content:"您可以放大或者縮小瀏覽器窗口!<br/>他會跟隨這個絕對位置,不會有絲毫偏差。",p:''right'',closeBtn:true})}; </script> </body> </html>
該文章在 2021/2/2 17:11:47 編輯過
|
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |