文本悬停在图像上方

时间:2013-03-08 16:13:06

标签: javascript html css hover

如何将文字悬停在图片上?我希望文本框准确地位于图像所在的位置,以便图像完全消失,并在鼠标移出图像时重新定位。我搜索了每一个但我只发现悬停效果与悬浮盒的不同位置从图像所在的位置...

3 个答案:

答案 0 :(得分:1)

不需要JavaScript,除非你想要一些平滑过渡而不依赖CSS3。假设图像具有固定的尺寸,您可以执行以下操作:

<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

答案 1 :(得分:1)

的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;
}

标记:

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

http://jsfiddle.net/EACxV/

答案 2 :(得分:0)

您不需要JavaScript代码来执行此操作。纯粹的html&amp; css它会运作良好。下面是带有不透明度变化的css动画的示例。

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


    .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;
    }

我在这里用许多类似的例子(有更好的动画)来描述它http://goo.gl/EECjCm