【问题标题】:Vertical-align span inside div @ 100% height & fixed width on hover (example included)div内的垂直对齐跨度@ 100%高度和悬停时的固定宽度(包括示例)
【发布时间】:2015-12-11 23:32:49
【问题描述】:

我正在尝试在高度未知的 div 上方实现悬停时显示的隐藏元素。

这里有一个完美的例子:

http://issuu.com/

如果您向下滚动并将鼠标悬停在任何出版物图像上,它将显示一个半透明的叠加层,其中“立即阅读”字样在垂直和水平方向上完全居中对齐。在悬停时淡入淡出。

我正在努力做到这一点。

谢谢

【问题讨论】:

    标签: jquery html css hover vertical-alignment


    【解决方案1】:

    这是一个使用 flexbox 的简单示例:http://jsfiddle.net/hj3gfwuk/

    你的html:

    <div class="parent">
        <div class="child">
            <p>Read now.</p>
        </div>
    </div>
    

    你的CSS:

    html, body { height: 100%; }
    
    .parent {
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: lightgrey;
      position: relative;
      height: 50%;
      width: 50%
    }
    
    .child {
        display: none;
        background-color: rgba(255,255,255,.5);
        height: 100%;
        width: 100%;
    }
    
    .parent:hover .child {
        display: flex;
        justify-content: center;
        align-items: center;
    }
    

    基本思想: 子元素(隐藏元素)的高度和宽度为父元素的 100%,并且背景具有一定的透明度。它最初用display: none; 隐藏当您将鼠标悬停在.parent 上时,.child 的css 会更改,并显示该元素。希望这会有所帮助!

    【讨论】:

    • 非常简单干净,请问这种方法在所有浏览器中的友好程度如何?
    • 这里有一个很好的资源来解决这类问题:caniuse.com/#feat=flexbox
    • 谢谢,会检查一下,虽然我刚刚试过你的小提琴,我试图在文本叠加层后面添加一个图像,但无法让它正常工作。图像的固定宽度为 235 像素,图像的高度相对于其宽度将是“自动”的。但覆盖似乎拉伸它。
    • 更新了解决背景问题的小提琴。请注意,此解决方案应适用于 IE11 和 Edge:jsfiddle.net/hj3gfwuk/1
    • 不幸的是,这使用图像作为背景,图像需要是内联代码,因为图像是从数据库中提取的。也固定宽度。 issuu.com 上的完美示例
    【解决方案2】:

    Working Fiddle

    这是我使用纯 html 和 css 的实现:

    HTML

    <div class="content">
        Hello here is content.Hello here is content.Hello here is content.Hello here is content.Hello here is content.Hello here is content.Hello here is content.Hello here is content.Hello here is content.Hello here is content.Hello here is content.Hello here is content.Hello here is content.
        <div class="hiddenElement"></div>
        <div class="textElement">Read Now</div>
    </div>
    

    CSS

    .content{
        width:200px;
        height:300px;
        background:#ccc;
        position:relative;
    }
    .hiddenElement,.textElement{
         position:absolute;
        left:0;
        top:0;
        height:100%;
        width:100%;
        text-align:center;
        display:none;
    }
    .hiddenElement{
    
        background:#fff;
        opacity:0.6;
    }
    .textElement{
        opacity:1;
        top:50%;
    }
    .content:hover .hiddenElement,.content:hover .textElement{
        display:block;
    }
    

    希望对你有帮助

    【讨论】:

    • 您好,感谢您的回复,不幸的是,由于高度是固定的,这将不起作用。所以 line-height 不起作用,图像宽度为 235px,高度根据宽度缩放。
    • 嘿詹姆斯。小提琴更新为动态高度。立即检查。
    • 看我的回答。删除了行高。它现在适用于不同高度的图像。我的答案中的小提琴链接已更新。
    • 这很好用,除了我必须在文本元素上添加一个负上边距,以使文本在垂直方向上完全居中,因为文本本身的高度。将尝试实现它并将其测试到我的代码中。
    • 如果解决了您的问题,请采纳。
    【解决方案3】:

    @pruber 解决方案不适用于我的 Internet Explorer。
    在@devendrant 解决方案中,他正在修复line-height: 300px; 或者在问题中是div that the height is unknown. 试试这个演示:jsfiddle

    html:

    <div class="content">
        <img src="http://www.gettyimages.co.uk/gi-resources/images/Homepage/Category-Creative/UK/UK_Creative_462809583.jpg"/>
        <div class="overlay">
        <span>Read more</span>
        </div>
    </div>
    

    css:

    div.content{
        position:relative;
    }
    div.content:hover div.overlay{
        display:block;
    }
    div.overlay{
        display: none;
        width: 100%;
        height: 100%;
        position: absolute;
        text-align: center;
        vertical-align: middle;
        top: 0;
        left: 0;
        color: #000;
        background-color: rgba(255,255,255,0.7);
    }
    div.overlay span{
        top: 50%;
        left:50%;
        position: absolute;
        margin-top: -9px;
        margin-left: -35px;
    }
    

    【讨论】:

    • 您好不幸的是,图像的宽度是固定的,但高度是根据宽度缩放的。因此悬停叠加层将固定为图像的宽度和 100% 的图像后面的高度。
    • 你可以尝试不同的div的宽高,一般结果是一样的
    猜你喜欢
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 2015-06-01
    相关资源
    最近更新 更多