js提示信息jtip封裝代碼,可以是圖片或文章
更新時(shí)間:2010年01月28日 21:25:52 作者:
今天是相當(dāng)?shù)睦?,所以就點(diǎn)比較容易點(diǎn)的東西吧,講關(guān)于鼠標(biāo)移動(dòng)后出現(xiàn)提示信息的js代碼。能力有限,寫得不好盡管提出來。
話說本人轉(zhuǎn)行做了前端,于是乎每天都是些div+css啥的。今天就講講這個(gè)用js實(shí)現(xiàn)類似于A標(biāo)簽里的title或alt功能,至于這個(gè)功能有什么好處呢,你聽我慢慢道來,首先title或alt屬性所帶來的提示太過于簡單,樣式也無法修改,而且鼠標(biāo)要移到元素上等待1至3秒鐘才會(huì)顯示出來,內(nèi)容也只有簡單的文字,無法加入html內(nèi)容。所以呢,綜上所述,只好自己封裝一個(gè)屬于自己的js提示框了?;蛟S你會(huì)說jquery不是有個(gè)jtip組件嗎?不錯(cuò),那說明你的思想還挺前衛(wèi)。如果用得習(xí)慣的話那就用吧,反正用誰不是用呢?我只是拿出這個(gè)小例子來大家研究研究。
首先,我們要做的就是理清思路,做任何事都應(yīng)該是這樣,不要一拿到東西就開始寫代碼,先要想想我們要得到什么,然后再去付出什么。這就和談戀愛似的,你不能總想著得到對(duì)方,而不去想方法去付出,呃,有點(diǎn)扯遠(yuǎn)了。我們要得到的是一個(gè)全新的提示框,它可以很簡單,也可以很復(fù)雜,它應(yīng)該能包羅萬象海納百川,這就很容易讓人聯(lián)想到div。然后我還希望我的鼠標(biāo)移到某個(gè)標(biāo)簽時(shí)他能夠及時(shí)的出現(xiàn)在鼠標(biāo)附近,移開時(shí)消失。就這么簡單,現(xiàn)在思路一清晰了,是不是覺得原來就這么容易的一件事。恩,愚子可教也!既然思路也清晰了,那就一步步按照這個(gè)思路來實(shí)現(xiàn)吧。
先是創(chuàng)建一個(gè)DIV出來,并把它隱藏,給它加上你想要的所有樣式。代碼如下:
var tipdiv = document.createElement("div");
tipdiv.id = "txbtip";
tipdiv.style.position = "absolute";
tipdiv.style.padding = "3px";
tipdiv.style.background = "#565656";
tipdiv.style.zIndex = "999";
tipdiv.style.border = "1px solid #000";
tipdiv.style.background = "#F4F8FC";
tipdiv.style.fontsize = "14px";
var rootEle = document.body || document.documentElement;
rootEle.appendChild(tipdiv);
接著給要添加的標(biāo)簽加上onmousemove事件和onmouseout事件了,由于為了更公用,所以在這里我給所有要加的標(biāo)簽一個(gè)共同的class名(txbtip)。
var txbtip = getElementsByClassName('txbtip', 'input');>
function getElementsByClassName(n, tag) {
tag = tag || "*";
var classElements = [], allElements = document.getElementsByTagName(tag);
for (var i = 0; i < allElements.length; i++) {
n = "" + n + "";
var cn = " " + allElements[i].className + " ";
if (cn.indexOf(n) != -1) {
classElements[classElements.length] = allElements[i];
}
}
return classElements;
}
注:這個(gè)方法是獲取某些標(biāo)簽的class為n的集合.
for (var tip in txbtip) {
var temp = "";
txbtip[tip].onmouseover = function(e) {
tipdiv.style.display = "block";
var title = this.title;
temp = this.title;
this.title = "";//這里這樣做的原因是為了清除原來存在的title提示.
tipdiv.innerHTML = title;
setTipPosition(e);//這個(gè)方法是給提示框定位的。
}
txbtip[tip].onmousemove = function(e) {
setTipPosition(e);//這個(gè)方法是給提示框定位的。
}
txbtip[tip].onmouseout = function(e) {
//alert("out");
this.title = temp;
temp = "";
tipdiv.style.display = "none";
}
最后就是給標(biāo)簽定位了,就是上面出現(xiàn)過的setTipPotion方法,它的具體實(shí)現(xiàn)如下:
function setTipPosition(e) {
e = e || event;
tipdiv.style.left = e.clientX + 10 + 'px';
var top = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
tipdiv.style.top = e.clientY + 10 + top + 'px';
}
這樣就算完成得差不多了,然后我們?cè)俚罐D(zhuǎn)過來,把它和頁面綁定相結(jié)合起來。于是乎寫進(jìn)window.onload里吧。
window.onload=function(){...}
然而這樣的話就會(huì)有可能出現(xiàn)一個(gè)頁面有多個(gè)window.onload事件而導(dǎo)至失效,所以還要加些工。而且剛才的提示框的對(duì)應(yīng)標(biāo)簽也有可能已經(jīng)有了鼠標(biāo)事件,也得加個(gè)判斷。
if (window.addEventListener) { window.addEventListener("load", ready, false); } else if (window.attachEvent) { window.attachEvent("onload", ready); }
下面是完整的代碼
jstip.js
[code]
//******js文字提示txb20100119********/
if (window.addEventListener) {
window.addEventListener("load", ready, false);
} else if (window.attachEvent) {
window.attachEvent("onload", ready);
}
function ready() {
var txbtip = getElementsByClassName('txbtip', '*');
var tipdiv = document.createElement("div");
tipdiv.id = "txbtip";
tipdiv.style.position = "absolute";
tipdiv.style.padding = "3px";
tipdiv.style.background = "#565656";
tipdiv.style.zIndex = "999";
tipdiv.style.border = "1px solid #000";
tipdiv.style.background = "#F4F8FC";
tipdiv.style.fontsize = "14px";
tipdiv.style.display = "none";
var rootEle = document.body || document.documentElement;
rootEle.appendChild(tipdiv);
for (var tip in txbtip) {
//alert(txbtip[tip].id);
var temp = "";
txbtip[tip].onmouseover = function(e) {
tipdiv.style.display = "block";
var title = this.title;
temp = this.title;
this.title = "";
tipdiv.innerHTML = title;
setTipPosition(e);
//alert(title);
}
txbtip[tip].onmousemove = function(e) {
setTipPosition(e);
}
txbtip[tip].onmouseout = function(e) {
//alert("out");
this.title = temp;
temp = "";
tipdiv.style.display = "none";
}
}
function getElementsByClassName(n, tag) {
tag = tag || "*";
var classElements = [], allElements = document.getElementsByTagName(tag);
for (var i = 0; i < allElements.length; i++) {
n = "" + n + "";
var cn = " " + allElements[i].className + " ";
if (cn.indexOf(n) != -1) {
classElements[classElements.length] = allElements[i];
}
}
return classElements;
}
function setTipPosition(e) {
e = e || event;
tipdiv.style.left = e.clientX + 10 + 'px';
var top = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
tipdiv.style.top = e.clientY + 10 + top + 'px';
}
}
[code]
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]
首先,我們要做的就是理清思路,做任何事都應(yīng)該是這樣,不要一拿到東西就開始寫代碼,先要想想我們要得到什么,然后再去付出什么。這就和談戀愛似的,你不能總想著得到對(duì)方,而不去想方法去付出,呃,有點(diǎn)扯遠(yuǎn)了。我們要得到的是一個(gè)全新的提示框,它可以很簡單,也可以很復(fù)雜,它應(yīng)該能包羅萬象海納百川,這就很容易讓人聯(lián)想到div。然后我還希望我的鼠標(biāo)移到某個(gè)標(biāo)簽時(shí)他能夠及時(shí)的出現(xiàn)在鼠標(biāo)附近,移開時(shí)消失。就這么簡單,現(xiàn)在思路一清晰了,是不是覺得原來就這么容易的一件事。恩,愚子可教也!既然思路也清晰了,那就一步步按照這個(gè)思路來實(shí)現(xiàn)吧。
先是創(chuàng)建一個(gè)DIV出來,并把它隱藏,給它加上你想要的所有樣式。代碼如下:
復(fù)制代碼 代碼如下:
var tipdiv = document.createElement("div");
tipdiv.id = "txbtip";
tipdiv.style.position = "absolute";
tipdiv.style.padding = "3px";
tipdiv.style.background = "#565656";
tipdiv.style.zIndex = "999";
tipdiv.style.border = "1px solid #000";
tipdiv.style.background = "#F4F8FC";
tipdiv.style.fontsize = "14px";
var rootEle = document.body || document.documentElement;
rootEle.appendChild(tipdiv);
接著給要添加的標(biāo)簽加上onmousemove事件和onmouseout事件了,由于為了更公用,所以在這里我給所有要加的標(biāo)簽一個(gè)共同的class名(txbtip)。
復(fù)制代碼 代碼如下:
var txbtip = getElementsByClassName('txbtip', 'input');>
function getElementsByClassName(n, tag) {
tag = tag || "*";
var classElements = [], allElements = document.getElementsByTagName(tag);
for (var i = 0; i < allElements.length; i++) {
n = "" + n + "";
var cn = " " + allElements[i].className + " ";
if (cn.indexOf(n) != -1) {
classElements[classElements.length] = allElements[i];
}
}
return classElements;
}
注:這個(gè)方法是獲取某些標(biāo)簽的class為n的集合.
復(fù)制代碼 代碼如下:
for (var tip in txbtip) {
var temp = "";
txbtip[tip].onmouseover = function(e) {
tipdiv.style.display = "block";
var title = this.title;
temp = this.title;
this.title = "";//這里這樣做的原因是為了清除原來存在的title提示.
tipdiv.innerHTML = title;
setTipPosition(e);//這個(gè)方法是給提示框定位的。
}
txbtip[tip].onmousemove = function(e) {
setTipPosition(e);//這個(gè)方法是給提示框定位的。
}
txbtip[tip].onmouseout = function(e) {
//alert("out");
this.title = temp;
temp = "";
tipdiv.style.display = "none";
}
最后就是給標(biāo)簽定位了,就是上面出現(xiàn)過的setTipPotion方法,它的具體實(shí)現(xiàn)如下:
復(fù)制代碼 代碼如下:
function setTipPosition(e) {
e = e || event;
tipdiv.style.left = e.clientX + 10 + 'px';
var top = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
tipdiv.style.top = e.clientY + 10 + top + 'px';
}
這樣就算完成得差不多了,然后我們?cè)俚罐D(zhuǎn)過來,把它和頁面綁定相結(jié)合起來。于是乎寫進(jìn)window.onload里吧。
window.onload=function(){...}
然而這樣的話就會(huì)有可能出現(xiàn)一個(gè)頁面有多個(gè)window.onload事件而導(dǎo)至失效,所以還要加些工。而且剛才的提示框的對(duì)應(yīng)標(biāo)簽也有可能已經(jīng)有了鼠標(biāo)事件,也得加個(gè)判斷。
if (window.addEventListener) { window.addEventListener("load", ready, false); } else if (window.attachEvent) { window.attachEvent("onload", ready); }
下面是完整的代碼
jstip.js
[code]
//******js文字提示txb20100119********/
if (window.addEventListener) {
window.addEventListener("load", ready, false);
} else if (window.attachEvent) {
window.attachEvent("onload", ready);
}
function ready() {
var txbtip = getElementsByClassName('txbtip', '*');
var tipdiv = document.createElement("div");
tipdiv.id = "txbtip";
tipdiv.style.position = "absolute";
tipdiv.style.padding = "3px";
tipdiv.style.background = "#565656";
tipdiv.style.zIndex = "999";
tipdiv.style.border = "1px solid #000";
tipdiv.style.background = "#F4F8FC";
tipdiv.style.fontsize = "14px";
tipdiv.style.display = "none";
var rootEle = document.body || document.documentElement;
rootEle.appendChild(tipdiv);
for (var tip in txbtip) {
//alert(txbtip[tip].id);
var temp = "";
txbtip[tip].onmouseover = function(e) {
tipdiv.style.display = "block";
var title = this.title;
temp = this.title;
this.title = "";
tipdiv.innerHTML = title;
setTipPosition(e);
//alert(title);
}
txbtip[tip].onmousemove = function(e) {
setTipPosition(e);
}
txbtip[tip].onmouseout = function(e) {
//alert("out");
this.title = temp;
temp = "";
tipdiv.style.display = "none";
}
}
function getElementsByClassName(n, tag) {
tag = tag || "*";
var classElements = [], allElements = document.getElementsByTagName(tag);
for (var i = 0; i < allElements.length; i++) {
n = "" + n + "";
var cn = " " + allElements[i].className + " ";
if (cn.indexOf(n) != -1) {
classElements[classElements.length] = allElements[i];
}
}
return classElements;
}
function setTipPosition(e) {
e = e || event;
tipdiv.style.left = e.clientX + 10 + 'px';
var top = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
tipdiv.style.top = e.clientY + 10 + top + 'px';
}
}
[code]
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]
您可能感興趣的文章:
相關(guān)文章
微信小程序?qū)崿F(xiàn)上傳照片代碼實(shí)例解析
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)上傳照片代碼實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08javascript獲取URL參數(shù)與參數(shù)值的示例代碼
本篇文章主要是對(duì)javascript獲取URL參數(shù)與參數(shù)值的示例代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-12-12js使用Replace結(jié)合正則替換重復(fù)出現(xiàn)的字符串功能示例
這篇文章主要介紹了js使用Replace結(jié)合正則替換重復(fù)出現(xiàn)的字符串功能,可實(shí)現(xiàn)關(guān)鍵詞描紅的功能,涉及JS重復(fù)匹配的相關(guān)操作技巧,需要的朋友可以參考下2016-12-12微信小程序?qū)崿F(xiàn)數(shù)字滾動(dòng)動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)數(shù)字滾動(dòng)動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07JS中使用apply方法通過不同數(shù)量的參數(shù)調(diào)用函數(shù)的方法
這篇文章主要介紹了JS中使用apply方法通過不同數(shù)量的參數(shù)調(diào)用函數(shù)的方法的相關(guān)資料,需要的朋友可以參考下2016-05-05js中判斷變量類型函數(shù)typeof的用法總結(jié)
下面小編就為大家?guī)硪黄猨s中判斷變量類型函數(shù)typeof的用法總結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08JavaScript實(shí)現(xiàn)全選和全不選操作
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)全選和全不選操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09