JavaScript實(shí)現(xiàn)短暫提示框功能
業(yè)務(wù)場景:當(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">唐詩</a></p>
<p><a class="ui-tooltip" id="tooltip-photo">背景圖片</a></p>
<p><a class="ui-tooltip" id="tooltip-poem">Yi人詩社</a></p>
</div>
<script src="js/ToolTip.js"></script>
<script>
//調(diào)用方式
ToolTip.show("tooltip-text", "01", "唐詩泛指創(chuàng)作于唐朝的詩" +
"。唐詩是中華民族最珍貴的文化遺產(chǎn)之一,是" +
"中華文化寶庫中的一顆明珠," +
"同時(shí)也對世界上許多民族和國家的文化發(fā)展產(chǎn)生了很大影響," +
"對于后人研究唐代的政治、民情、風(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)短暫提示框,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
使用原生js實(shí)現(xiàn)拖拽和粘貼上傳圖片功能
這篇文章主要介紹了使用原生js實(shí)現(xiàn)拖拽和粘貼上傳圖片功能,Vue/Rect?生態(tài)用多了都快忘記原生js怎么寫了,今天需要直接在服務(wù)器裸寫個(gè)頁面,實(shí)現(xiàn)?textarea?文本框里接收拖拽多個(gè)圖片,需要的朋友可以參考下2024-04-04
JavaScript參數(shù)個(gè)數(shù)可變的函數(shù)舉例說明
JavaScript允許一個(gè)函數(shù)傳遞個(gè)數(shù)可變的參數(shù),因?yàn)橛衋rguments這個(gè)內(nèi)置對象,它一個(gè)函數(shù)傳遞的所有參數(shù)的數(shù)組2014-10-10
JS優(yōu)雅的使用function實(shí)現(xiàn)一個(gè)class
這篇文章主要為大家介紹了JS優(yōu)雅的使用function實(shí)現(xiàn)一個(gè)class示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
javascript實(shí)現(xiàn)控制文字大中小顯示
網(wǎng)頁上可以自由改變字體大小是個(gè)非常有助于用戶體驗(yàn)的小功能,現(xiàn)在許多網(wǎng)站上都有此功能,今天我們來簡單實(shí)現(xiàn)下。2015-04-04
微信小程序?qū)崿F(xiàn)點(diǎn)擊空白隱藏的方法示例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)點(diǎn)擊空白隱藏的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
js下拉菜單語言選項(xiàng)簡單實(shí)現(xiàn)
大家對下拉菜單并不陌生吧,下面為大家介紹下使用js實(shí)現(xiàn)下拉菜單語言選項(xiàng),具體實(shí)現(xiàn)如下,喜歡的朋友可以看看2013-09-09

