


Detailed explanation of the graphics and text code for implementing the Tooltip floating prompt box effect using native JavaScript
This article mainly introduces the native JavaScript design and implementation of TooltipFloating prompt box special effects. It has certain reference value. Interested friends can refer to it.
Use native JavaScript to design and implement Tooltip floating prompt box effects, and learn about code simplification, event binding, event bubbling and other techniques and knowledge.
Four key points of special effects:
Display: When the mouse moves over the ToolTip hyperlink, the ToolTip prompt box can be displayed
Hide: Mouse When moved, the ToolTip prompt box automatically hides
Positioning: The position of the ToolTip prompt box needs to be set according to the position of the ToolTip hyperlink
Configurable: The ToolTip prompt box can change the size and display content according to different parameters
Note:
1) border-radius and box-shadow are compatible with writing methods
2) Regardless of whether the mouse pointer passes through the selected element or its sub-elements, The mouseover event will be triggered. Corresponding to mouseout
The mouseenter event will only be triggered when the mouse pointer passes through the selected element. Corresponding to mouseleave
3) W3C regulations do not allow inline elements to nest block-level elements. The a link in it has a nested p, which may not comply with W3C standards ( tip: It was created in the a link when moving the a link. The id gets the DOM reference of the element
var $ = function(id){ return document.getElementById(id); }
2) The function of binding the event
function addEvent(obj,event,fn){ //要绑定的元素对象,要绑定的事件,触发的回调函数 if(obj.addEventListener){ //非IE,支持冒泡和捕获 obj.addEventListenner(event,fn,false); }else if(obj.attachEvent){ //IE,只支持冒泡 obj.attachEvent('on'+event,fn); } }
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <style type="text/css"> body{ font-size: 14px; line-height: 1.8; background: url("img/bg.jpg") no-repeat center top; font-family: "微软雅黑"; } #demo{ width: 500px; margin: 30px auto; padding: 20px 30px; position: relative; background-color: #fff; border-radius: 10px; -moz-border-radius: 10px;/*这个属性 主要是专门支持Mozilla Firefox 火狐浏览器的CSS属性*/ -webkit-border-radius: 10px;/*苹果;谷歌,等一些浏览器认,因为他们都用的是webkit内核*/ box-shadow: 0px 0px 0px 10px rgba(0,0,0,0.2); -moz-box-shadow: 0px 0px 0px 10px rgba(0,0,0,0.2); -webkit-box-shadow: 0px 0px 0px 10px rgba(0,0,0,0.2); } #demo h2{ color: #03f; } #demo .tooltip{ color: #03f; cursor: help; } .tooltip-box{ display: block; background: #fff; line-height: 1.6; border: 1px solid #66CCFF; color: #333; padding: 20px; font-size: 12px; border-radius: 5px; -moz-border-radius: 5px; -webkit-border-radius: 5px; overflow: auto; } #mycard img{ float: left; width: 100px; height: 100px; padding: 10px; } #mycard p{ float: left; width: 150px; padding: 0 10px; } </style> <script type="text/javascript"> window.onload=function(){ //绑定事件的函数 function addEvent(obj,event,fn){ //要绑定的元素对象,要绑定的事件,触发的回调函数 if(obj.addEventListener){ //非IE,支持冒泡和捕获 obj.addEventListener(event,fn,false); }else if(obj.attachEvent){ //IE,只支持冒泡 obj.attachEvent('on'+event,fn); } } //通过用户代理的方式判断是否是IE的方法,不能判断出IE11 var isIE = navigator.userAgent.indexOf("MSIE") > -1; var $ = function(id){ return document.getElementById(id); } var demo = $("demo"); //obj - ToolTip超链接元素 //id - ToolTip提示框id //html - ToolTip提示框HTML内容 //width - ToolTip提示框宽度(可选) //height - ToolTip提示框高度(可选) function showTooltip(obj,id,html,width,height){ if($(id)==null){ //创建 <p class="tooltip-box" id="xx">xxxxxxxx</p> var toolTipBox; toolTipBox = document.createElement('p'); toolTipBox.className = "tooltip-box"; toolTipBox.id = id; toolTipBox.innerHTML = html; obj.appendChild(toolTipBox); toolTipBox.style.width = width ? width + 'px':"auto"; toolTipBox.style.height = height ? height + 'px':"auto"; if(!width && isIE){ toolTipBox.style.width = toolTipBox.offsetWidth;//因为IE不支持auto属性 } toolTipBox.style.position = 'absolute'; toolTipBox.style.display = 'block'; var left = obj.offsetLeft; var top = obj.offsetTop + 20; //当浏览器窗口缩小时不让提示框超出浏览器 if(left + toolTipBox.offsetWidth > document.body.clientWidth){ var demoLeft = demo.offsetLeft; left = document.body.clientWidth - toolTipBox.offsetWidth - demoLeft; if(left < 0) left = 0; } toolTipBox.style.left = left + 'px'; toolTipBox.style.top = top + 'px'; addEvent(obj,"mouseleave" ,function(){ setTimeout(function(){ $(id).style.display = 'none'; },300); }); } else{ //显示 $(id).style.display = 'block'; } } //事件冒泡 addEvent(demo,'mouseover',function(e){ var event = e || window.event; var target = event.target || event.srcElement;//IE下,event对象有srcElement属性,但是没有target属性; //Firefox下,event对象有target属性,但是没有srcElement属性.但他们的作用是相当的 //event.srcElement:表示的当前的这个事件源。 if(target.className == "tooltip"){ var _html; var _id; var _width = 200; switch (target.id){ case "tooltip1": _id = "t1"; _html = "中华人民共和国"; break; case "tooltip2": _id = "t2"; _html = "美国篮球职业联赛"; break; case "tooltip3": _id = "t3"; _html = "<h2>春晓</h2><p>春眠不觉晓,</p><p>处处闻啼鸟。</p><p>夜来风雨声,</p><p>花落知多少。</p>"; _width = 100; break; case "tooltip4": _id = "t4"; _html = "<img src='img/1.jpg' width='500' /> "; _width = 520; break; case "tooltip5": _id = "t5"; _html = "<p id='mycard'><img src='img/2.jpg' alt=''/><p><strong>昵称一定要长</strong></p><p>我的简介我的简介</p></p>"; _width = 300; break; case "tooltip6": _id = "t6"; _html = "<iframe src='http://www.imooc.com/' width='480' height='300'></iframe>"; _width = 500; break; } showTooltip(target,_id,_html,_width); } }); /* var t1 = $("tooltip1"); var t2 = $("tooltip2"); var t3 = $("tooltip3"); var t4 = $("tooltip4"); var t5 = $("tooltip5"); var t6 = $("tooltip6"); t1.onmouseenter = function () { showTooltip(this, "t1", '中华人民共和国', 200); }; t2.onmouseenter = function () { showTooltip(this, "t2", '美国篮球职业联赛', 200); }; t3.onmouseenter = function () { showTooltip(this, "t3", '<h2>春晓</h2><p>春眠不觉晓,</p><p>处处闻啼鸟。</p><p>夜来风雨声,</p><p>花落知多少。</p>', 100); }; t4.onmouseenter = function () { showTooltip(this, "t4", '<img src="img/1.jpg" width="500" /> ', 520); }; t5.onmouseenter = function () { var _html = '<p id="mycard"><img src="img/2.jpg" alt=""/><p><strong>昵称一定要长</strong></p><p>我的简介我的简介</p></p>'; showTooltip(this, "t5", _html, 300); }; t6.onmouseenter = function () { var _html = '<iframe src="http://www.imooc.com/" width="480" height="300"></iframe>' showTooltip(this, "t6", _html, 500); };*/ } </script> <body> <p id="demo"> <h2>原生JavaScript实现ToolTip效果</h2> <p>ToolTip效果是非常常见的网页特效,它可以在用户将指针放置在控件上时为用户显示提示信息。 比如简称文字显示一行文字全称,例:<a class="tooltip" id="tooltip1">中国</a>, <a class="tooltip" id="tooltip2">NBA</a>。 又比如显示一段文字,例:唐诗三百首中的<a class="tooltip" id="tooltip3">春晓</a>你会么?如果不看tooltip提示你背不出来的话,那么你 可要加油了。 </p> <p> ToolTip效果还可以用来显示图片,例:<a class="tooltip" id="tooltip4">西湖美景</a>。当然显示一块儿带格式的内容也不在话下,例: <a class="tooltip" id="tooltip5">我的资料</a>。 </p> <p> 甚至你可以显示一整个网站:例:<a class="tooltip" id="tooltip6">慕课网</a>。 </p> <p> 注意好的ToolTip需要考虑样式、效果、页面的边界等信息,希望你可以做出更漂亮的ToolTip效果。 </p> </p> </body> </html>
The above is the detailed content of Detailed explanation of the graphics and text code for implementing the Tooltip floating prompt box effect using native JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
