如何将鼠标悬停在文本上与触发文本处于同一行高

时间:2015-01-09 08:21:52

标签: html css alignment

我想知道你是否可以告诉我如何将鼠标悬停在文本上以放置在触发文本所在的位置?截至目前,当您将鼠标悬停在触发文字上时,将鼠标悬停在文字底部的文本上会消失?

在这里摆弄:http://jsfiddle.net/kenhimself/yhhzy9vq/5/

CSS:

div.centered {
    font-size: 5vh;
}
span.ok {
    opacity:1;
}
span.ok:hover, span.ok:focus {
    opacity:0;
}
span.ok:hover + span.no {
    opacity:1;
}
span.no {
    opacity:0;
}
span.ok, span.no {
    -webkit-transition: opacity .2s ease-in-out;
    -moz-transition: opacity .2s ease-in-out;
    -ms-transition: opacity .2s ease-in-out;
    -o-transition: opacity .2s ease-in-out;
    transition: opacity .2s ease-in-out;
}
span {
    margin: 0 auto;
    display: table;
    font-size: 5vh;
    line-height: 1;
    white-space: nowrap;
}

2 个答案:

答案 0 :(得分:1)

使用rgba()的伪元素和color值并转换color属性,而不是转换opacity

Updated Fiddle

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  font-size: 100%;
  font-family: helvetica;
  font-weight: normal;
  font-style: normal;
}
div.centered {
  font-size: 5vh;
}
span.ok {
  position: relative;
  color: rgba(0, 0, 0, 1);
  -webkit-transition: color .2s ease-in-out;
  -ms-transition: color .2s ease-in-out;
  transition: color .2s ease-in-out;
}
span.ok:after {
  content: attr(data-tooltip);
  position: absolute;
  left: 0;
  transform: translateX(-25%);
  z-index: -1;
  color: rgba(0, 0, 0, 0);
  -webkit-transition: color .2s ease-in-out;
  -ms-transition: color .2s ease-in-out;
  transition: color .2s ease-in-out;
}
span.ok:hover:after {
  color: rgba(0, 0, 0, 1)
}
span.ok:hover {
  color: rgba(0, 0, 0, 0);
}
span {
  margin: 0 auto;
  display: table;
  font-size: 5vh;
  line-height: 1;
  white-space: nowrap;
}
<div class="centered">
  <span data-tooltip="hello hoverover" class="ok">My Link</span><br/>
  <span data-tooltip="hello hoverover" class="ok">My Link</span><br/>
  <span data-tooltip="hello hoverover" class="ok">My Link</span><br/>
  <span data-tooltip="hello hoverover" class="ok">My Link</span><br/>
  <span data-tooltip="hello hoverover" class="ok">My Link</span><br/>
</div>

答案 1 :(得分:0)

通过指定span.no并提供position:relative来尝试将z-index:-1元素拉到顶部:

span.no {
    opacity:0;
    position: relative;
    top: -27px;
    z-index: -1;
}

检查DEMO