淡入淡出简单的css工具提示

时间:2014-01-15 13:08:35

标签: html css

Noobish问题。试图制作一个简单的css工具提示淡入淡出但无法使其工作。搜索了很多,但找不到简单的答案。我假设我把过渡css3放在了错误的地方,但它在其他地方也不起作用......

<style>

    .tooltip{
        display: inline;
        position: relative;
    }

    .tooltip:hover:after{
        border-radius: 5px;
        bottom: 26px;
        content: attr(title);
        left: 20%;
        adding: 5px 15px;
    position: absolute;
    z-index: 98;
    width: 220px;
     }

.tooltip:hover:before{

border-width: 6px 6px 0 6px;
bottom: 20px;
        content: "";
        left: 50%;
        position: absolute;
        z-index: 99;
          opacity: 0;
 -webkit-transition: opacity 1s ease-in-out;
   -moz-transition: opacity 1s ease-in-out;
  -ms-transition: opacity 1s ease-in-out;
   -o-transition: opacity 1s ease-in-out;
   transition: opacity 1s ease-in-out;
     }

</style>

HTML

 <br /><br /><br /><br />
<a href="#" title="Text text text text" class="tooltip"><span title="More">CSS3     Tooltip</span></a>

3 个答案:

答案 0 :(得分:1)

  1. 您不需要:before:after
  2. 您必须首先定义:after内容及其样式,并进行转换。
  3. 然后仅使用过渡属性(例如不透明度)在:after上定义:hover个样式。
  4. 演示 http://jsfiddle.net/abhitalks/aRzA3/1/

    CSS

    .tooltip:after {
        content: attr(title); /* define content here */
        ...
        opacity: 0; /* define initial transition property */
        -webkit-transition: opacity 1s ease-in-out; /* define transitions */
        transition: opacity 1s ease-in-out;
    }
    .tooltip:hover:after {
        opacity: 1; /* provide only the final transition property */
    }
    

答案 1 :(得分:1)

你需要在.tooltip中设置属性:after,not .tooltip:hover:after。

然后添加:

.tooltip:after {
    opacity:0;
}

.tooltip:hover:after {
    opacity:1;
}

.tooltip:hover:之前不需要。

请参阅http://jsfiddle.net/NHkQr/

答案 2 :(得分:0)

我一直在寻找相同但没有javascript或jquery。我发现使用纯CSS3过渡的方式如下。

<强> CSS3

a.tooltip span
{
  position: absolute;
  top: 30px;
  left: 50%;
  margin-left: -76px;
  z-index: 999;
  width: 250px;
  height: auto;
  color: #fff;
  font-size: 12px;
  line-height: 20px;
  border-radius: 6px;
  padding: 2px;
  text-align: center;
  background: #000;
  border: 1px solid #808080;
  visibility: hidden;
  opacity: 0;
  -webkit-transition: all 2s ease-out 0s;
  -moz-transition: all 2s ease-out 0s;
  -ms-transition: all 2s ease-out 0s;
  -o-transition: all 2s ease-out 0s;
  transition: all 2s ease-out 0s;
}
a:hover.tooltip span
{
  visibility: visible;
  -webkit-opacity: 0.90;
  -moz-opacity: 0.90;
  opacity: 0.90;
}

<强> HTML

<a class="tooltip" href="#">Hover Me
    <span>Some text information that you want to display in tooltip</span></a>

有关实时演示的详细信息,请访问此处Tooltip Example using CSS3 Transitions

相关问题