JS實現TITLE懸停長久顯示效果


canrun

  1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2 <html>
  3 <head>
  4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5 <title>JS控制TITLE懸停效果</title>
  6 <script type="text/javascript">
  7 /**
  8  * className 類名
  9  * tagname HTML標簽名,如div,td,ul等
 10  * @return Array 所有class對應標簽對象組成的數組
 11  * @example
 12  <div class="abc">abc</div>
 13  var abc = getClass('abc');
 14  for(i=0;i<abc.length;i++){
 15      abc[i].style.backgroundColor='red';
 16  }
 17 */
 18 function getClass(className,tagname) {
 19     //tagname默認值為'*',不能直接寫成默認參數方式getClass(className,tagname='*'),否則IE下報錯
 20     if(typeof tagname == 'undefined') tagname = '*';
 21     if(typeof(getElementsByClassName) == 'function') {
 22         return getElementsByClassName(className);
 23     }else {
 24         var tagname = document.getElementsByTagName(tagname);
 25         var tagnameAll = [];
 26         for(var i = 0; i < tagname.length; i++) {
 27             if(tagname[i].className == className) {
 28                 tagnameAll[tagnameAll.length] = tagname[i];
 29             }
 30         }
 31         return tagnameAll;
 32     }
 33 }
 34 
 35 /**
 36  * JS字符切割函數
 37  * @params     string                原字符串
 38  * @params    words_per_line        每行顯示的字符數
 39  */
 40 function split_str(string,words_per_line) {
 41     //空串,直接返回
 42     if(typeof string == 'undefined' || string.length == 0) return '';
 43     //單行字數未設定,非數值,則取默認值50
 44     if(typeof words_per_line == 'undefined' || isNaN(words_per_line)){
 45         words_per_line = 50;
 46     }
 47     //格式化成整形值
 48     words_per_line = parseInt(words_per_line);
 49     //取出i=0時的字,避免for循環里換行時多次判斷i是否為0
 50     var output_string = string.substring(0,1);
 51     //循環分隔字符串
 52     for(var i=1;i<string.length;i++) {
 53         //如果當前字符是每行顯示的字符數的倍數,輸出換行
 54         if(i%words_per_line == 0) {
 55             output_string += "<br/>";
 56         }
 57         //每次拼入一個字符
 58         output_string += string.substring(i,i+1);
 59     }
 60     return output_string;
 61 }
 62 
 63 /**
 64  * 鼠標懸停顯示TITLE
 65  * @params     obj        當前懸停的標簽
 66  *
 67  */
 68 function titleMouseOver(obj,event,words_per_line) {
 69     //無TITLE懸停,直接返回
 70     if(typeof obj.title == 'undefined' || obj.title == '') return false;
 71     //不存在title_show標簽則自動新建
 72     var title_show = document.getElementById("title_show");
 73     if(title_show == null){
 74         title_show = document.createElement("div");                            //新建Element
 75         document.getElementsByTagName('body')[0].appendChild(title_show);    //加入body中
 76         var attr_id = document.createAttribute('id');                        //新建Element的id屬性
 77         attr_id.nodeValue = 'title_show';                                    //為id屬性賦值
 78         title_show.setAttributeNode(attr_id);                                //為Element設置id屬性
 79         
 80         var attr_style = document.createAttribute('style');                    //新建Element的style屬性
 81         attr_style.nodeValue = 'position:absolute;'                            //絕對定位
 82             +'border:solid 1px #999999; background:#EDEEF0;'                //邊框、背景顏色
 83             +'border-radius:2px;box-shadow:2px 3px #999999;'                //圓角、陰影
 84             +'line-height:18px;'                                            //行間距
 85             +'font-size:12px; padding: 2px 5px;';                            //字體大小、內間距
 86         try{
 87             title_show.setAttributeNode(attr_style);                        //為Element設置style屬性
 88         }catch(e){
 89             //IE6
 90             title_show.style.position = 'absolute';
 91             title_show.style.border = 'solid 1px #999999';
 92             title_show.style.background = '#EDEEF0';
 93             title_show.style.lineHeight = '18px';
 94             title_show.style.fontSize = '18px';
 95             title_show.style.padding = '2px 5px';
 96         }
 97     }
 98     //存儲並刪除原TITLE
 99     document.title_value = obj.title;
100     obj.title = '';
101     //單行字數未設定,非數值,則取默認值50
102     if(typeof words_per_line == 'undefined' || isNaN(words_per_line)){
103         words_per_line = 50;
104     }
105     //格式化成整形值
106     words_per_line = parseInt(words_per_line);
107     //在title_show中按每行限定字數顯示標題內容,模擬TITLE懸停效果
108     title_show.innerHTML = split_str(document.title_value,words_per_line);
109     //顯示懸停效果DIV
110     title_show.style.display = 'block';
111     
112     //根據鼠標位置設定懸停效果DIV位置
113     event = event || window.event;                            //鼠標、鍵盤事件
114     var top_down = 15;                                        //下移15px避免遮蓋當前標簽
115     //最左值為當前鼠標位置 與 body寬度減去懸停效果DIV寬度的最小值,否則將右端導致遮蓋
116     var left = Math.min(event.clientX,document.body.clientWidth-title_show.clientWidth);
117     title_show.style.left = left+"px";            //設置title_show在頁面中的X軸位置。
118     title_show.style.top = (event.clientY + top_down)+"px";    //設置title_show在頁面中的Y軸位置。
119 }
120 /**
121  * 鼠標離開隱藏TITLE
122  * @params    obj        當前懸停的標簽
123  *
124  */
125 function titleMouseOut(obj) {
126     var title_show = document.getElementById("title_show");
127     //不存在懸停效果,直接返回
128     if(title_show == null) return false;    
129     //存在懸停效果,恢復原TITLE
130     obj.title = document.title_value;
131     //隱藏懸停效果DIV
132     title_show.style.display = "none";
133 }
134 
135 /**
136  * 懸停事件綁定
137  * @params    objs        所有需要綁定事件的Element
138  *
139  */
140 function attachEvent(objs,words_per_line){
141     if(typeof objs != 'object') return false;
142     //單行字數未設定,非數值,則取默認值50
143     if(typeof words_per_line == 'undefined' || isNaN(words_per_line)){
144         words_per_line = 50;
145     }
146     for(i=0;i<objs.length;i++){
147         objs[i].onmouseover = function(event){
148             titleMouseOver(this,event,words_per_line);
149         }
150         objs[i].onmouseout = function(event){
151             titleMouseOut(this);
152         }
153     }
154 }
155 
156 
157 //初始化,當頁面onload的時候,對所有class="title_hover"的標簽綁定TITLE懸停事件
158 window.onload = function(){
159     attachEvent(getClass('title_hover'),18);    //行字數設定為18
160 }
161 </script>
162 </head>
163 <body>
164 <style>
165 tr{float:left; margin:0 50px;}
166 </style>
167 <table>
168     <tr>
169         <td title="這個是默認的TITLE這個是默認的TITLE這個是默認的TITLE這個是默認的TITLE這個是默認的TITLE這個是默認的TITLE">鼠標懸停[原生版本]</td>
170     </tr>
171     <tr>
172         <td title="這個是JS實現懸停的TITLE這個是JS實現懸停的TITLE這個是JS實現懸停的TITLE這個是JS實現懸停的TITLE這個是JS實現懸停的TITLE" 
173         onmouseover="titleMouseOver(this,event,15);" onmouseout="titleMouseOut(this);">鼠標懸停[直接調用函數版本,設定行字數]</td>
174     </tr>
175     <tr>
176         <td class="title_hover" title="ABCTesterABCTesterABCTesterABCTesterABCTesterABCTesterABCTester">鼠標懸停[class控制版本]</td>
177     </tr>
178     <tr>
179         <td title="這個是JS實現懸停的TITLE這個是JS實現懸停的TITLE這個是JS實現懸停的TITLE這個是JS實現懸停的TITLE這個是JS實現懸停的TITLE" 
180         onmouseover="titleMouseOver(this,event);" onmouseout="titleMouseOut(this);">鼠標懸停[直接調用函數版本,默認行字數]</td>
181     </tr>    
182 </table>
183 </body>
184 </html>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM