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

javascript - DIV with text over an image on hover - Stack Overflow

programmeradmin0浏览0评论

OKay first off this is really really similiar to the homepage.

In the simplest form possible. I have an image, and i'm trying to CSS it so that when i hover over the image, a DIV shows up with some text and a partially transparent background color.

I have no idea how to do this..

OKay first off this is really really similiar to the http://dribbble. homepage.

In the simplest form possible. I have an image, and i'm trying to CSS it so that when i hover over the image, a DIV shows up with some text and a partially transparent background color.

I have no idea how to do this..

Share Improve this question asked Jun 7, 2010 at 2:35 Dylan TaylorDylan Taylor 712 gold badges3 silver badges7 bronze badges 1
  • 4 Why don't you use firebug to inspect what they are doing on the dribble homepage? – Oren Hizkiya Commented Jun 7, 2010 at 2:37
Add a ment  | 

4 Answers 4

Reset to default 3

Here is a start. IE6 won't do this, unless you make the parent an anchor (a).

HTML

<div class="container">
    <img src="something.jpg" alt="" />
    <div>some text</div>
</div>

CSS

.container div {
    display: none;
    opacity: 0.7; /* look into cross browser transparency */
}

.container:hover div {
    display: block;
}

@alex, I think he wants the text to appear over the image, not under it. Two ways to fix this:

  1. Add position:absolute to the div containing the text.
  2. Use a background-image instead of an img tag.

I'd go with 1, as it's better semantically and better for accessibility to use img tags for content-bearing images.

If what you want to obtain is an effect like that on Dribbble page, then you do not need to create a div over an img.

It's sufficient to have 2 versions of the image, one normal and one desaturated and with luminosity increased (or something like that, to give the impression of "transparency").

Now you create a div with the image as background and on mouseover you switch background and add the text. On mouseout you revert the changes.

EDIT: Of course in practice you will dynamically assign the images name (e.g. with PHP), but that's another story. You may even automagically generate the "transparent" image by using GD libraries I guess.

A little example:

CSS:

.squareImg
    {
    display: block;
    width: 100px;
    height: 100px;
    background-image: url("100x100.jpg"); 
    }

.squareImgOver
    {
    display: block;
    width: 100px;
    height: 100px;
    background-image: url("100x100transp.jpg"); 
    }

HTML

<div id="mydiv" class="squareImg" onmouseover="writeText();" 
    onmouseout="eraseText()"></div>

JS

function writeText()
    {
    var d = document.getElementById("mydiv");
    d.className = "squareImgOver";
    d.innerHTML = "something here!";
    }

function eraseText()
    {
    var d = document.getElementById("mydiv");
    d.className = "squareImg";
    d.innerHTML = "";
    }
</script>

I suggest using jQuery as it's easy to say "mouseover" triggers another thing to show up.

发布评论

评论列表(0)

  1. 暂无评论