Home  >  Article  >  Web Front-end  >  Detailed explanation of the graphics and text code for implementing the Tooltip floating prompt box effect using native JavaScript

Detailed explanation of the graphics and text code for implementing the Tooltip floating prompt box effect using native JavaScript

黄舟
黄舟Original
2017-03-22 14:42:052886browse

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>春晓</h2><p>春眠不觉晓,</p><p>处处闻啼鸟。</p><p>夜来风雨声,</p><p>花落知多少。</p>";
        _width = 100;
        break;
      case "tooltip4":
        _id = "t4";
        _html = "<img src=&#39;img/1.jpg&#39; width=&#39;500&#39; /> ";
        _width = 520;
        break;
      case "tooltip5":
        _id = "t5";
        _html = "<p id=&#39;mycard&#39;><img 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>春晓</h2><p>春眠不觉晓,</p><p>处处闻啼鸟。</p><p>夜来风雨声,</p><p>花落知多少。</p>&#39;, 100);
    };
    t4.onmouseenter = function () {
      showTooltip(this, "t4", &#39;<img src="img/1.jpg" width="500" /> &#39;, 520);
    };
    t5.onmouseenter = function () {
      var _html = &#39;<p id="mycard"><img src="img/2.jpg" 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>原生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