JS實現(xiàn)TITLE懸停長久顯示效果完整示例
本文實例講述了JS實現(xiàn)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對應標簽對象組成的數(shù)組
* @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默認值為'*',不能直接寫成默認參數(shù)方式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字符切割函數(shù)
* @params string 原字符串
* @params words_per_line 每行顯示的字符數(shù)
*/
function split_str(string,words_per_line) {
//空串,直接返回
if(typeof string == 'undefined' || string.length == 0) return '';
//單行字數(shù)未設定,非數(shù)值,則取默認值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循環(huán)里換行時多次判斷i是否為0
var output_string = string.substring(0,1);
//循環(huán)分隔字符串
for(var i=1;i<string.length;i++) {
//如果當前字符是每行顯示的字符數(shù)的倍數(shù),輸出換行
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;'; //字體大小、內(nèi)間距
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 = '';
//單行字數(shù)未設定,非數(shù)值,則取默認值50
if(typeof words_per_line == 'undefined' || isNaN(words_per_line)){
words_per_line = 50;
}
//格式化成整形值
words_per_line = parseInt(words_per_line);
//在title_show中按每行限定字數(shù)顯示標題內(nèi)容,模擬TITLE懸停效果
title_show.innerHTML = split_str(document.title_value,words_per_line);
//顯示懸停效果DIV
title_show.style.display = 'block';
//根據(jù)鼠標位置設定懸停效果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;
//單行字數(shù)未設定,非數(shù)值,則取默認值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); //行字數(shù)設定為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實現(xiàn)懸停的TITLE這個是JS實現(xiàn)懸停的TITLE這個是JS實現(xiàn)懸停的TITLE這個是JS實現(xiàn)懸停的TITLE這個是JS實現(xiàn)懸停的TITLE"
οnmοuseοver="titleMouseOver(this,event,15);" οnmοuseοut="titleMouseOut(this);">鼠標懸停[直接調(diào)用函數(shù)版本,設定行字數(shù)]</td>
</tr>
<tr>
<td class="title_hover" title="ABCTesterABCTesterABCTesterABCTesterABCTesterABCTesterABCTester">鼠標懸停[class控制版本]</td>
</tr>
<tr>
<td title="這個是JS實現(xiàn)懸停的TITLE這個是JS實現(xiàn)懸停的TITLE這個是JS實現(xiàn)懸停的TITLE這個是JS實現(xiàn)懸停的TITLE這個是JS實現(xiàn)懸停的TITLE"
οnmοuseοver="titleMouseOver(this,event);" οnmοuseοut="titleMouseOut(this);">鼠標懸停[直接調(diào)用函數(shù)版本,默認行字數(shù)]</td>
</tr>
</table>
</body>
</html>
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試一下運行效果。
更多關于JavaScript相關內(nèi)容感興趣的讀者可查看本站專題:《JavaScript頁面元素操作技巧總結》、《JavaScript操作DOM技巧總結》、《JavaScript切換特效與技巧總結》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調(diào)試技巧總結》、《JavaScript數(shù)據(jù)結構與算法技巧總結》及《JavaScript數(shù)學運算用法總結》
希望本文所述對大家JavaScript程序設計有所幫助。
- js實現(xiàn)動畫特效的文字鏈接鼠標懸停提示的方法
- jQuery懸停文字提示框插件jquery.tooltipster.js用法示例【附demo源碼下載】
- js 文字超出長度用省略號代替,鼠標懸停并以懸浮框顯示實例
- js實現(xiàn)鼠標懸停圖片上時滾動文字說明的方法
- js實現(xiàn)文字垂直滾動和鼠標懸停效果
- 基于JS代碼實現(xiàn)當鼠標懸停表格上顯示這一格的全部內(nèi)容
- Javascript仿新浪游戲頻道鼠標懸停顯示子菜單效果
- CSS或者JS實現(xiàn)鼠標懸停顯示另一元素
- JS實現(xiàn)title標題欄文字不間斷滾動顯示效果
- js鼠標移動在title中顯示圖片的效果代碼
- 一個JS函數(shù)搞定網(wǎng)頁標題(title)閃動效果
- js下用層來實現(xiàn)select的title提示屬性
相關文章
js求數(shù)組中全部數(shù)字可拼接出的最大整數(shù)示例代碼
這篇文章主要給大家介紹了利用js如何求數(shù)組中全部數(shù)字可拼接出的最大整數(shù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考借鑒,下面隨著小編一起來學習學習吧。2017-08-08
JavaScript實現(xiàn)多態(tài)和繼承的封裝操作示例
這篇文章主要介紹了JavaScript實現(xiàn)多態(tài)和繼承的封裝操作,結合實例形式分析了javascript中多態(tài)與繼承的實現(xiàn)及封裝相關操作技巧,需要的朋友可以參考下2018-08-08
mqtt.js?無法連接/錯誤提示?WebSocket?connection?to?‘ws://xxxxx‘?
這篇文章主要介紹了mqtt.js?無法連接/錯誤提示?WebSocket?connection?to?‘ws://xxxxx‘?failed:,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-01-01

