首頁  >  文章  >  web前端  >  JS做出快速提示框

JS做出快速提示框

php中世界最好的语言
php中世界最好的语言原創
2018-06-11 15:04:231707瀏覽

這次帶給大家JS做出快速提示框,JS做出快速提示框的注意事項有哪些,以下就是實戰案例,一起來看一下。

業務場景:當滑鼠移入某元素時,顯示提示方塊進行介紹。當滑鼠移除時,會自動消失。引入ToolTip.js和ToolTip.css

主方法:ToolTip.show(需要提示的元素id, 隨意不重複即可, 要提示的html文字, 寬(可不指定), 高(可不指定) );

ToolTip.show(obj, id, html, width, height);

效果如下:

這次帶給大家,的注意事項有哪些,下面就是實戰案例,一起來看一下。

顯示文字:

2:顯示圖片

# 3:顯示網站

js程式碼:F:\Html5\Plugins\ToolTip\js\ToolTip.js    

(function () {
 var ToolTip = {};
 /**
 * 显示函数
 */
 ToolTip._showTip = function (parentId, childId, html, width, height) {
 var parent = document.getElementById(parentId)//要提示的元素
 var child = document.getElementById(childId);
 if (child === null) {//创建
  var toolTip = document.createElement("p");
  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";
 }
 },
 /**
  * 调用入口
  */
 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;
})();//为防止污染,将方法写在匿名函数中

html程式碼:F:\Html5\Plugins\ToolTip\ToolTip. html




 
 提示框
 


 

唐诗

 

背景图片

 

Yi人诗社

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;
}

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

vue mint-ui在專案中如何使用

D3.js做出記憶體圓形儲存圖

以上是JS做出快速提示框的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn