简体   繁体   English

将文字悬停在图像上时显示文字

[英]Making text appear when hovering over an image

I'm trying to style something for my website. 我正在尝试为我的网站设置样式。 It's super simple, yet I've tried for hours and can't get it to work. 它非常简单,但是我已经尝试了几个小时,无法正常工作。 The preview is here: http://efthemespage02.tumblr.com/ and what I want to do is make a title appear (with the image opaque in the background) when you hover over the image (gif). 预览在这里: http : //efthemespage02.tumblr.com/我想做的是当您将鼠标悬停在图像(gif)上时,出现一个标题(图像在背景中不透明)。 Here is the coding I have right now. 这是我现在的编码。

style/css: 样式/ css:

#wow {
    margin-top:105px;
    margin-left: 320px;
    -webkit-transition: opacity 0.8s ease-in-out;
    -moz-transition: opacity 0.8s ease-in-out;
    -o-transition: opacity 0.8s ease-in-out;
    -ms-transition: opacity 0.8s ease-in-out;
    transition: opacity 0.8s ease-in-out;
    }

    #wow img {
    opacity:1;
    width:560px;
    height:auto;
    padding:5px;
    border: 1px solid #bbb8b8;
    }

    #wow img:hover {
        opacity: 0.4;
         -webkit-transition: opacity 0.4s ease-in-out;
    -moz-transition: opacity 0.4s ease-in-out;
    -o-transition: opacity 0.4s ease-in-out;
    -ms-transition: opacity 0.4s ease-in-out;
    transition: opacity 0.4s ease-in-out;
    }

actual html: 实际的html:

<div id="wow""><img src=""http://media.tumblr.com/30ad609089a2d306c77f3de99a184a4c/tumblr_inline_mpq7oro2491qz4rgp.gif"></div>

Ok so basically I don't know what I'm doing. 好吧,基本上我不知道自己在做什么。 I've tried using every available online source to try and figure it out but nothing has worked. 我尝试使用所有可用的在线资源进行尝试,但没有任何效果。 Thanks for your help! 谢谢你的帮助!

Just make a little change in your HTML to include your text like this: 只需对HTML进行一些更改,以包含如下所示的文本:

<div class="wow">
    <img src="http://media.tumblr.com/30ad609089a2d306c77f3de99a184a4c/tumblr_inline_mpq7oro2491qz4rgp.gif" alt="" />
    <div class="overlay">This is some text</div>
</div>

and then in your CSS, since your sample site will require the use of fixed widths, it will ease everything, so let's go with something really straight, just some positioning and hover states, nothing fancy. 然后在您的CSS中,由于示例站点将需要使用固定宽度,因此它将简化所有操作,因此,让我们使用真正笔直的东西,只是一些定位和悬停状态,不要花哨。 The only important thing is that I got rid of the IDs and replaced them by classes so you can re-use the styles if you need them, and instead of styling the hover state on the image, I added an .overlay class which holds the text and has a background, size, block properties, etc, to it's easier to manipulate. 唯一重要的是,我摆脱了ID并将其替换为类,以便在需要时可以重新使用样式,并且我添加了一个.overlay类,而不是对图像上的悬停状态进行样式设置,文字,并具有背景,大小,块属性等,以便于操作。 See code below: 参见下面的代码:

.wow {
    position:relative;
    width:560px;
    height:310px;
    -webkit-transition: opacity 0.8s ease-in-out;
    -moz-transition: opacity 0.8s ease-in-out;
    -o-transition: opacity 0.8s ease-in-out;
    -ms-transition: opacity 0.8s ease-in-out;
    transition: opacity 0.8s ease-in-out;
}
.wow img {
    position:relative;
    top:0;
    left:0;
    z-index:1
}
.overlay {
    position:absolute;
    width:100%;
    height:100%;
    top:0;
    left:0;
    z-index:2;
    opacity:0;
    -webkit-transition: opacity 0.4s ease-in-out;
    -moz-transition: opacity 0.4s ease-in-out;
    -o-transition: opacity 0.4s ease-in-out;
    -ms-transition: opacity 0.4s ease-in-out;
    transition: opacity 0.4s ease-in-out;
}
.wow:hover > .overlay {
    opacity:1;
    width:560px;
    height:310px height:auto;
    padding:5px;
    display:block;
    background:rgba(255, 255, 255, 0.4)
}

you'll notice that I'm using more or less your styles, so you weren't that far away 您会注意到我正在使用或多或少使用您的样式,所以您离您并不远

You can see a fiddle here 你可以在这里看到一个小提琴

You can omit this from your code 您可以从代码中省略

#wow img:hover {
    opacity: 0.4;
    -webkit-transition: opacity 0.4s ease-in-out;
    -moz-transition: opacity 0.4s ease-in-out;
    -o-transition: opacity 0.4s ease-in-out;
    -ms-transition: opacity 0.4s ease-in-out;
    transition: opacity 0.4s ease-in-out;
}

and set your new image over it by using z-index:2. 并使用z-index:2在其上设置新图像。 Then you change the opacity of your new image which is sitting on top of your original image. 然后,您可以更改位于原始图像顶部的新图像的不透明度。

Click here to see an example fiddle 单击此处查看小提琴示例

html: 的HTML:

    <div id="wow">
         <img src="http://media.tumblr.com/30ad609089a2d306c77f3de99a184a4c/tumblr_inline_mpq7oro2491qz4rgp.gif">
    <div class="newImage">
         <img src="http://media.tumblr.com/30ad609089a2d306c77f3de99a184a4c/tumblr_inline_mpq7oro2491qz4rgp.gif">
    </div>
    </div>

css: CSS:

#wow img {
    position:relative;
    top:0;
    left:0;
    z-index:1 /*bottom level*/
}
.newImage {
    position:absolute;
    top:0;
    left:0;
    z-index:2; /*top level*/
    opacity:0; /*hidden in normal state*/
}
#wow:hover > .newImage{
    opacity:1; /*visible in hover state*/
}

try this css 试试这个CSS

    .txtOverlay{
        margin-top:105px;
        margin-left: 320px;
        width:560px;
        opacity:0.9;
        font-size:20px;
        font-weight:700;
        text-align:justify;
        border: 1px solid #bbb8b8;
        padding:5px;
        background: url(image url) no-repeat;
     }
     .theText{
        opacity:0;
      }
    .txtOverlay:hover .theText
    {
        opacity:0.9;
        color:#FFFFFF;
        font-size:20px;
    }

html html

 <div class="txtOverlay">
    <div class="theText">
     <!--text here-->
    </div>
  </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM