悬停内容也包括兄弟div

时间:2016-01-22 16:25:32

标签: javascript jquery html css css3

我有三个处于inline-block状态的图像。每当我将鼠标悬停在这些图像上时,我希望黑色叠加不透明度仅出现在图像上。我重新编写了我的代码,将我对图像的描述放在div.home-image-blocks内,以便我能够在不同的视口中修改它。

问题: 当我将悬停仅应用于div.home-img-wording-blocks时,我不确定为什么黑色叠加覆盖img块容器中的内容。我还将max-height设置为100%和overflow: hidden

有没有人看到我的错误,因为悬停效果显示在下面的内容上,悬停以让图片超越正常视点?



$('.home-img-block img').addClass(function() {
  return (this.width / this.height > 1) ? 'wide' : 'tall';
});

#home-img-block-section {
  width: 100%;
  height: 900px;  /*changed*/
}
#home-img-blocks {
  width: 100%;
  height: 750px;
}
.home-img-block {
  width: 33.33%;
  /*height: 100%;*/
  float: left;
  display: inline-block;
  overflow: hidden;
  cursor: pointer;
  position: relative;
}
.home-img-block:hover .overlay {
  background: rgba(0, 0, 0, 0.6);
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}
.home-img-block:after {
  content: attr(data-content);
  color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  opacity: 0;
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
  border: 1px solid #fff;
  padding: 20px 25px;
  text-align: center;
}
.home-img-block:hover:after {
  opacity: 1;
}
.home-img-block img {
  display: block;
  -webkit-transition: all 1s ease;  /* Safari and Chrome */
  -moz-transition: all 1s ease;  /* Firefox */
  -ms-transition: all 1s ease;  /* IE 9 */
  -o-transition: all 1s ease;  /* Opera */
  transition: all 1s ease;
}
.home-img-block:hover img {
  -webkit-transform: scale(1.25);  /* Safari and Chrome */
  -moz-transform: scale(1.25);  /* Firefox */
  -ms-transform: scale(1.25);  /* IE 9 */
  -o-transform: scale(1.25);  /* Opera */
  transform: scale(1.25);
  background: rgba(0, 0, 0, 0.3);
  width: 33.33%;
  max-height: 100%;
  overflow: hidden;
}
.home-img-block img.wide {
  max-width: 100%;
  max-height: 100%;
  height: auto;
  width: 100%;
}
.home-img-block img.tall {
  max-width: 100%;
  max-height: 100%;
  width: auto;
}
#home-img-block-wording-container {
  width: 100%;
  height: 300px;
}
.home-img-wording-blocks {
  width: 100%;  /* changed was 33%*/
  height: 100%;
  text-align: center;
  display: inline-block;
  vertical-align: top;
}
.home-img-wording-block-title {
  padding-top: 30px;
  font-size: 1.7em;
}
.home-img-wording-block-description {
  padding: 25px 50px 0 50px;
  font-size: 1.1em;
  color: #5d5d5d;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="home-img-block-section">
  <div id="home-img-blocks">
    <div data-content="FIND OUT MORE" class="home-img-block">
      <img src="http://optimumwebdesigns.com/images/test1.jpg">
      <div class="overlay"></div>
      <div class="home-img-wording-blocks">
        <div class="home-img-wording-block-title">WEB DESIGN</div>
        <div class="home-img-wording-block-description">The OD team can see your web design visions brought to life, creating a site that promotes your uniqueness through specific functionalities and features.</div>
      </div>
    </div>
    <div data-content="FIND OUT MORE" class="home-img-block">
      <img src="http://optimumwebdesigns.com/images/test2new.jpg">
      <div class="overlay"></div>
      <div class="home-img-wording-blocks">
        <div class="home-img-wording-block-title">ECOMMERCE</div>
        <div class="home-img-wording-block-description">Custom built solutions catered towards you end goal.</div>
      </div>
    </div>
    <div data-content="FIND OUT MORE" class="home-img-block">
      <img src="http://optimumwebdesigns.com/images/test3new.jpg">
      <div class="overlay"></div>
      <div class="home-img-wording-blocks">
        <div class="home-img-wording-block-title">MARKETING STRATEGIES</div>
        <div class="home-img-wording-block-description">MARKETING STRATEGIES</div>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:3)

您需要将imgdiv.overlay单独包装到自己的div中。根据相关提供的代码,覆盖元素的位置绝对与div.home-img-block元素相关,div.homve-img-wording-blocks也包含height:100%,因此,当设置div.home-img-block时,它意味着完整的高度img元素(也包括描述文本占用的高度)。

同样地,max-height: 100%也从父亲那里获得它的高度,因此img意味着它可以扩展直到它填满整个父亲的高度(包括描述)。但是应用于元素的缩放变换仅增加了25%的高度,因此其缩放高度不超过父容器的高度,因此它不会溢出。

