最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to show text in image hover - Stack Overflow

programmeradmin3浏览0评论

I have a few tumbnails that I want to show some text on them in hover. I could make them dark in hover but do not know how to add text.

example: /

Here is what I have done:

    a {
    display: inline-block;
    overflow: hidden;
    position: relative;
    }
    a:hover .play {
    background:url() 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=".jpg" />
<br />
</a>

Demo: /

I have a few tumbnails that I want to show some text on them in hover. I could make them dark in hover but do not know how to add text.

example: http://www.lenzflare./video-production-portfolio/

Here is what I have done:

    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./2v9zuc1.jpg" />
<br />
</a>

Demo: http://jsfiddle/jmXdh/79/

Share Improve this question edited Nov 17, 2013 at 22:44 Richard Tingle 17.2k5 gold badges56 silver badges80 bronze badges asked Nov 17, 2013 at 21:40 user2977338user2977338 1353 gold badges5 silver badges11 bronze badges 3
  • You're sure you're not doing that the wrong way -> jsfiddle/jmXdh/80 – adeneo Commented Nov 17, 2013 at 21:47
  • @adeneo so do you mean your links working? I tried it but its not. – user2977338 Commented Nov 17, 2013 at 22:05
  • Solution: stackoverflow./questions/16971757/… – user2977338 Commented Nov 17, 2013 at 22:18
Add a ment  | 

7 Answers 7

Reset to default 3

Well I'm going to assume you want this in a list:

There are a few main concepts here: Relative positioning and how it works with absolute positioning, Source order, and your centering technique.

When giving position:relative; to the box, you are saying "I am the boundary now - for any absolutely positioned things within me" (unless they are relative, and then like that on down the line) - So, the absolutely positioned cover thing you want to fade in - is absolutely positioned to one or more edges of the relative box. (most people use top: 0; left: 0;) --- so the absolute box is no longer in the "flow" and lives in it's own magic world determined by the relative parent.

Source order: your html will appear bottom up when stacking. so your cover thing should be below the image (in the html order) - you could use z-index - but there is no need to do that.

The negative margins are not really awesome and unneeded here. You can just text align center them. I would do some tests and put borders around stuff so you can see what it actually happening. ALSO - I encourage you to use box-sizing: border-box; on everything...

Read about: Border box

HTML

<ul class="thumbnail-list">

  <li>
    <a href="#" class="image-w">
      <img alt="thumbnail"
           src="http://placekitten./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

you could consider something like this fiddle. I copy my code here:

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

HTML

    <a href="/"  class="img" 
          style="background-image:url('http://i42.tinypic./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;
}

It sounds like you want to have a tooltip, if so then add a title to the a href:

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

You could also use the tooltip in jQuery UI.

Otherwise, you could use the javascript onmouseover or the jQuery hover / mouseenter events to show the text in the play div. You may need to make sure that the z-index of the play div is higher than the img.

This works:

.pic{
    background: url(http://i42.tinypic./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

Add some text content to the play element.

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

With added css for .play:

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

Try this: http://jsfiddle/JU7zm/

<a href="/">
    <div class="play">text</div>
    <img class="img" src="http://i42.tinypic./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;  }

Here's a simple JS solution you can insert into your HTML:

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

评论列表(0)

  1. 暂无评论