鼠标悬停在Image之前的常量文本

时间:2015-03-21 15:52:28

标签: html css css3

我有Example

HTML:

<ul class="img-list">
    <li>
        <img src="http://s11.postimg.org/ynw9rnexf/Cetina_river.jpg" width="360px" height="280px" />
<span class="text-content"><span>
 Text To Display</span></span>
    </li>
</ul>

CSS:

ul.img-list {
     list-style-type: none;
     margin: 0;
     padding: 0;
     text-align: center;
 }
 ul.img-list {
     display: inline-block;
     height: 150px;
     margin: 0 5px 1em 0;
     position: relative;
     width: 360px;
     margin-top:-5px;
 }
 span.text-content {
     background: rgba(0, 0, 0, 0.5);
     color: white;
     cursor: pointer;
     display: table;
     height: 150px;
     left: 0;
     position: absolute;
     top: 0;
     width: 150px;
 }
 span.text-content span {
     display: table-cell;
     text-align: center;
     vertical-align: middle;
 }
 span.text-content {
     background: rgba(0, 0, 0, 0.5);
     color: white;
     cursor: pointer;
     display: table;
     height: 150px;
     left: 0;
     position: absolute;
     top: 0;
     width: 150px;
     opacity: 0;
 }
 ul.img-list li:hover span.text-content {
     opacity: 1;
 }
 span.text-content {
     background: rgba(0, 0, 0, 0.5);
     color: white;
     cursor: pointer;
     display: table;
     height: 280px;
     left: 0;
     position: absolute;
     top: 0;
     width: 360px;
     opacity: 0;
     -webkit-transition: opacity 500ms;
     -moz-transition: opacity 500ms;
     -o-transition: opacity 500ms;
     transition: opacity 500ms;
     font-family: Droid Arabic Kufi;
     font-size: xx-large;
 }

鼠标悬停时,我必须在图像上显示文字。

我需要的是让这个文本在图像底部保持不变,当鼠标悬停在顶部时。

我的意思是,在图像的最后50px,我想要带有常量文本的黑色背景,以及当鼠标运行相同的功能时。

谢谢大家的意见。

3 个答案:

答案 0 :(得分:2)

首先,您要重复定义span.text-content。

其次我改变了你的CSS。检查小提琴http://jsfiddle.net/ynrsh3ue/2/

 ul.img-list
 {
    list-style-type: none;
    margin: 0;
    padding: 0;
    text-align: center;
    display: inline-block;
    height: 150px;
    margin: 0 5px 1em 0;
    position: relative;
    width: 360px;
    margin-top:-5px;

}

span.text-content span
{
    display: table-cell;
    text-align: center;
    vertical-align: bottom;
    -webkit-transition: all 500ms;
    -moz-transition: all 500ms;
    -o-transition: all 500ms;
    transition: all 500ms;
}


ul.img-list li:hover span.text-content
{
    opacity: 1;
    background: rgba(0,0,0,0.5);
    height:280px;
    top:0px;
}

ul.img-list li:hover span.text-content span
{
    vertical-align:middle;
}
span.text-content
{
    background: rgba(0,0,0,1);
    color: white;
    cursor: pointer;
    display: table;
    height: 50px;
    left: 0;
    position: absolute;
    top: 230px;
    width: 360px;
    opacity: 1;
    -webkit-transition: all 500ms;
    -moz-transition: all 500ms;
    -o-transition: all 500ms;
    transition: all 500ms;
    font-family: Droid Arabic Kufi;
    font-size: xx-large;
}

答案 1 :(得分:1)

我更新了你的JSFiddle:http://jsfiddle.net/chimos/ynrsh3ue/1/

您需要删除并更改一些CSS规则才能实现:

 ul.img-list {
     list-style-type: none;
     margin: 0;
     padding: 0;
     height: 100%;
     text-align: center;
 }
 ul.img-list {
     display: inline-block;
     height: 150px;
     margin: 0 5px 1em 0;
     position: relative;
     width: 360px;
     margin-top:-5px;
 }
 ul.img-list li {
     position: relative;
 }
 span.text-content span {
     display: table-cell;
     text-align: center;
     vertical-align: middle;
 }
 ul.img-list li:hover span.text-content {
     height: 280px;
     /*opacity: 1;*/
 }
 span.text-content {
     background: rgba(0, 0, 0, 0.5);
     z-index: 999;
     color: white;
     cursor: pointer;
     display: table;
     height: 20px;
     left: 0;
     position: absolute;
     bottom: 0;
     width: 360px;
     /*opacity: 0;*/
     -webkit-transition: height 500ms;
     -moz-transition: height 500ms;
     -o-transition: height 500ms;
     transition: height 500ms;
     font-family: Droid Arabic Kufi;
     font-size: xx-large;
 }

答案 2 :(得分:1)

这是你试图实现的吗? Fiddle

&#13;
&#13;
* {
  margin: 0;
  padding: 0;
  border-spacing: 0;
  top: 0;
}
ul.img-list {
  list-style-type: none;
  text-align: center;
}
ul.img-list {
  border-spacing: 0;
  display: inline-block;
  height: 150px;
  position: relative;
  width: 360px;
}
span.text-content span {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}
ul.img-list li:hover span.text-content {
  opacity: 1;
  background: rgba(0, 0, 0, 0.5);
  margin-top: -280px;
  height: 280px;
  -webkit-transition: 500ms;
  -moz-transition: 500ms;
  -o-transition: 500ms;
  transition: 500ms;
}
span.text-content {
  background: black;
  position: relative;
  color: white;
  cursor: pointer;
  display: table;
  height: 50px;
  left: 0;
  top: -5px;
  width: 360px;
  opacity: 1;
  -webkit-transition: 500ms;
  -moz-transition: 500ms;
  -o-transition: 500ms;
  transition: 500ms;
  font-family: Droid Arabic Kufi;
  font-size: xx-large;
  z-index: 2;
}
&#13;
<ul class="img-list">
  <li>
    <img src="http://s11.postimg.org/ynw9rnexf/Cetina_river.jpg" width="360px" height="280px" /> <span class="text-content">
            <span>Text To Display</span>
    </span>
  </li>
</ul>
&#13;
&#13;
&#13;