向 div 添加工具提示
我有一个像这样的 div 标签:
<div>
<label>Name</label>
<input type="text"/>
</div>
How can I displayed a tooltip on :hover
of the div,最好具有淡入/淡出效果。
I have a div tag like this:
<div>
<label>Name</label>
<input type="text"/>
</div>
How can I displaying a tooltip on :hover
of the div, preferably with a fade in/out effect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(25)
对于基本工具提示,您需要:
例如:
对于更高级的 javascript 版本,您可以查看:
https://jqueryhouse.com/best-jquery -tooltip-plugins/
上面的链接为您提供了 25 个工具提示选项。
For the basic tooltip, you want:
like:
For a fancier javascript version, you can look into:
https://jqueryhouse.com/best-jquery-tooltip-plugins/
The above link gives you 25 options for tooltips.
它可以仅使用 CSS 完成,根本不需要 javascript
运行演示
应用自定义 HTML 属性,例如。
data-tooltip="bla bla"
到您的对象(div 或其他):定义每个
[data-tooltip] 的
对象是透明的、绝对定位的并且以::before
伪元素data-tooltip=""
值作为内容:定义每个
[data-tooltip]
的:hover::before
悬停状态以使其可见:将您的样式(颜色、大小、位置等)应用到工具提示对象;故事结束了。
在演示中,我定义了另一个规则来指定工具提示在将鼠标悬停在工具提示上方但在父项之外时是否必须消失,并使用另一个自定义属性
data-tooltip-persistent
和一个简单的规则:< /p>< strong>注1:浏览器的覆盖范围非常广泛,但请考虑对旧版 IE 使用 javascript 后备(如果需要)。
注 2: 一项增强功能可能是添加一些 JavaScript 来计算鼠标位置,并通过更改应用于它的类将其添加到伪元素中。
It can be done with CSS only, no javascript at all
running demo
Apply a custom HTML attribute, eg.
data-tooltip="bla bla"
to your object (div or whatever):Define the
::before
pseudoelement of each[data-tooltip]
object to be transparent, absolutely positioned and withdata-tooltip=""
value as content:Define
:hover::before
hovering state of each[data-tooltip]
to make it visible:Apply your styles (color, size, position etc) to the tooltip object; end of the story.
In the demo I've defined another rule to specify if the tooltip must disappear when hovering over it but outside of the parent, with another custom attribute,
data-tooltip-persistent
, and a simple rule:Note 1: The browser coverage for this is very wide, but consider using a javascript fallback (if needed) for old IE.
Note 2: an enhancement might be adding a bit of javascript to calculate the mouse position and add it to the pseudo elements, by changing a class applied to it.
您根本不需要 JavaScript 来完成此操作;只需设置
title
属性:请注意,工具提示的视觉呈现取决于浏览器/操作系统,因此它可能会淡入,也可能不会。然而,这是执行工具提示的语义方式,并且它可以与屏幕阅读器等辅助功能软件一起正常工作。
堆栈片段中的演示
You don't need JavaScript for this at all; just set the
title
attribute:Note that the visual presentation of the tooltip is browser/OS dependent, so it might fade in and it might not. However, this is the semantic way to do tooltips, and it will work correctly with accessibility software like screen readers.
Demo in Stack Snippets
这是一个纯 CSS 3 实现(带有可选的 JS),
您唯一要做的就是在任何名为“data-tooltip”的 div 上设置一个属性,当您悬停时该文本将显示在它旁边超过它。
我包含了一些可选的 JavaScript,它们将使工具提示显示在光标附近。 如果您不需要此功能,您可以安全地忽略此小提琴的 JavaScript 部分。
如果您不希望在悬停状态下淡入,只需删除过渡属性。
它的样式类似于
title
属性工具提示。这是 JSFiddle: http://jsfiddle.net/toe0hcyn/1/HTML 示例:
CSS:
可选用于基于鼠标位置的工具提示位置更改的 JavaScript:
Here's a pure CSS 3 implementation (with optional JS)
The only thing you have to do is set an attribute on any div called "data-tooltip" and that text will be displayed next to it when you hover over it.
I've included some optional JavaScript that will cause the tooltip to be displayed near the cursor. If you don't need this feature, you can safely ignore the JavaScript portion of this fiddle.
If you don't want the fade-in on the hover state, just remove the transition properties.
It's styled like the
title
property tooltip. Here's the JSFiddle: http://jsfiddle.net/toe0hcyn/1/HTML Example:
CSS:
Optional JavaScript for mouse position-based tooltip location change:
这是一个很好的 jQuery 工具提示:
https://jqueryui.com/tooltip/
要实现此功能,只需按照以下步骤操作:
将此代码添加到您的
标记中:
在您想要显示工具提示的 HTML 元素上,只需添加 < code>title 属性。 title 属性中的任何文本都将出现在工具提示中。
注意:当 JavaScript 被禁用时,它将回退到默认浏览器/操作系统工具提示。
Here's a nice jQuery Tooltip:
https://jqueryui.com/tooltip/
To implement this, just follow these steps:
Add this code in your
<head></head>
tags:On the HTML elements that you want to have the tooltip, just add a
title
attribute to it. Whatever text is in the title attribute will be in the tooltip.Note: When JavaScript is disabled, it will fallback to the default browser/operating system tooltip.
我做了一些应该能够适应 div 的事情。
HTML
CSS
有关更深入的讨论,请参阅我的帖子:
悬停时的简单格式化工具提示文本
I did something that should be able to be adapted to a div as well.
HTML
CSS
For a more in-depth discussion, see my post:
A simple Formatted ToolTip text on hover
好的,这里满足了您所有的赏金要求:
这是一个演示并链接到我的代码 (JSFiddle)
以下是我融入到这个纯 JS、CSS 和 HTML5 fiddle 中的功能:
HTML:
CSS:
JavaScript:
Okay, here's all of your bounty requirements met:
Here's a demo and link to my code (JSFiddle)
Here are the features that I've incorporated into this purely JS, CSS and HTML5 fiddle:
HTML:
CSS:
JavaScript:
我开发了三种类型的淡入淡出效果:
I have developed three type fade effects :
您可以使用数据属性、伪元素和
content: attr()
例如创建自定义 CSS 工具提示。http://jsfiddle.net/clintioo/gLeydk0k/11/
。
You can create custom CSS tooltips using a data attribute, pseudo elements and
content: attr()
eg.http://jsfiddle.net/clintioo/gLeydk0k/11/
.
您可以使用标题。它适用于几乎所有事情
只需考虑任何对 html 窗口可见的标签,然后在其标签内插入一个
title="whatever tooltip you'd like"
即可获得一个工具提示。You can use title. it'll work for just about everything
<div title="Great for making new friends through cooperation.">
<input script=JavaScript type=button title="Click for a compliment" onclick="window.alert('Your hair reminds me of a sunset across a prairie')" value="making you happy">
<table title="Great job working for those who understand the way i feel">
just think of any tag that can be visible to html window and insert a
title="whatever tooltip you'd like"
inside it's tag and you got yourself a tooltip.您也可以使用它作为工具提示...它的工作原理相同,但您必须编写额外的标签,就是这样。
you can also use this as tooltip...It works same but you have to write extra tag thats it..
这是一个简单的工具提示实现,它考虑了鼠标的位置以及窗口的高度和宽度:
(另请参阅这个小提琴)
Here's a simple tooltip implementation that keeps into account the position of your mouse as well as the height and width of your window :
(see also this Fiddle)
您可以在
onmouseover
和onmouseout
期间切换子 div,如下所示:Stack Snippets & 中的示例jsFiddle
You can toggle a child div during
onmouseover
andonmouseout
like this:Example in Stack Snippets & jsFiddle
最简单的方法是在包含元素上设置
position:relative
,在容器内的工具提示元素上设置position:absolute
,使其相对于父元素(包含元素)浮动)。例如:The simplest way would be to set
position: relative
on the containing element andposition: absolute
on the tooltip element inside the container to make it float relative to the parent (containing element). For example:试试这个。您可以仅使用 css 来完成此操作,我只为工具提示添加了
data-title
属性。Try this. You can do it with only css and I have only added
data-title
attribute for tooltip.您还可以自定义工具提示样式。请参考这个链接:
http://jqueryui.com/tooltip/#custom-style
You can also customise tooltip style. Please refer this link:
http://jqueryui.com/tooltip/#custom-style
仅限 CSS3 的解决方案可能是:
CSS3:
HTML5:
然后您可以以相同的方式创建
tooltip-2
div...您当然也可以使用title
属性而不是data
属性。A CSS3-only solution could be:
CSS3:
HTML5:
You could then create a
tooltip-2
div the same way... you can of course also use thetitle
attribute instead ofdata
attribute.示例
您可以使用简单的 css 来完成...
jsfiddle
在这里您可以看到下面的工具提示 css 代码you can do it with simple css...
jsfiddle
here you can see the examplebelow css code for tooltip
不使用任何API
来执行类似的操作
您也可以使用纯 CSS 和 Jquery Demo HTML
CSS
Jquery
Without using any API
You can do something like this too by using pure CSS and Jquery Demo
HTML
CSS
Jquery
你可以使用纯CSS制作工具提示。试试这个。希望它能帮助你解决你的问题。
HTML
CSS
You can make tooltip using pure CSS.Try this one.Hope it should help you to solve your problem.
HTML
CSS
工具提示位置纯 css
Tooltips Position pure css
这个问题有很多答案,但仍然可能会对某人有所帮助。它适用于所有左、右、上、下位置。
这是 css:
HTML 标签可以是这样的:
Text Here
此处文本
< ;p data-tooltip-position="top" data-tooltip="此处有一些工具提示文本" title="">此处文本
这里的文本
There are lots of answers to this question but still may be it will help someone. It is for all left, right, top, bottom positions.
Here is the css:
And the HTML tag can be like this:
<p data-tooltip-position="right" data-tooltip="Some tooltip text here" title="">Text Here</p>
<p data-tooltip-position="left" data-tooltip="Some tooltip text here" title="">Text Here</p>
<p data-tooltip-position="top" data-tooltip="Some tooltip text here" title="">Text Here</p>
<p data-tooltip-position="bottom" data-tooltip="Some tooltip text here" title="">Text Here</p>
您可以尝试引导工具提示。
进一步阅读此处
You can try bootstrap tooltips.
further reading here
和我的版本
然后是 HTML
And my version
Then the HTML
我为它创建了一种公式。
首先这是 html
现在是 css
I created a kind of formula for it.
First this is the html
Now the css for it