【问题标题】:Image gallery hover overlay height problem图片库悬停叠加高度问题
【发布时间】:2020-03-15 22:09:02
【问题描述】:

我正在尝试使用图像悬停覆盖效果重新创建谷歌图像搜索布局。 我面临的问题是设置叠加层的正确高度,我设法通过 jQuery 设置了正确的宽度,但我似乎无法获得正确的高度。我希望覆盖覆盖整个图像,而不仅仅是顶部。

图像悬停叠加效果的示例可以在这里找到:Image hover overlay effect

这是我的jsFiddle,将鼠标悬停在图片上方即可查看效果。

function picRow(selector) {

            masterArray = [];

            // create each lineArray and push it to masterArray 
            $(selector).each(function () {

                // get "selector" css px value for margin-bottom 
                // - parse out a floating point number 
                // - and divide by the outer width to get a decimal percentage
                margin = (parseFloat($(this).css("margin-bottom"), 10)) / ($(this).outerWidth());
                marginRight = margin * 100 + "%";
                // subtract subtract the total child margin from the total width to find the usable width
                usableWidth = (1 - ((($(this).find("img").length) - 1) * margin));

                // for each child img of "selector" - add a width/height as value in the ratios array
                ratios = [];
                $(this).find("img").each(function () {
                    ratios.push(($(this).attr('width')) / ($(this).attr('height')));
                });

                // sum all the ratios for later divison
                ratioSum = 0;
                $.each(ratios, function () {
                    ratioSum += parseFloat(this) || 0;
                });

                lineArray = [];
                $.each(ratios, function (i) {
                    obj = {
                        // divide each item in the ratios array by the total array
                        // as set that as the css width in percentage
                        width: ((ratios[i] / ratioSum) * usableWidth) * 100 + "%",
                        height: ((ratios[i] / ratioSum) * usableWidth) * 100 + "%",
                        // set the margin-right equal to the parent margin-bottom
                        marginRight: marginRight
                    };
                    lineArray.push(obj);
                });
                lineArray[lineArray.length - 1].marginRight = "0%";
                // alert(lineArray[lineArray.length - 1].marginRight);
                masterArray.push(lineArray);
            });

            $(selector).each(function (i) {
                $(this).find("img").each(function (x) {
                    $(this).css({
                        "width": masterArray[i][x].width,
                        "margin-right": masterArray[i][x].marginRight
                    });
                });
                $(this).find(".text").each(function (x) {
                    $(this).css({
                        "width": masterArray[i][x].width,
                        /*"height": masterArray[i][x].height,*/
                        "margin-right": masterArray[i][x].marginRight                        
                    });
                });
            });

        }

        $(document).ready(function () {
            picRow(".image-row");
        });
* {
    box-sizing: border-box;
}
.wrapper {
    position: relative;
    padding: 0;
    /*width:100px;*/
    display:block;
}
.text {
    position: absolute;
    top: 0;
    color:#9CBDBE;
    font-weight:bold;
    font-size:30pt;
    background-color:#fff;
    width: 100px;
    /*height: 50px;*/
    text-align: center;
    padding: 1%;
    z-index: 10;
    opacity: 0;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
}
.text:hover {
    opacity:0.8;
}

img {
    z-index:1;
}

