HTML的工具提示位置相对于鼠标指针 [英] HTML-Tooltip position relative to mouse pointer

查看:130
本文介绍了HTML的工具提示位置相对于鼠标指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何对齐工具提示的自然风格:将鼠标指针的右下方

~~~一些文字才能够提交。 ~~~一些文字才能够提交。 ~~~一些文字才能够提交。 ~~~

 <!DOCTYPE HTML>
< HTML>
< HEAD>
  <冠军>工具提示与图片< /标题>
  <元的charset =UTF-8>
  <风格类型=文本/ CSS>
    .tooltip {
        文字修饰:无;
        位置:亲属;
    }
     .tooltip跨度{
        显示:无;
    }
     .tooltip跨度IMG {
        浮动:左;
    }
     .tooltip:悬停跨度{
        显示:块;
        位置:绝对的;
        溢出:隐藏;
    }
  < /风格>
< /头>
<身体GT;
  <一类=提示的href =htt​​p://www.google.com/>
    谷歌
    <跨度>
      &所述; IMG的alt =SRC =htt​​p://www.google.com/images/srpr/logo4w.png>
    < / SPAN>
  &所述; / a取代;
< /身体GT;
< / HTML>
 

解决方案

有关默认的工具提示的行为只需添加的标题属性。这不能包含图像,但。

 <格标题=经常提示>将鼠标悬停ME< / DIV>
 

在你澄清我在纯JavaScript做这件事的问题,希望你觉得它有用。图像会弹出并跟随鼠标。

的jsfiddle

的JavaScript

  VAR tooltipSpan =的document.getElementById('提示跨度');

window.onmousemove =功能(E){
    VAR X = e.clientX,
        Y = e.clientY;
    tooltipSpan.style.top =(Y + 20)+'像素';
    tooltipSpan.style.left =(X + 20)+'像素';
};
 

CSS

  .tooltip跨度{
    显示:无;
}
.tooltip:悬停跨度{
    显示:块;
    位置:固定;
    溢出:隐藏;
}
 

扩展为多个元素

有关多个元素的一个解决方案是更新所有提示范围的和设置他们在移动鼠标光标下。

的jsfiddle

  VAR提示= document.querySelectorAll('提示跨度。');

window.onmousemove =功能(E){
    变种,X =(e.clientX + 20)+'像素',
        Y =(e.clientY + 20)+'像素';
    对于(VAR I = 0; I< tooltips.length;我++){
        提示[I] .style.top = Y;
        提示[I] .style.left = X;
    }
};
 

How to align the tooltip the natural style: right bottom of the mouse pointer?

~~~ Some more text to be able to submit. ~~~ Some more text to be able to submit. ~~~ Some more text to be able to submit. ~~~

<!DOCTYPE html>
<html>
<head>
  <title>Tooltip with Image</title>
  <meta charset="UTF-8">
  <style type="text/css">
    .tooltip {
        text-decoration:none;
        position:relative;
    }
     .tooltip span {
        display:none;
    }
     .tooltip span img {
        float:left;
    }
     .tooltip:hover span {
        display:block;
        position:absolute;
        overflow:hidden;
    }
  </style>
</head>
<body>
  <a class="tooltip" href="http://www.google.com/">
    Google
    <span>
      <img alt="" src="http://www.google.com/images/srpr/logo4w.png">
    </span>
  </a>
</body>
</html>

解决方案

For default tooltip behavior simply add the the title attribute. This can't contain images though.

<div title="regular tooltip">Hover me</div>

Before you clarified the question I did this up in pure JavaScript, hope you find it useful. The image will pop up and follow the mouse.

jsFiddle

JavaScript

var tooltipSpan = document.getElementById('tooltip-span');

window.onmousemove = function (e) {
    var x = e.clientX,
        y = e.clientY;
    tooltipSpan.style.top = (y + 20) + 'px';
    tooltipSpan.style.left = (x + 20) + 'px';
};

CSS

.tooltip span {
    display:none;
}
.tooltip:hover span {
    display:block;
    position:fixed;
    overflow:hidden;
}

Extending for multiple elements

One solution for multiple elements is to update all tooltip span's and setting them under the cursor on mouse move.

jsFiddle

var tooltips = document.querySelectorAll('.tooltip span');

window.onmousemove = function (e) {
    var x = (e.clientX + 20) + 'px',
        y = (e.clientY + 20) + 'px';
    for (var i = 0; i < tooltips.length; i++) {
        tooltips[i].style.top = y;
        tooltips[i].style.left = x;
    }
};

这篇关于HTML的工具提示位置相对于鼠标指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