JavaScript實(shí)現(xiàn)短暫提示框功能
業(yè)務(wù)場(chǎng)景:當(dāng)鼠標(biāo)移入某元素時(shí),顯示提示框進(jìn)行介紹。當(dāng)鼠標(biāo)移除時(shí),會(huì)自動(dòng)消失。引入ToolTip.js和ToolTip.css
主方法:ToolTip.show(需要提示的元素id, 隨意不重復(fù)即可, 要提示的html文本, 寬(可不指定), 高(可不指定));
ToolTip.show(obj, id, html, width, height);
效果如下:
1.顯示文本:

2:顯示圖片

3:顯示網(wǎng)站

js代碼:F:\Html5\Plugins\ToolTip\js\ToolTip.js
(function () {
var ToolTip = {};
/**
* 顯示函數(shù)
*/
ToolTip._showTip = function (parentId, childId, html, width, height) {
var parent = document.getElementById(parentId)//要提示的元素
var child = document.getElementById(childId);
if (child === null) {//創(chuàng)建
var toolTip = document.createElement("div");
toolTip.classList = "ui-tooltip-box";
toolTip.id = childId;
toolTip.innerHTML = html;
parent.appendChild(toolTip);
toolTip.style.width = width ? width + "px" : "auto"
toolTip.style.height = height ? height + "px" : "auto"
//定位:
toolTip.style.position = "absolute";
toolTip.style.display = "block";
var left = parent.offsetLeft;
var top = parent.offsetTop;
if (left + toolTip.offsetWidth > document.body.clientWidth) {
left = document.body.clientWidth / 2;
}
toolTip.style.left = left + "px";
toolTip.style.top = top + 20 + "px";
parent.onmouseleave = function (ev) {
setTimeout(function () { //延遲:
document.getElementById(childId).style.display = "none";//隱藏
}, 300);
}
} else {
//顯示
document.getElementById(childId).style.display = "block";
}
},
/**
* 調(diào)用入口
*/
ToolTip.show = function (parentId, childId, html, width, height) {
var parent = document.getElementById(obj)
parent.onmouseenter = function (ev) {
ToolTip._showTip(parentId, childId, html, width, height)
}
}
window.ToolTip = ToolTip;
})();//為防止污染,將方法寫在匿名函數(shù)中
html代碼:F:\Html5\Plugins\ToolTip\ToolTip.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>提示框</title>
<link rel="stylesheet" type="text/css" href="ToolTip.css" rel="external nofollow" >
</head>
<body>
<div class="ui-tooltip-demo">
<p><a class="ui-tooltip" id="tooltip-text">唐詩(shī)</a></p>
<p><a class="ui-tooltip" id="tooltip-photo">背景圖片</a></p>
<p><a class="ui-tooltip" id="tooltip-poem">Yi人詩(shī)社</a></p>
</div>
<script src="js/ToolTip.js"></script>
<script>
//調(diào)用方式
ToolTip.show("tooltip-text", "01", "唐詩(shī)泛指創(chuàng)作于唐朝的詩(shī)" +
"。唐詩(shī)是中華民族最珍貴的文化遺產(chǎn)之一,是" +
"中華文化寶庫(kù)中的一顆明珠," +
"同時(shí)也對(duì)世界上許多民族和國(guó)家的文化發(fā)展產(chǎn)生了很大影響," +
"對(duì)于后人研究唐代的政治、民情、風(fēng)俗、" +
"文化等都有重要的參考意義和價(jià)值。",300,90);
ToolTip.show("tooltip-photo", "02", "<img src=\"imgs/bg.jpg\" height=\"80px\">",150,80);
var html='<iframe src="http://www.toly.top" width="480px" height="300px"/>'
ToolTip.show("tooltip-poem", "03", html);
</script>
</body>
</html>
css代碼:F:\Html5\Plugins\ToolTip\ToolTip.css
body {
font-size: 14px;
line-height: 1.8;
background-image: url("imgs/bg.jpg");
}
.ui-tooltip-demo {
width: 500px;
margin: 30px auto;
padding: 20px 30px;
background-color: rgba(100%, 100%, 100%, 0.4);
border-radius: 10px;
text-align: center;
box-shadow: 2px 1px 0px 3px rgba(0, 0, 0, 0.2);
}
.ui-tooltip-demo .ui-tooltip {
color: #03f;
font-size: 18px;
cursor: help;
}
.ui-tooltip-box {
display: block;
background: #fff;
line-height: 1.6;
border: 1px solid #6cf;
color: #333;
padding: 20px;
font-size: 12px;
border-radius: 5px;
overflow: auto;
}
總結(jié)
以上所述是小編給大家介紹的JavaScript實(shí)現(xiàn)短暫提示框,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
JS 中實(shí)現(xiàn)一個(gè)串型異步函數(shù)隊(duì)列
這篇文章主要介紹了JS 中實(shí)現(xiàn)一個(gè)串型異步函數(shù)隊(duì)列,文章通過(guò)async/await 串型請(qǐng)求展開詳情,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07
使用firebug進(jìn)行調(diào)試javascript的示例
調(diào)試javascript的方法有很多,在本文為大家介紹下使用firebug是如何做到的,感興趣的朋友可以參考下2013-12-12
使用nestjs實(shí)現(xiàn)郵件發(fā)送的代碼詳解
郵箱發(fā)送是我們常見(jiàn)的一個(gè)服務(wù),本篇文章帶大家用nestjs來(lái)實(shí)現(xiàn)一下,文中有詳細(xì)的代碼示例和圖文講解,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-10-10
JavaScript使用URL.canParse驗(yàn)證URL的方法詳解
JavaScript誕生以來(lái),一直沒(méi)有一種簡(jiǎn)單的方法驗(yàn)證URL,現(xiàn)在JavaScript新增了一個(gè)新方法——URL.canParse,文中通過(guò)代碼示例和圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12