.image-row {
  width: 100%;
  margin: 1% 0;
}
.image-row img {
  width: 100%;
  height: auto;
  display: block;
  font-size: 0;
  float: left;
}
.image-row::after {
  content: "";
  display: table;
  clear: both;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="image-row">
    <a href="#1" class="wrapper">
      <span class="text">Hover text</span>
      <img src="https://source.unsplash.com/random/768x960" width="768" height="960"/>
    </a>
    <a href="#2" class="wrapper">
      <span class="text">Hover text</span>
      <img src="https://source.unsplash.com/random/1280x851" width="1280" height="851"/>
    </a>
    </div>

【问题讨论】:

    标签: javascript jquery html css image


    【解决方案1】:

    使用 CSS 实现此目的的最简单方法(参考其他人的答案),但如果您想使用 Jquery,您可以这样做。获取每个图像的高度并将其应用到它们对应的 .text 类中。

    $(selector).each(function (i) {
    
                $(this).find("img").each(function (x) {
                    $(this).css({
                        "width": masterArray[i][x].width,
                        "margin-right": masterArray[i][x].marginRight
                    });
    
                });
                $(this).find(".text").each(function (x) {
                                var imgHeight = $(this).parent().find("img").height();
                    $(this).css({
                        "width": masterArray[i][x].width,
                        "height": imgHeight,
                        "margin-right": masterArray[i][x].marginRight                        
                    });
                });
            });
    

    这是工作的fiddle

    【讨论】:

    • 扩大窗口时,覆盖高度中断,不再填满整个图像。
    • 只需添加 $( window ).resize(function() { picRow(".image-row"); }); .看到这个小提琴jsfiddle.net/shuaibkhan1911/crw2bs15/9
    • 知道如何垂直对齐悬停文本使其居中吗?
    【解决方案2】:

    你可以试试这个 CSS。我给 a 标签.wrapper 一个宽度和高度,以便它可以包含跨度标签。然后是 span stag `.text',我显示块然后 100% 的宽度和高度,这样它将占据 a 标签的空间。最后,我将图像定位为绝对位置。

    请检查我下面的css代码:

    * {
        box-sizing: border-box;
    }
    .wrapper {
        position: relative;
        padding: 0;
        display:block;
        height: 200px;
        width: 500px;
        overflow: hidden;
    }
    .text {
        position: absolute;
        top: 0;
        color:#9CBDBE;
        font-weight:bold;
        font-size:30pt;
        background-color:#fff;
        /* width: 100px; */
        /*height: 50px;*/
        text-align: center;
        /* padding: 1%; */
        z-index: 10;
        opacity: 0;
        -webkit-transition: all 0.5s ease;
        -moz-transition: all 0.5s ease;
        -o-transition: all 0.5s ease;
        transition: all 0.5s ease;
        display: block;
        height: 100%;
        width:100%;
        left: 0;
        border: 1px solid;
    }
    .text:hover {
        opacity:0.8;
    }
    
    img {
        z-index:1;
        position: absolute;
        top: 0;
        left: 0;
    }
    
    .image-row {
      width: 100%;
      margin: 1% 0;
    }
    .image-row img {
      width: 100%;
      height: auto;
      display: block;
      font-size: 0;
      float: left;
    }
    .image-row::after {
      content: "";
      display: table;
      clear: both;
    }
    

    【讨论】:

    • 感谢您的回答,但图像不再位于同一行。在这里检查小提琴:Fiddle
    • 啊,好吧,你能不能显示内联块 .wrapper 类而不是块...
    【解决方案3】:

    我很确定只用 CSS 就可以实现这种效果。

    这是我的解决方案:

    html, body {
      margin: 0;
      width: 100%;
      height: 100%;
    }
    
    * {
        box-sizing: border-box;
    }
    
    .imgWrapper { position: absolute; }
    
    .imgWrapper::before {
      transition: all ease-in-out .2s;
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
    }
    
    .imgWrapper:hover::before {
      background: red;
    }
    
    .imgWrapper:hover .middle {
      opacity: 1;
    }
    
    .middle {
      transition: all ease-in-out .2s;
      opacity: 0;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      background: #fff;
      padding: .5em;
    }
    
    .middle span {
      white-space: nowrap;
    }
    <div class="imgRow">
      <div class="imgWrapper">
        <img src="https://via.placeholder.com/300x300" />
        <div class="middle">
          <span>Hello I am some text!</span>
        </div>
      </div>
    </div>

    【讨论】:

      【解决方案4】:

      添加这个.. 我只是更改了文本位置值。 css

      * {
          box-sizing: border-box;
      }
      .wrapper {
          position: relative;
          padding: 0;
          width:auto;
          display:inline-block;
      }
      .text {
          position: absolute;
          top: 0;
          left:0;
          bottom:0;
          right:0;
          color:#9CBDBE;
          font-weight:bold;
          font-size:30pt;
          background-color:#fff;
          width: 100px;
          /*height: 50px;*/
          text-align: center;
          padding: 1%;
          z-index: 10;
          opacity: 0;
          -webkit-transition: all 0.5s ease;
          -moz-transition: all 0.5s ease;
          -o-transition: all 0.5s ease;
          transition: all 0.5s ease;
          display:flex;
          justify-content:center;
          align-items:center;
      }
      .text:hover {
          opacity:0.8;
      }
      
      img {
          z-index:1;
      }
      
      .image-row {
        width: 100%;
        margin: 1% 0;
      }
      .image-row img {
        width: 100%;
        height: auto;
        display: block;
        font-size: 0;
        float: left;
      }
      .image-row::after {
        content: "";
        display: table;
        clear: both;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-26
        • 1970-01-01
        • 2017-12-29
        • 2022-09-27
        • 2023-01-12
        • 1970-01-01
        相关资源
        最近更新 更多