【问题标题】:object-fit:contain on child prevents parent event from triggeringobject-fit:contain on child 防止父事件触发
【发布时间】:2021-04-30 20:43:28
【问题描述】:

let parent = document.getElementById("parent");
let img = document.getElementById("img");

parent.addEventListener("click", function() {
  parent.style.display = "none";
});

img.addEventListener("click", function(e) {
  e.stopPropagation();
})
#parent {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background-color: blue;
}

#img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}
<div id="parent">
  <img src="https://dummyimage.com/600x400/000/fff" id="img">
</div>

基本上,我想要实现的是一个覆盖整个窗口的弹出窗口,并具有一个居中的图像,该图像延伸到最大高度或宽度(取决于图像尺寸)并保持纵横比。完整图像仍然应该是可见的。当您单击剩余的可见父级蓝色背景时,父级和图像都应该消失。

父事件未触发。但是如果我删除 CSS 代码,事件就会触发。这里发生了什么?

【问题讨论】:

  • 您知道您在运行时使用的图像的长宽比吗?是否可以将此数据添加到 HTML 输出中?
  • @yunzen 不。我在想也许有一个简单的 CSS 解决方案,但我想我会采用 JavaScript 方式

标签: javascript css events object-fit


【解决方案1】:

HTML 中的图像是 replaced element

如果您在开发人员工具中检查 img,您可能会发现图像元素本身覆盖了所有父元素(即通过 width: 100%; height: 100%;)。

屏幕上的图像表示由object-fit: contain; 更改。但这不会改变图像元素本身的尺寸。如果我为图像元素添加黄色背景,您会看到。这涵盖了所有蓝色parent

let parent = document.getElementById("parent");
let img = document.getElementById("img");

parent.addEventListener("click", function() {
  parent.style.display = "none";
});

img.addEventListener("click", function(e) {
  e.stopPropagation();
})
#parent {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background-color: blue;
}

#img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: yellow;
}
<div id="parent">
  <img src="https://dummyimage.com/600x400/000/fff" id="img">
</div>

您需要尝试不同的方法才能使您的代码按预期工作

编辑:更改了实现

图片大小为 500 x 500 的示例

let parent = document.getElementById("parent");
let img = document.getElementById("img");

parent.addEventListener("click", function() {
  parent.style.display = "none";
});

img.addEventListener("click", function(e) {
  e.stopPropagation();
})
#parent {
  width: 100%;
  height: 100%;
  
  position: fixed;
  top: 0;
  left: 0;
  background-color: blue;
  
}

#img {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  max-width: 100%;
  max-height: 100%;
  margin: auto;
}
<div id="parent">
  <img src="https://dummyimage.com/500x500/f0f/000" id="img">
</div>

图片大小为 1000 x 600 的示例

let parent = document.getElementById("parent");
let img = document.getElementById("img");

parent.addEventListener("click", function() {
  parent.style.display = "none";
});

img.addEventListener("click", function(e) {
  e.stopPropagation();
})
#parent {
  width: 100%;
  height: 100%;
  
  position: fixed;
  top: 0;
  left: 0;
  background-color: blue;
  
}

#img {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  max-width: 100%;
  max-height: 100%;
  margin: auto;
}
<div id="parent">
  <img src="https://dummyimage.com/1000x600/f0f/000" id="img">
</div>

图片大小为 600 x 1000 的示例

let parent = document.getElementById("parent");
let img = document.getElementById("img");

parent.addEventListener("click", function() {
  parent.style.display = "none";
});

img.addEventListener("click", function(e) {
  e.stopPropagation();
})
#parent {
  width: 100%;
  height: 100%;
  
  position: fixed;
  top: 0;
  left: 0;
  background-color: blue;
  
}

#img {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  max-width: 100%;
  max-height: 100%;
  margin: auto;
}
<div id="parent">
  <img src="https://dummyimage.com/600x1000/f0f/000" id="img">
</div>

【讨论】:

  • 现在我明白了。但不幸的是,图像仍然没有拉伸。它仍然居中,但不占据整个窗口。
  • 您的 sn-p 仅适用,因为您使用的图像尺寸很大,而我使用的大多数图像都只有 500x500 左右。但是 +1 用于解释对象拟合背后的原因。
  • @atan 你是对的。我还没有发现不使用object-fit 模拟object-fit 包含的方法。
  • @atan 我设法解决了图像尺寸的问题。看看我的编辑
  • @atan 您是否检查了我答案末尾的示例?
【解决方案2】:

由于我找不到使用 CSS 的简单解决方案(考虑到问题并不复杂),我决定使用 JavaScript 通过动态调整图像大小来快速修复。

我将把我的答案留给任何需要这个的人。但我仍然希望有一个 CSS 解决方案来解决这个问题。

let parent = document.getElementById("parent");
let img = document.getElementById("img");

parent.addEventListener("click", function() {
  parent.style.display = "none";
});

img.addEventListener("click", function(e) {
  e.stopPropagation();
})

function stretchImage(){
    let w = this.innerWidth;
    let h = this.innerHeight;

    if(w < h){
        img.style.width = "100%";
        img.style.height = "auto";
    }else{
        img.style.width = "auto";
        img.style.height = "100%";
    }
}

window.onresize = stretchImage;
window.onload = stretchImage;
#parent {
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    background-color: blue;
    display: flex;
    justify-content: center;
    align-items: center;
}
<div id="parent">
  <img src="https://dummyimage.com/400x400/000/fff" id="img">
</div>

脚本根据窗口的宽度和高度使用 onload 和 onresize 动态更改图像大小。这样,我什至不需要知道图像的尺寸。

【讨论】:

  • 不需要额外的脚本。使用 CSS。查看我的更新解决方案
【解决方案3】:

子级与父级重叠。子元素占用全部可用空间,并完全覆盖其父元素。所以父元素没有可点击的区域。

如果你调整子元素的高度和宽度,那么你就可以做到这一点。

let parent = document.getElementById("parent");
let img = document.getElementById("img");

parent.addEventListener("click", function() {
  parent.style.display = "none";
});

img.addEventListener("click", function(e) {
  e.stopPropagation();
})
#parent {
  width: 100%;
  height: 100%;
  position:fixed;
  top: 0;
  left: 0;
  background-color: blue;
  display: flex;
    align-items: center;
    justify-content: center;
}

#img {
 overflow:hidden;
}
<div id="parent">
  <img src="https://dummyimage.com/400x400/000/fff" id="img">
</div>

【讨论】:

  • 但是您的代码仅将图像居中。图像还应拉伸到最大高度或宽度,同时保持纵横比,并且整个图像仍应可见。
  • 图片纵横比失真。
  • @DeepuReghunath 它不保留纵横比。
  • @atan 将解决方案更新为 overflow:hidden
  • @atan 如果您想将图像的高度保持为 100%,请添加此#img { height:100%; overflow:hidden; }
猜你喜欢
  • 1970-01-01
  • 2016-09-30
  • 2014-09-17
  • 1970-01-01
  • 2016-06-03
  • 2010-10-27
  • 2012-05-09
  • 2013-08-26
  • 1970-01-01
相关资源
最近更新 更多