简体   繁体   English

如何使纯css浮动工具提示(绝对定位跨度)动态调整大小以适应文本

[英]How to make pure css floating tooltips (absolutely positioned span) dynamically resize to accommodate text

I recently had an idea for using the CSS pseudo-class :hover to display a styled tooltip when the mouse is hovered over a link. 我最近有一个使用CSS伪类的想法:hover当鼠标悬停在链接上时, :hover以显示样式化的工具提示。

The basic code for the link looks like this: 链接的基本代码如下所示:

 .hasTooltip { position:relative; } .hasTooltip span { display:none; } .hasTooltip:hover span { display:block; background-color:black; border-radius:5px; color:white; box-shadow:1px 1px 3px gray; position:absolute; padding:5px; top:1.3em; left:0px; max-width:200px; /* I don't want the width to be too large... */ } 
 <a href="#" class="hasTooltip">This link has a tooltip!<span>This is the tooltip text!</span></a> 

The result is exactly what I want , but with one annoying problem: the span does not expand to accommodate text, and if I don't specify a width, the text is squashed. 结果正是我想要的 ,但有一个恼人的问题:跨度不扩展以容纳文本,如果我没有指定宽度,文本被压扁。

I did some searching on Google, found a couple examples of work people had done ( this example is creepily similar to what I've gotten), but no one seems to have addressed the span width problem I'm having. 我在谷歌上做了一些搜索,发现了一些人们做过的工作的例子( 这个例子与我所得到的相似),但似乎没有人解决我所拥有的span宽度问题。

I know this answer is extremely late, but it appears the key to your issue would be to use: 我知道这个答案非常晚,但看起来你的问题的关键是使用:

white-space: nowrap;

inside of your span, and get rid of any sort of width definition. 在您的范围内,并摆脱任何类型的宽度定义。 Of course the drawback to this will be that the tooltip will only be able to support a single line. 当然,缺点是工具提示只能支持一行。 If you want a multiline solution you will most likely have to use javascript. 如果你想要一个多线解决方案,你很可能必须使用javascript。

Here is an example of of this method: http://jsbin.com/oxamez/1/edit 以下是此方法的示例: http//jsbin.com/oxamez/1/edit

An added bonus is that this works all the way down to IE7. 另外一个好处是,它一直可以运行到IE7。 If you do not need to support IE7, I would suggest folding the span, and img styles into a :before, and :after for the .tooltip. 如果你不需要支持IE7,我建议折叠跨度,并将img样式分为:before,和:after.tooltip。 Then you can populate the text using the data-* attribute. 然后,您可以使用data- *属性填充文本。

I don't think there's a perfect solution to this problem with pure CSS. 我不认为纯CSS有一个完美的解决方案。 The first problem is that when you place the span inside the a tag the span only wants to expand as far as the width of the link. 第一个问题是当你将span放在a标签内时, span只想扩展到链接的宽度。 If you place the span after the the a it's possible to get close to what you're trying to do but you'll have to set the margin-top: 1.3em and then have to set a negative margin to slide the tooltip left. 如果你在a之后放置span ,你可以接近你想要做的但是你必须设置margin-top: 1.3em然后必须设置一个负边距来滑动工具提示。 However, it's going to be a fixed setting so it won't sit exactly at the start of each link. 但是,它将是一个固定的设置,因此它不会完全位于每个链接的开头。

I whipped up a jQuery solution that sets left dynamically (and a nice little fade effect for good measure). 我掀起了一个动态设置left的jQuery解决方案(以及一个很好的小淡入淡出效果)。

Demo: http://jsfiddle.net/wdm954/9jaZL/7/ 演示: http//jsfiddle.net/wdm954/9jaZL/7/


$('.hasTooltip').hover(function() {
    var offset = $(this).offset();
    $(this).next('span').fadeIn(200).addClass('showTooltip');
    $(this).next('span').css('left', offset.left + 'px');
}, function() {
    $(this).next('span').fadeOut(200);
});

These tool tips can also be integrated into a word press theme easily. 这些工具提示也可以轻松地集成到word press主题中。 Just copy the CSS into your style. 只需将CSS复制到您的样式中即可。 Css file and when creating your posts, just take help of the HTML code and create your own tool tips. Css文件和创建帖子时,只需获取HTML代码的帮助并创建自己的工具提示。 Rest is all styling, which can be altered according to your own choice. 休息是所有造型,可以根据您自己的选择进行更改。 You may also use images inside the tool tip boxes. 您也可以在工具提示框内使用图像。

http://www.handycss.com/how/how-to-create-a-pure-css-tooltip/ http://www.handycss.com/how/how-to-create-a-pure-css-tooltip/

I found this and it was working for me. 我找到了这个,它对我有用。 It's a good solution when you have a lot of elements and jquery plugins on the same page and you can't work with 当你在同一页面上有很多元素和jquery插件而你无法使用它时,这是一个很好的解决方案

<a href="#">Text <span>Tooltip</span></a>

View pure CSS solution: JS BIN 查看纯CSS解决方案: JS BIN

Credit to trezy.com 感谢trezy.com

Even though this question is a bit older already, I would suggest the following compromise: 即使这个问题已经有点老了,我建议以下妥协:
Just use max-width: 200px; 只需使用max-width:200px; and min-width: 300%; 最小宽度:300%; or so, 或者,
whereas the min-width could result higher than the max-width. 而最小宽度可能导致高于最大宽度。
Just figure it out. 试一试。
This way you could not have entirely liquid tooltips but the width would stand in kind of a correlation with the width of the containing link element. 这样你就不可能有完全液体的工具提示,但宽度与包含链接元素的宽度有相关性。
In terms of optical pleasantness this approach could be of value. 就光学愉悦而言,这种方法可能是有价值的。


edit : 编辑
Well I must admit it is nonsense what I wrote. 好吧,我必须承认我写的是胡说八道。 When the min-width can be higher than the max-width, there is no sense to it. 当最小宽度可以高于最大宽度时,没有任何意义。 So just putting the min-width in percent would achieve what I tried to suggest. 因此,只需将最小宽度设为百分比即可实现我的建议。 Sorry for that. 对不起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM