jquery - 如何使用 CSS 中的一个元素创建工具提示

标签 jquery css tooltip

我有一个由 3 个元素和一个包装器创建的工具提示

html:

<div class="tooltip-wrapper" style="position: relative;">
    <a href="https://twitter.com/jcubic" title="" rel="Twitter">
        <img src="http://images.jcubic.pl/jcubic/profile-twitter.png">
    </a>
    <div class="tooltip" style="top: -34px; left: -18px; display: block; opacity: 0.9648882429441257;">
        <div class="box">Twitter</div>
        <div class="arrow" style="border-left-width: 5px; border-right-width: 5px; border-top-width: 5px;"></div>
    </div>
</div>

内联样式由 javascript(jQuery 插件)创建。

和CSS:

.tooltip {
    display: none;
    position: absolute;
    z-index: 100;
}
.tooltip-wrapper .box {
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    background: #b72100;
    padding: 5px 10px;
    display: inline-block;
    text-align: center;
}
.tooltip-wrapper .arrow {
    width: 0; 
    height: 0; 
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;    
    border-top: 5px solid #b72100;
    margin: auto;
}

仅使用一个元素和 css 伪元素 :after for arrow 来创建工具提示是否可行?

<div class="tooltip">Twitter</div>

可以看demo on my website (配置文件图标)。

我尝试将 .tooltip.box 样式合并并创建 tooltip:after { content: ''; 来自 .arrow 但样式不起作用。

最佳答案

是的,这是可能的。

位置 .box 相对位置和 :after 绝对位置。

HTML:

<div class="tooltip-wrapper" style="position: relative;">
    <a href="https://twitter.com/jcubic" title="" rel="Twitter">
        <img src="http://images.jcubic.pl/jcubic/profile-twitter.png" />
    </a>
    <div class="tooltip" style="top: -34px; left: -18px; display: block; opacity: 0.9648882429441257;">
        <div class="box">Twitter</div>
    </div>
</div>

CSS:

.tooltip-wrapper .box {
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    background: #b72100;
    padding: 5px 10px;
    display: inline-block;
    text-align: center;
    position: relative; /* added this line */
}
.tooltip-wrapper .box:after {
    content: '';
    width: 0; 
    height: 0; 
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;    
    border-top: 5px solid #b72100;
    position: absolute;
    bottom: -5px;
    left: 50%;
    margin-left: -3px;    
}

同时检查这个 demo .

关于jquery - 如何使用 CSS 中的一个元素创建工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19081358/

相关文章:

javascript - 如何选择范围之间的元素?

javascript - SVG 上的 .mouseover() 事件表现得很奇怪

jquery - 如何防止第一次点击时的链接操作并仅使用 jquery 允许第二次点击

css - 如何解决图像 "climbing"(重叠)相互重叠的问题?

html - 将子div放在父div的底部

tooltip - highcharts 工具提示文本对齐

javascript - Umbraco-Images 不显示现有 HTML 标记

javascript - jQuery:基于容器中元素数量的 css 边距?

html - 当悬停在图像上时,链接图像的不透明度为 0.5,工具提示没有不透明度

twitter-bootstrap - 如何更改 Twitter Bootstrap 工具提示的宽度和高度?