Tips:点此可运行HTML源码

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>

 

 

posted @ 2013-06-17 22:19  Zjmainstay  阅读(10270)  评论(2编辑  收藏  举报