简体   繁体   English

jQuery div悬停时带有图像标题的额外高度

[英]jQuery div extra height with image captions on hover

I'm getting a weird 3px gap between the image and the overlay, it seems the overlay goes 3px OVER the image height wise. 我在图片和叠加层之间出现了3px的怪异间隙,看来叠加层在图片高度上超出了3px。 It works when I change the CSS to bottom:3px but I think that is a bit tacky. 当我将CSS更改为bottom:3px时,它可以工作,但我认为这有点俗气。 But have no idea where this space is coming from. 但是不知道这个空间是从哪里来的。

This is the code I'm using? 这是我正在使用的代码吗?

HTML HTML

  <div class="item col-50">
    <div class="image-hover">
      <div class="image-hover-inner">
      <a href="<?php the_sub_field('project_index_link');?>">
      <img src="<?php the_sub_field('project_index_image'); ?>">

        <div class="image-caption">                    
          <div class="image-caption-inner">
          <h3 class="heading"><?php the_sub_field('project_index_title');?></h3>
          <p><?php the_sub_field('project_index_description');?></p>
          </div>
        </div>
        </a>
      </div>
    </div>
  </div>

SASS 上海社会科学院

.image-hover {
    position: relative;

        a,
        a:hover {
            color:white;
        }
}

.image-caption {
    position: absolute;
    top:0;
    right:0;
    bottom:3px;
    left:0;
}

.image-caption-inner {
    position: absolute;
    top:50%;
    width:100%;
    display:block;
    -webkit-transform: translatey(-50%);
    -moz-transform: translatey(-50%);
    -o-transform: translatey(-50%);
    transform: translatey(-50%);
}

JQUERY JQUERY

jQuery(document).ready(function($) {
    $('.image-caption').hide();
        $('.image-hover').hover( function() {
        $(this).find('.image-caption').fadeIn(300);
        }, function() {
        $(this).find('.image-caption').fadeOut(300);
    });
});

This happens because you have whitespaces rendered after the image (new lines, tabs, etc.). 发生这种情况是因为您在图像后呈现了空白(新行,制表符等)。 Browsers respect them and this leads to unwanted gap. 浏览器尊重它们,这会导致不必要的差距。 The simple fix for your problem is temporary set a font-size to 0 on a level, and reset it back to original value for .image-caption : 您的问题简单的解决方法是临时设置字体大小,以0a级别,并重置回为原始值.image-caption

.image-hover {
    position: relative;
        a,
        a:hover {
            color:white;
        }
}

.image-caption {
    ...
    bottom: 0;
    font-size: 16px;
}

Now you can set bottom: 0 and don't worry about any weird gaps. 现在您可以设置bottom: 0 ,不用担心任何奇怪的差距。

Demo: http://jsfiddle.net/f8pjqam1/ 演示: http//jsfiddle.net/f8pjqam1/

I used your code.. only thing I added is just a border on .image-caption class so that we can see the boundary of overlay.. 我使用了您的代码..我添加的唯一内容只是.image-caption类上的边框,以便我们可以看到overlay的边框。

check the fiddle I have not found any gap in between... title and description are in middle on image.. 检查小提琴,我之间没有发现任何缝隙...标题和描述在图像中间。

.image-caption {
    position: absolute;
    top:0;
    right:0;
    bottom:3px;
    left:0;
    border:1px solid red;  /* this line added */
}

can you please tell us with the help of fiddle so that people can understand your problem.. 您能否在小提琴的帮助下告诉我们,以便人们可以理解您的问题。

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

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