自定义工具提示的基本概念,使用纯 Javascript

问题描述 投票:0回答:4

我需要使用纯

Javascript
代码创建自定义工具提示的基本想法;

我想要什么:

例如:

<a href="#" onmousemover="tooltip('text')">Link Text</a>

onmouseover
我想根据
fixed position
的元素
<a>
显示带有
position
的自定义工具提示,从
right:0
left:0
元素的
<a>
开始;

javascript tooltip
4个回答
8
投票

我对这个问题有个好主意

HTML

<a href="https://www.google.com" tooltip="Go to Google">Google</a>

JavaScript

(function () {

var a = document.getElementsByTagName('*'),
    tip, text,
    base = document.createElement('tooltip'); //Defining all objects

for (var x=0;x < a.length;x++) { //I'm using "for" loop to get all "a" elements with attribute "tooltip".
     a[x].onmouseover = function () {
         text = this.getAttribute('tooltip');
         tip = document.createTextNode(text);
         if (text != null) {// Checking if tooltip is empty or not.
               base.innerHTML = '';
               base.appendChild(tip);
               if (document.getElementsByTagName('tooltip')[0]){// Checking for any "tooltip" element
                   document.getElementsByTagName('tooltip')[0].remove();// Removing old tooltip
               }
               base.style.top = (event.pageY + 20) + 'px';
               base.style.left = (event.pageX + 20) + 'px';
               document.body.appendChild(base);
         }

     };
     a[x].onmouseout = function () {
         document.getElementsByTagName('tooltip')[0].remove();// Remove last tooltip
     };
}

})();

通过将此脚本包含到您的页面中,您只需在任何 HTML 元素中使用

tooltip="Text"

CSS

tooltip {
    position: fixed;
    background: #fff;
    color: #333;
    border: 2px solid #333;
    font-size: 15px;
}

您可以随意编辑 CSS!

这是我的JSFiddle

希望这对你有帮助。


3
投票

当用户搜索

custom tooltip
时出现这个问题,我想添加引导程序的 JS 工具提示,尽管您提到了 pure javascript。所以从某种意义上说,这个答案只是给未来的读者另一个不错的选择。

看看这里如何使用引导工具提示插件。 这里了解使用 Bootstrap 的 JS 工具提示时可用的选项和方法。

我仍然在他们的网站上提供

tryit

$(document).ready(function() {
  $('[data-toggle="tooltip"]').tooltip();
});
/* Tooltip */

.test+.tooltip>.tooltip-inner {
  background-color: #73AD21;
  color: #FFFFFF;
  border: 1px solid green;
  padding: 15px;
  font-size: 20px;
}


/* Tooltip on top */

.test+.tooltip.top>.tooltip-arrow {
  border-top: 5px solid green;
}


/* Tooltip on bottom */

.test+.tooltip.bottom>.tooltip-arrow {
  border-bottom: 5px solid blue;
}


/* Tooltip on left */

.test+.tooltip.left>.tooltip-arrow {
  border-left: 5px solid red;
}


/* Tooltip on right */

.test+.tooltip.right>.tooltip-arrow {
  border-right: 5px solid black;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>

<body>

  <div class="container">
    <h3>Tooltip CSS Example</h3>
    <ul class="list-inline">
      <li><a class="test" href="#" data-toggle="tooltip" data-placement="top" title="Hooray!">Top</a></li>
      <li><a class="test" href="#" data-toggle="tooltip" data-placement="bottom" title="Hooray!">Bottom</a></li>
      <li><a class="test" href="#" data-toggle="tooltip" data-placement="left" title="Hooray!">Left</a></li>
      <li><a class="test" href="#" data-toggle="tooltip" data-placement="right" title="Hooray!">Right</a></li>
    </ul>
  </div>
</body>

</html>


1
投票

你需要这样的功能:

function tooltip(text, element) {

    var offsetDistance = 20;

    tt = document.getElementById('ttdiv');
    elem = document.getElementById(element.id);
    tt.style.top = elem.offsetTop + offsetDistance + 'px';
    tt.style.left = elem.offsetLeft + offsetDistance + 'px';
    tt.style.display = 'block';
    tt.innerHTML = text;
}

其中

ttdiv
是工具提示div的id,最初设置为
display:none
。当
hide()
被触发时,您需要一个附带的
onmouseout
函数来隐藏工具提示。

这是一个有效的JSFiddle.


-1
投票

您可以查看 Syncfusion JavaScript Tooltip

https://www.syncfusion.com/javascript-ui-controls/js-tooltip

注意:我为 Syncfusion 工作

© www.soinside.com 2019 - 2024. All rights reserved.