当您将.overlay.home-img-block放入自己的容器元素中时,它们的高度不会基于$('.home-img-block img').addClass(function() { return (this.width / this.height > 1) ? 'wide' : 'tall'; });(包括说明文字)得出。由于我们没有明确地在此包装元素上设置任何高度,因此它的高度恰好足以容纳内容,因此叠加层和图像不会溢出到描述占用的区域。

&#13;
&#13;
#home-img-block-section {
  width: 100%;
  height: 900px;
}
#home-img-blocks {
  width: 100%;
  height: 750px;
}
.home-img-block {
  width: 33.33%;
  float: left;
  display: inline-block;
  overflow: hidden;
  position: relative;
}
.home-img-container {
  position: relative;
  overflow: hidden;
  cursor: pointer;
}
.home-img-container:hover .overlay {
  background: rgba(0, 0, 0, 0.6);
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}
.home-img-container:after {
  content: attr(data-content);
  color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  opacity: 0;
  transition: all 0.5s;
  border: 1px solid #fff;
  padding: 20px 25px;
  text-align: center;
}
.home-img-container:hover:after {
  opacity: 1;
}
.home-img-block img {
  display: block;
  transition: all 1s ease;
}
.home-img-container:hover img {
  transform: scale(1.25);
  background: rgba(0, 0, 0, 0.3);
  width: 33.33%;
  max-height: 100%;
  overflow: hidden;
}
.home-img-block img.wide {
  max-width: 100%;
  max-height: 100%;
  height: auto;
  width: 100%;
}
.home-img-block img.tall {
  max-width: 100%;
  max-height: 100%;
  width: auto;
}
#home-img-block-wording-container {
  width: 100%;
  height: 300px;
}
.home-img-wording-blocks {
  width: 100%;
  height: 100%;
  text-align: center;
  display: inline-block;
  vertical-align: top;
}
.home-img-wording-block-title {
  padding-top: 30px;
  font-size: 1.7em;
}
.home-img-wording-block-description {
  padding: 25px 50px 0 50px;
  font-size: 1.1em;
  color: #5d5d5d;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="home-img-block-section">
  <div id="home-img-blocks">
    <div class="home-img-block">
      <div data-content="FIND OUT MORE" class='home-img-container'>
        <img src="http://optimumwebdesigns.com/images/test1.jpg">
        <div class="overlay"></div>
      </div>
      <div class="home-img-wording-blocks">
        <div class="home-img-wording-block-title">WEB DESIGN</div>
        <div class="home-img-wording-block-description">The OD team can see your web design visions brought to life, creating a site that promotes your uniqueness through specific functionalities and features.</div>
      </div>
    </div>
    <div class="home-img-block">
      <div data-content="FIND OUT MORE" class='home-img-container'>
        <img src="http://optimumwebdesigns.com/images/test2new.jpg">
        <div class="overlay"></div>
      </div>
      <div class="home-img-wording-blocks">
        <div class="home-img-wording-block-title">ECOMMERCE</div>
        <div class="home-img-wording-block-description">Custom built solutions catered towards you end goal.</div>
      </div>
    </div>
    <div class="home-img-block">
      <div data-content="FIND OUT MORE" class='home-img-container'>
        <img src="http://optimumwebdesigns.com/images/test3new.jpg">
        <div class="overlay"></div>
      </div>
      <div class="home-img-wording-blocks">
        <div class="home-img-wording-block-title">MARKETING STRATEGIES</div>
        <div class="home-img-wording-block-description">MARKETING STRATEGIES</div>
      </div>
    </div>
  </div>
</div>
&#13;
df <- read.table(header=T, sep="|", check.names = F, text="
|     TIMESTAMP       | DOY(1)  | Hour(1) | Minute(1) | Second(1) | uSecond(1) |
| 1990-01-01 00:00:00 |      76 |      17 |        35 |        26 |     200000 |
| 1990-01-01 00:00:00 |      76 |      17 |        35 |        26 |     250000 |
| 1990-01-01 00:00:00 |      76 |      17 |        35 |        26 |     300000 |")
df <- df[, -c(1, 8)]
df[, 1] <- as.numeric(as.Date(df[, 1])+df[, 2]-1)
options("digits.secs"=2)
df[, 1] <- as.POSIXct(apply(df, 1, function(x) { sprintf("%s %s:%s:%s", as.Date(x[1], origin="1970-01-01"), x[3], x[4], x[5]+x[6]/1e6 ) }))
df
#                TIMESTAMP DOY(1) Hour(1) Minute(1) Second(1) uSecond(1)
# 1 1990-03-17 17:35:26.20     76      17        35        26     200000
# 2 1990-03-17 17:35:26.25     76      17        35        26     250000
# 3 1990-03-17 17:35:26.29     76      17        35        26     300000
&#13;
&#13;
&#13;

答案 1 :(得分:1)

element.widthelement.height不是真正的属性,每个属性都是未定义的

尝试

$('.home-img-block img').addClass(function() {
  return ($(this).width() / $(this).height() > 1) ? 'wide' : 'tall';
});