小編給大家分享一下JS如何實(shí)現(xiàn)TITLE懸停長(zhǎng)久顯示效果,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站制作、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的柯坪網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
具體如下:
<!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標(biāo)簽名,如div,td,ul等
* @return Array 所有class對(duì)應(yīng)標(biāo)簽對(duì)象組成的數(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默認(rèn)值為'*',不能直接寫成默認(rèn)參數(shù)方式getClass(className,tagname='*'),否則IE下報(bào)錯(cuò)
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 '';
//單行字?jǐn)?shù)未設(shè)定,非數(shù)值,則取默認(rèn)值50
if(typeof words_per_line == 'undefined' || isNaN(words_per_line)){
words_per_line = 50;
}
//格式化成整形值
words_per_line = parseInt(words_per_line);
//取出i=0時(shí)的字,避免for循環(huán)里換行時(shí)多次判斷i是否為0
var output_string = string.substring(0,1);
//循環(huán)分隔字符串
for(var i=1;i<string.length;i++) {
//如果當(dāng)前字符是每行顯示的字符數(shù)的倍數(shù),輸出換行
if(i%words_per_line == 0) {
output_string += "<br/>";
}
//每次拼入一個(gè)字符
output_string += string.substring(i,i+1);
}
return output_string;
}
/**
* 鼠標(biāo)懸停顯示TITLE
* @params obj 當(dāng)前懸停的標(biāo)簽
*
*/
function titleMouseOver(obj,event,words_per_line) {
//無TITLE懸停,直接返回
if(typeof obj.title == 'undefined' || obj.title == '') return false;
//不存在title_show標(biāo)簽則自動(dòng)新建
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設(shè)置id屬性
var attr_style = document.createAttribute('style'); //新建Element的style屬性
attr_style.nodeValue = 'position:absolute;' //絕對(duì)定位
+'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設(shè)置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';
}
}
//存儲(chǔ)并刪除原TITLE
document.title_value = obj.title;
obj.title = '';
//單行字?jǐn)?shù)未設(shè)定,非數(shù)值,則取默認(rèn)值50
if(typeof words_per_line == 'undefined' || isNaN(words_per_line)){
words_per_line = 50;
}
//格式化成整形值
words_per_line = parseInt(words_per_line);
//在title_show中按每行限定字?jǐn)?shù)顯示標(biāo)題內(nèi)容,模擬TITLE懸停效果
title_show.innerHTML = split_str(document.title_value,words_per_line);
//顯示懸停效果DIV
title_show.style.display = 'block';
//根據(jù)鼠標(biāo)位置設(shè)定懸停效果DIV位置
event = event || window.event; //鼠標(biāo)、鍵盤事件
var top_down = 15; //下移15px避免遮蓋當(dāng)前標(biāo)簽
//最左值為當(dāng)前鼠標(biāo)位置 與 body寬度減去懸停效果DIV寬度的最小值,否則將右端導(dǎo)致遮蓋
var left = Math.min(event.clientX,document.body.clientWidth-title_show.clientWidth);
title_show.style.left = left+"px"; //設(shè)置title_show在頁面中的X軸位置。
title_show.style.top = (event.clientY + top_down)+"px"; //設(shè)置title_show在頁面中的Y軸位置。
}
/**
* 鼠標(biāo)離開隱藏TITLE
* @params obj 當(dāng)前懸停的標(biāo)簽
*
*/
function titleMouseOut(obj) {
var title_show = document.getElementById("title_show");
//不存在懸停效果,直接返回
if(title_show == null) return false;
//存在懸停效果,恢復(fù)原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;
//單行字?jǐn)?shù)未設(shè)定,非數(shù)值,則取默認(rèn)值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);
}
}
}
//初始化,當(dāng)頁面onload的時(shí)候,對(duì)所有class="title_hover"的標(biāo)簽綁定TITLE懸停事件
window.onload = function(){
attachEvent(getClass('title_hover'),18); //行字?jǐn)?shù)設(shè)定為18
}
</script>
</head>
<body>
<style>
tr{float:left; margin:0 50px;}
</style>
<table>
<tr>
<td title="這個(gè)是默認(rèn)的TITLE這個(gè)是默認(rèn)的TITLE這個(gè)是默認(rèn)的TITLE這個(gè)是默認(rèn)的TITLE這個(gè)是默認(rèn)的TITLE這個(gè)是默認(rèn)的TITLE">鼠標(biāo)懸停[原生版本]</td>
</tr>
<tr>
<td title="這個(gè)是JS實(shí)現(xiàn)懸停的TITLE這個(gè)是JS實(shí)現(xiàn)懸停的TITLE這個(gè)是JS實(shí)現(xiàn)懸停的TITLE這個(gè)是JS實(shí)現(xiàn)懸停的TITLE這個(gè)是JS實(shí)現(xiàn)懸停的TITLE"
οnmοuseοver="titleMouseOver(this,event,15);" οnmοuseοut="titleMouseOut(this);">鼠標(biāo)懸停[直接調(diào)用函數(shù)版本,設(shè)定行字?jǐn)?shù)]</td>
</tr>
<tr>
<td class="title_hover" title="ABCTesterABCTesterABCTesterABCTesterABCTesterABCTesterABCTester">鼠標(biāo)懸停[class控制版本]</td>
</tr>
<tr>
<td title="這個(gè)是JS實(shí)現(xiàn)懸停的TITLE這個(gè)是JS實(shí)現(xiàn)懸停的TITLE這個(gè)是JS實(shí)現(xiàn)懸停的TITLE這個(gè)是JS實(shí)現(xiàn)懸停的TITLE這個(gè)是JS實(shí)現(xiàn)懸停的TITLE"
οnmοuseοver="titleMouseOver(this,event);" οnmοuseοut="titleMouseOut(this);">鼠標(biāo)懸停[直接調(diào)用函數(shù)版本,默認(rèn)行字?jǐn)?shù)]</td>
</tr>
</table>
</body>
</html>以上是“JS如何實(shí)現(xiàn)TITLE懸停長(zhǎng)久顯示效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享名稱:JS如何實(shí)現(xiàn)TITLE懸停長(zhǎng)久顯示效果
URL分享:http://www.chinadenli.net/article22/gccscc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、商城網(wǎng)站、網(wǎng)站導(dǎo)航、響應(yīng)式網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)