简体   繁体   English

如何模拟 css 背景图像的行为,而不是使用在其相对容器内缩放的具有绝对定位的 div?

[英]How can I emulate the behaviour of a css background image, instead using a div with absolute positioning which scales within it's relative container?

I am trying to build a card that has a background image, that when clicked, darkens that background image and then displays a lighter, transparent div with text on top of it.我正在尝试构建一张具有背景图像的卡片,单击该卡片会使该背景图像变暗,然后显示一个更亮、透明的 div,上面有文本。 Both the darkening of the background card, and the appearance of the text div need to have transitions.无论是背景卡片的变暗,还是文本 div 的外观都需要有过渡。

I have managed to create this functionality, including transitions, using a container with relative positioning, that contains an image with absolute positioning: https://jsfiddle.net/marcuslv/kmbvxt14/我已经设法使用具有相对定位的容器创建了此功能,包括转换,其中包含具有绝对定位的图像: https://jsfiddle.net/marcuslv/kmbvxt14/

The issue with this solution is that I can't work out how to scale the image inside the container such that when it grows and shrinks, it adapts its size to the container.这个解决方案的问题是我不知道如何缩放容器内的图像,以便当它增长和缩小时,它会适应容器的大小。

I want it to behave like a css background-image if it had these properties:如果它具有以下属性,我希望它表现得像 css 背景图像:

  background-image: url('https://www.fillmurray.com/300/300');
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center top;

I have implemented a near equivalent solution using a css background-image instead of an img element as in the first fiddle: https://jsfiddle.net/marcuslv/knq3e7dg/我已经使用 css 背景图像而不是第一个小提琴中的 img 元素实现了一个几乎等效的解决方案: https://jsfiddle.net/marcuslv/knq3e7dg/

The issue with this solution is that I can't get the background image to transition to its darkened state - it transitions immediately which is a bit jarring - and more importantly, I will be using b64 images for this project, which are finicky to set as a css background-image and I would much prefer to use an img element.这个解决方案的问题是我无法让背景图像过渡到其变暗的 state - 它会立即过渡,这有点刺耳 - 更重要的是,我将在这个项目中使用 b64 图像,设置起来很挑剔作为 css 背景图像,我更喜欢使用 img 元素。

You can overlay a transparent area using the::before psuedo element then use the opacity which is animatable.您可以使用::before 伪元素覆盖透明区域,然后使用可动画化的不透明度。 To use an img tag rather than a background image, absolutely position the info box like the example below.要使用 img 标签而不是背景图像,绝对是 position 信息框,如下例所示。 I've also tweaked your JS我还调整了你的 JS

 window.onload = () => { const container = document.querySelector('.container'); container.addEventListener('click', () => { document.querySelector('.movieCard').classList.toggle('movieCardActive'); document.querySelector('.info').classList.toggle('infoActive'); }); }
 * { font-family: Arial; font-size: 14px; }.container { position: relative; max-width: 400px; min-width: 200px; height: 400px; margin: auto; border: 1px solid red; box-sizing: border-box; padding: 1rem; }.movieCard { cursor: pointer; background-color: #242424; height: 100%; padding: 0; box-sizing: border-box; }.movieCard img { width: 100%; }.movieCard::before { content: ''; position: absolute; inset: 0; background-color: #000; opacity: 0; margin: 1rem; transition: opacity 1s; }.movieCardActive.movieCard::before { opacity: 0.8; }.info { position: absolute; top: 2rem; left: 2rem; right: 2rem; padding: 1rem; color: white; background: rgba(204, 204, 204, 0.4); opacity: 0; transition: opacity 0.1s; }.infoActive { color: #e8e6e6; background: rgba(204, 204, 204, 0.4); opacity: 1; transition: opacity 0.1s; }
 <div class="container"> <div class="movieCard"> <img src='https://www.fillmurray.com/300/300'> <div class="info"> <div id="Title">Harry Potter, 2000</div> <div id="Genre">Fantasy, Adventure</div> <div id="Plot">A young wizard discovers a world of adventure.</div> <div id="Rated">PG</div> <div id="imdbRating">7.1</div> <div id="imdbVotes">9090</div> <div id="Runtime">127 mins</div> </div> </div> </div>

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

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