简体   繁体   English

文本悬停在图像上方

[英]Text hover over an image

How can I make a text hover over a image? 如何将文字悬停在图像上? I want the text box to apear exactly over where the image is positioned, so that the image dissapears completly, and on mouse out the image reapears. 我希望文本框准确地位于图像所在的位置,以便图像完全消失,并在鼠标移出图像时重新定位。 I searched everywere but I only found hover effects with different positionig of the hoverbox from where the image is situated... 我搜索了每一个但我只发现悬停效果与悬浮盒的不同位置从图像所在的位置...

No need for JavaScript, unless you're wanting some smooth transition without relying on CSS3. 不需要JavaScript,除非你想要一些平滑过渡而不依赖CSS3。 Assuming the image has fixed dimensions, you'd do something like this: 假设图像具有固定的尺寸,您可以执行以下操作:

<div>
    <p>Text</p>
    <img src="" alt="" width="100px" height="100px" />
</div>

div { position:relative; z-index:1; height:100px; width:100px;  }
img { position:absolute; top:0; left:0; z-index:2; }
div:hover img { display:none; }

JSFiddle . JSFiddle

css: CSS:

.textHover {
    display:none;
    width:100%;
    height:100%;
    position:absolute;
    top:0; left:0;
    text-align:center;
    color:white;
}
.imgContain {
    position:relative;
    display:table;
}
.imgContain:hover .textHover {
    display:block;
}

markup: 标记:

<div class="imgContain">
    <img src="http://placehold.it/300x200"/>
    <div class="textHover">My text here</div>
</div>

http://jsfiddle.net/EACxV/ http://jsfiddle.net/EACxV/

You don't need JavaScript code to do that. 您不需要JavaScript代码来执行此操作。 In pure html & css it will work well. 在纯html和CSS中它会很好用。 Below is example with css animation with opacity change. 下面是带有不透明度变化的css动画的示例。

html HTML

<div class="hvrbox">
        <img src="https://upload.wikimedia.org/wikipedia/commons/2/22/Bochnia_poland_saltmine.jpg" alt="Salt mine" class="hvrbox-layer_bottom">
        <div class="hvrbox-layer_top">
            <div class="hvrbox-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce porttitor ligula porttitor, lacinia sapien non.</div>
        </div>
    </div>

css CSS

.hvrbox,
    .hvrbox * {
        box-sizing: border-box;
    }
    .hvrbox {
        position: relative;
        display: inline-block;
        overflow: hidden;
        max-width: 100%;
        height: auto;
    }
    .hvrbox img {
        max-width: 100%;
    }
    .hvrbox .hvrbox-layer_bottom {
        display: block;
    }
    .hvrbox .hvrbox-layer_top {
        opacity: 0;
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.6);
        color: #fff;
        padding: 15px;
        -moz-transition: all 0.4s ease-in-out 0s;
        -webkit-transition: all 0.4s ease-in-out 0s;
        -ms-transition: all 0.4s ease-in-out 0s;
        transition: all 0.4s ease-in-out 0s;
    }
    .hvrbox:hover .hvrbox-layer_top,
    .hvrbox.active .hvrbox-layer_top {
        opacity: 1;
    }
    .hvrbox .hvrbox-text {
        text-align: center;
        font-size: 18px;
        display: inline-block;
        position: absolute;
        top: 50%;
        left: 50%;
        -moz-transform: translate(-50%, -50%);
        -webkit-transform: translate(-50%, -50%);
        -ms-transform: translate(-50%, -50%);
        transform: translate(-50%, -50%);
    }
    .hvrbox .hvrbox-text_mobile {
        font-size: 15px;
        border-top: 1px solid rgb(179, 179, 179); /* for old browsers */
        border-top: 1px solid rgba(179, 179, 179, 0.7);
        margin-top: 5px;
        padding-top: 2px;
        display: none;
    }
    .hvrbox.active .hvrbox-text_mobile {
        display: block;
    }

I described it with many similar examples (with better animations) here http://goo.gl/EECjCm 我在这里用许多类似的例子(有更好的动画)来描述它http://goo.gl/EECjCm

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

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