【问题标题】:How to show text in image hover如何在图像悬停中显示文本
【发布时间】:2013-11-30 22:31:56
【问题描述】:

我有一些缩略图,我想在悬停时在它们上面显示一些文字。我可以让它们在悬停时变暗,但不知道如何添加文本。

示例:http://www.lenzflare.com/video-production-portfolio/

这是我所做的:

    a {
    display: inline-block;
    overflow: hidden;
    position: relative;
    }
    a:hover .play {
    background:url(http://goo.gl/yJqCOR) no-repeat center    center;
    opacity: 0.8;
    position: absolute;
    width: 200px;
    height: 200px;
    left: 50%;
    top: 50%;
    margin-left: -110px;
    margin-top: -150px;
    }

<a href="/">
<div class="play"></div>
<img class="img" src="http://i42.tinypic.com/2v9zuc1.jpg" />
<br />
</a>

演示:http://jsfiddle.net/jmXdh/79/

【问题讨论】:

标签: javascript jquery html css hover


【解决方案1】:

play 元素添加一些文本内容。

<div class="play">Some text</div>

为 .play 添加了 css:

color:#fff;
font-size:16px;

【讨论】:

    【解决方案2】:

    听起来你想要一个工具提示,如果是这样,那么在 a href 中添加一个标题:

    <a href="/" title="This is my text" >
    

    您也可以在 jQuery UI 中使用工具提示。

    否则,您可以使用 javascript onmouseover 或 jQuery hover / mouseenter 事件在播放 div 中显示文本。您可能需要确保 play div 的 z-index 高于 img。

    【讨论】:

    • 感谢您的帖子。我不擅长 jQuery,所以不知道这个答案。 ://
    【解决方案3】:

    这行得通:

    .pic{
        background: url(http://i42.tinypic.com/2v9zuc1.jpg);
        width: 200px;
        height: 100px;
    }
    
    .text{
        background: black;
        text-align: center;
        color: white;
        opacity: 0;
        width: 100%;
        height: 100%;
        -webkit-transition: opacity 0.6s;
        -moz-transition: opacity 0.6s;
        transition: opacity 0.6s;
    }
    
    .text:hover{
        opacity: 0.8;
    }
    
    
    <div class="pic">
        <div class="text">My Text</div>
    </div>
    

    DEMO

    【讨论】:

      【解决方案4】:

      试试这个:http://jsfiddle.net/JU7zm/

      <a href="/">
          <div class="play">text</div>
          <img class="img" src="http://i42.tinypic.com/2v9zuc1.jpg" />
      </a>
      
      
      
      a {
          display: inline-block;
          overflow: hidden;
          position: relative;
      }
      
      a .play {
          display: none;
          background:url(http://goo.gl/yJqCOR) no-repeat center center;
          opacity: 0.8;
          position: absolute;
          width: 184px;
          height: 104px;
          color: #fff;
      }
      
      a:hover .play { display: block;  }
      

      【讨论】:

        【解决方案5】:

        好吧,我假设您希望将其列在列表中:

        这里有几个主要概念:相对定位以及它如何与绝对定位、源顺序以及您的居中技术一起使用。

        给予位置时:相对;到盒子里,你说“我现在是边界 - 对于我体内的任何绝对定位的东西”(除非它们是相对的,然后像下面那样) - 所以,你想要淡入的绝对定位的覆盖物- 绝对定位到相关框的一个或多个边缘。 (大多数人使用 top: 0; left: 0;) --- 所以绝对盒子不再在“流”中,而是生活在它自己的魔法世界中,由相对父母决定。

        源顺序:堆叠时您的 html 会出现自下而上。所以你的封面应该在图片下方(按 html 顺序) - 你可以使用 z-index - 但没有必要这样做。

        这里的负边距并不是很好,也不需要。您可以将文本居中对齐。我会做一些测试并在东西周围设置边界,这样你就可以看到它实际发生了什么。 ALSO - 我鼓励你使用 box-sizing: border-box;关于一切......

        阅读:Border box

        HTML

        <ul class="thumbnail-list">
        
          <li>
            <a href="#" class="image-w">
              <img alt="thumbnail"
                   src="http://placekitten.com/600/300" />
              
              <div class="cover">
                <h3>Title</h3>
                <p>A little bit more about the thing</p>
              </div>
            </a>
          </li>
          
        </ul> <!-- .thumbnail-list -->
        

        CSS

        .thumbnail-list {
          list-style: none;
          margin: 0; paddingn: 0;
        }
        
        .thumbnail-list li {
          float: left;
        }
        
        a {
          text-decoration: none;
          color: inherit;
        }
        
        .thumbnail-list .image-w {
          display: block;
          position: relative;
          width: 16em;
        }
        
        .image-w img {
          display: block;
          width: 100%;
          height: auto;
        }
        
        .cover {
          position: absolute;
          top: 0; right: 0;
          bottom: 0; left: 0;
          width: 100%;
          height: 100%;
          color: rgba(255,255,255,0);
          text-align: center;
          transition: all 1s linear;
        }
        
        .cover:hover {
          background-color: rgba(0,0,0,.8);
          color: rgba(255,255,255,1);
          transition: all .2s linear;
        }
        
        .thumbnail-list h3 {
          font-size: 1.2em;
        }
        
        .thumbnail-list p {
          font-size: .9em;
        }
        

        Here is the code in action on jsfiddle

        【讨论】:

        • 我没有使用背景图像 --- 我使用的图像实际上是给盒子它的大小 --- 所以你必须实际给盒子一个宽度和如果要使用背景图像,请使用高度。但是您的代码中有一张图片...
        【解决方案6】:

        你可以考虑像this fiddle 这样的东西。 我在这里复制我的代码:

        ==================

        HTML

            <a href="/"  class="img" 
                  style="background-image:url('http://i42.tinypic.com/2v9zuc1.jpg');" 
                  onmouseover="this.firstElementChild.style.display='block'" >
                  <span class='play' onmouseout="this.style.display = 'none'";>
                   my lovely text here
                  <span>
            </a>
        

        ==================

        CSS

        a {
            min-height:104px;
            min-width:184px;
            display: inline-block;
            overflow: hidden;
            position: relative;
        }
        .play{
            display:none;
            color:#fff;
            height:104px;
            width:184px;
            background-color:rgba(0,0,0,0.5);
            position: absolute;
        }
        

        【讨论】:

          【解决方案7】:

          这是一个可以插入 HTML 的简单 JS 解决方案:

          <script type="text/javascript">
              document.getElementById("your_image_id").title = 'your hover text';
          </script>
          

          【讨论】:

            猜你喜欢
            • 2017-02-06
            • 2013-07-11
            • 2016-12-23
            • 1970-01-01
            • 2013-10-23
            • 2017-02-09
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多