search
HomeWeb Front-endJS TutorialDetailed 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);
}
}

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(&#39;on&#39;+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(&#39;p&#39;);
        toolTipBox.className = "tooltip-box";
        toolTipBox.id = id;
        toolTipBox.innerHTML = html;
        obj.appendChild(toolTipBox);
        toolTipBox.style.width = width ? width + &#39;px&#39;:"auto";
        toolTipBox.style.height = height ? height + &#39;px&#39;:"auto";
        if(!width && isIE){
          toolTipBox.style.width = toolTipBox.offsetWidth;//因为IE不支持auto属性
        }
        toolTipBox.style.position = &#39;absolute&#39;;
        toolTipBox.style.display = &#39;block&#39;;
        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 + &#39;px&#39;;
        toolTipBox.style.top = top + &#39;px&#39;;
        addEvent(obj,"mouseleave" ,function(){
          setTimeout(function(){
            $(id).style.display = &#39;none&#39;;
          },300);
        });
      }
      else{
        //显示
        $(id).style.display = &#39;block&#39;;
      }
    }
    //事件冒泡
addEvent(demo,&#39;mouseover&#39;,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=&#39;img/1.jpg&#39;    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=&#39;mycard&#39;><img src="/static/imghwm/default1.png"  data-src="img/1.jpg"  class="lazy"  src=&#39;img/2.jpg&#39; alt=&#39;&#39;/><p><strong>昵称一定要长</strong></p><p>我的简介我的简介</p></p>";
        _width = 300;
        break;
      case "tooltip6":
        _id = "t6";
        _html = "<iframe src=&#39;http://www.imooc.com/&#39; width=&#39;480&#39; height=&#39;300&#39;></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", &#39;中华人民共和国&#39;, 200);
    };
    t2.onmouseenter = function () {
      showTooltip(this, "t2", &#39;美国篮球职业联赛&#39;, 200);
    };
    t3.onmouseenter = function () {
      showTooltip(this, "t3", &#39;<h2 id="春晓">春晓</h2><p>春眠不觉晓,</p><p>处处闻啼鸟。</p><p>夜来风雨声,</p><p>花落知多少。</p>&#39;, 100);
    };
    t4.onmouseenter = function () {
      showTooltip(this, "t4", &#39;<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" > &#39;, 520);
    };
    t5.onmouseenter = function () {
      var _html = &#39;<p id="mycard"><img src="/static/imghwm/default1.png"  data-src="img/2.jpg"  class="lazy"   alt=""/><p><strong>昵称一定要长</strong></p><p>我的简介我的简介</p></p>&#39;;
      showTooltip(this, "t5", _html, 300);
    };
    t6.onmouseenter = function () {
      var _html = &#39;<iframe src="http://www.imooc.com/" width="480" height="300"></iframe>&#39;
      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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function