


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); } }The effect is as shown:
<!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 id="春晓">春晓</h2><p>春眠不觉晓,</p><p>处处闻啼鸟。</p><p>夜来风雨声,</p><p>花落知多少。</p>"; _width = 100; break; case "tooltip4": _id = "t4"; _html = "<img src="/static/imghwm/default1.png" data-src="img/1.jpg" class="lazy" src='img/1.jpg' style="max-width:90%"Detailed explanation of the graphics and text code for implementing the Tooltip floating prompt box effect using native JavaScript" > "; _width = 520; break; case "tooltip5": _id = "t5"; _html = "<p id='mycard'><img src="/static/imghwm/default1.png" data-src="img/1.jpg" class="lazy" 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 id="春晓">春晓</h2><p>春眠不觉晓,</p><p>处处闻啼鸟。</p><p>夜来风雨声,</p><p>花落知多少。</p>', 100); }; t4.onmouseenter = function () { showTooltip(this, "t4", '<img src="/static/imghwm/default1.png" data-src="img/1.jpg" class="lazy" style="max-width:90%" / alt="Detailed explanation of the graphics and text code for implementing the Tooltip floating prompt box effect using native JavaScript" > ', 520); }; t5.onmouseenter = function () { var _html = '<p id="mycard"><img src="/static/imghwm/default1.png" data-src="img/2.jpg" class="lazy" 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 id="原生JavaScript实现ToolTip效果">原生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!

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools
