本文实例讲述了JS实现TITLE悬停长久显示效果。分享给大家供大家参考,具体如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JS控制TITLE悬停效果</title> <script type="text/javascript"> /** * className 类名 * tagname HTML标签名,如div,td,ul等 * @return Array 所有class对应标签对象组成的数组 * @example <div class="abc">abc</div> var abc = getClass('abc'); for(i=0;i<abc.length;i++){ abc[i].style.backgroundColor='red'; } */ function getClass(className,tagname) { //tagname默认值为'*',不能直接写成默认参数方式getClass(className,tagname='*'),否则IE下报错 if(typeof tagname == 'undefined') tagname = '*'; if(typeof(getElementsByClassName) == 'function') { return getElementsByClassName(className); }else { var tagname = document.getElementsByTagName(tagname); var tagnameAll = []; for(var i = 0; i < tagname.length; i++) { if(tagname[i].className == className) { tagnameAll[tagnameAll.length] = tagname[i]; } } return tagnameAll; } } /** * JS字符切割函数 * @params string 原字符串 * @params words_per_line 每行显示的字符数 */ function split_str(string,words_per_line) { //空串,直接返回 if(typeof string == 'undefined' || string.length == 0) return ''; //单行字数未设定,非数值,则取默认值50 if(typeof words_per_line == 'undefined' || isNaN(words_per_line)){ words_per_line = 50; } //格式化成整形值 words_per_line = parseInt(words_per_line); //取出i=0时的字,避免for循环里换行时多次判断i是否为0 var output_string = string.substring(0,1); //循环分隔字符串 for(var i=1;i<string.length;i++) { //如果当前字符是每行显示的字符数的倍数,输出换行 if(i%words_per_line == 0) { output_string += "<br/>"; } //每次拼入一个字符 output_string += string.substring(i,i+1); } return output_string; } /** * 鼠标悬停显示TITLE * @params obj 当前悬停的标签 * */ function titleMouseOver(obj,event,words_per_line) { //无TITLE悬停,直接返回 if(typeof obj.title == 'undefined' || obj.title == '') return false; //不存在title_show标签则自动新建 var title_show = document.getElementById("title_show"); if(title_show == null){ title_show = document.createElement("div"); //新建Element document.getElementsByTagName('body')[0].appendChild(title_show); //加入body中 var attr_id = document.createAttribute('id'); //新建Element的id属性 attr_id.nodeValue = 'title_show'; //为id属性赋值 title_show.setAttributeNode(attr_id); //为Element设置id属性 var attr_style = document.createAttribute('style'); //新建Element的style属性 attr_style.nodeValue = 'position:absolute;' //绝对定位 +'border:solid 1px #999999; background:#EDEEF0;' //边框、背景颜色 +'border-radius:2px;box-shadow:2px 3px #999999;' //圆角、阴影 +'line-height:18px;' //行间距 +'font-size:12px; padding: 2px 5px;'; //字体大小、内间距 try{ title_show.setAttributeNode(attr_style); //为Element设置style属性 }catch(e){ //IE6 title_show.style.position = 'absolute'; title_show.style.border = 'solid 1px #999999'; title_show.style.background = '#EDEEF0'; title_show.style.lineHeight = '18px'; title_show.style.fontSize = '18px'; title_show.style.padding = '2px 5px'; } } //存储并删除原TITLE document.title_value = obj.title; obj.title = ''; //单行字数未设定,非数值,则取默认值50 if(typeof words_per_line == 'undefined' || isNaN(words_per_line)){ words_per_line = 50; } //格式化成整形值 words_per_line = parseInt(words_per_line); //在title_show中按每行限定字数显示标题内容,模拟TITLE悬停效果 title_show.innerHTML = split_str(document.title_value,words_per_line); //显示悬停效果DIV title_show.style.display = 'block'; //根据鼠标位置设定悬停效果DIV位置 event = event || window.event; //鼠标、键盘事件 var top_down = 15; //下移15px避免遮盖当前标签 //最左值为当前鼠标位置 与 body宽度减去悬停效果DIV宽度的最小值,否则将右端导致遮盖 var left = Math.min(event.clientX,document.body.clientWidth-title_show.clientWidth); title_show.style.left = left+"px"; //设置title_show在页面中的X轴位置。 title_show.style.top = (event.clientY + top_down)+"px"; //设置title_show在页面中的Y轴位置。 } /** * 鼠标离开隐藏TITLE * @params obj 当前悬停的标签 * */ function titleMouseOut(obj) { var title_show = document.getElementById("title_show"); //不存在悬停效果,直接返回 if(title_show == null) return false; //存在悬停效果,恢复原TITLE obj.title = document.title_value; //隐藏悬停效果DIV title_show.style.display = "none"; } /** * 悬停事件绑定 * @params objs 所有需要绑定事件的Element * */ function attachEvent(objs,words_per_line){ if(typeof objs != 'object') return false; //单行字数未设定,非数值,则取默认值50 if(typeof words_per_line == 'undefined' || isNaN(words_per_line)){ words_per_line = 50; } for(i=0;i<objs.length;i++){ objs[i].onmouseover = function(event){ titleMouseOver(this,event,words_per_line); } objs[i].onmouseout = function(event){ titleMouseOut(this); } } } //初始化,当页面onload的时候,对所有class="title_hover"的标签绑定TITLE悬停事件 window.onload = function(){ attachEvent(getClass('title_hover'),18); //行字数设定为18 } </script> </head> <body> <style> tr{float:left; margin:0 50px;} </style> <table> <tr> <td title="这个是默认的TITLE这个是默认的TITLE这个是默认的TITLE这个是默认的TITLE这个是默认的TITLE这个是默认的TITLE">鼠标悬停[原生版本]</td> </tr> <tr> <td title="这个是JS实现悬停的TITLE这个是JS实现悬停的TITLE这个是JS实现悬停的TITLE这个是JS实现悬停的TITLE这个是JS实现悬停的TITLE" οnmοuseοver="titleMouseOver(this,event,15);" οnmοuseοut="titleMouseOut(this);">鼠标悬停[直接调用函数版本,设定行字数]</td> </tr> <tr> <td class="title_hover" title="ABCTesterABCTesterABCTesterABCTesterABCTesterABCTesterABCTester">鼠标悬停[class控制版本]</td> </tr> <tr> <td title="这个是JS实现悬停的TITLE这个是JS实现悬停的TITLE这个是JS实现悬停的TITLE这个是JS实现悬停的TITLE这个是JS实现悬停的TITLE" οnmοuseοver="titleMouseOver(this,event);" οnmοuseοut="titleMouseOut(this);">鼠标悬停[直接调用函数版本,默认行字数]</td> </tr> </table> </body> </html>
感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools.jb51.net/code/HtmlJsRun测试一下运行效果。
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript页面元素操作技巧总结》、《JavaScript操作DOM技巧总结》、《JavaScript切换特效与技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。
JS,TITLE,悬停,长久显示
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 群星2013-青春缤纷辑压箱宝大公开3CD2[新加坡限量版][WAV整轨]
- 林育群.2013-BalladShow(日本版)【环球】【WAV+CUE】
- 陈加洛.1992-痛到感觉不到【宝丽金】【WAV+CUE】
- 群星.2023-宿命之敌电视剧原声带【韶愔音乐】【FLAC分轨】
- 東京事変-大発見[FLAC+CUE]
- 椎名林檎-三文ゴシップ[FLAC+CUE]
- 2024年08月04日
- 裘德《裘德「最后的水族馆」演唱会LIVE》[320K/MP3][228.89MB]
- 裘德《裘德「最后的水族馆」演唱会LIVE》[24bit 48kHz][FLAC/分轨][2.08G]
- 基因三重奏《如果你什么都不说 音乐会现场录音》[320K/MP3][145.37MB]
- 孟庭苇.1996-月亮说话(2020环球24KGOLD限量版)【上华】【WAV+CUE】
- 群星.1997-新艺宝优质音响系列·国语精选监听版【新艺宝】【WAV+CUE】
- 阿桑.2005-寂寞在唱歌(星外星引进版)【华研国际】【WAV+CUE】
- 基因三重奏《如果你什么都不说 音乐会现场录音》[FLAC/分轨][287.43MB]
- 蔡题谦《我爱你,却依然要看你走》[320K/MP3][88.65MB]