简体   繁体   English

如何在当前未悬停的所有其他图像上显示图像叠加?

[英]How can I display an image overlay on all other images that are not currently being hovered over?

I have this rendering code in my component (this renders an unordered list of li elements that each have their own image):我的组件中有这个渲染代码(这会渲染一个li元素的无序列表,每个元素都有自己的图像):

<div className="thumbnail-photos-wrapper">
  {spot.thumbnail_image_urls ? spot.thumbnail_image_urls.map(url => 
    <div className="thumbnail-image-wrapper">
      <img src={url} alt="Thumbnail photo" />
    </div>) : ''}
</div>

And when I'm hovering over one of these images, I'd like for the other images to be grayed out.当我将鼠标悬停在这些图像之一上时,我希望其他图像变灰。 I've found solutions about graying/blacking out the entire background, but that's not quite what I need;我找到了有关使整个背景变灰/变黑的解决方案,但这并不是我所需要的; I'd only like the images that are not being hovered over grayed out.我只希望没有悬停在灰色上的图像。

I've also read that it's better to use states to interact with HTML elements in a JS/React app, however, I don't think that would work.我还读到过在 JS/React 应用程序中使用状态与 HTML 元素交互会更好,但是,我认为这行不通。 If I had dynamic HTML classes in the render code above that depended on toggling the state, it would toggle all of the class names upon rerender (effectively changing all the class names for an image wrapper if the state was true or false; I only want to change the appearance of the non hovered images!).如果我在上面的渲染代码中有动态 HTML 类依赖于切换状态,它会在重新渲染时切换所有类名(如果状态为真或假,则有效地更改图像包装器的所有类名;我只想要更改非悬停图像的外观!)。

I've also thought about doing something like this:我也想过做这样的事情:

<div className="thumbnail-photos-wrapper">
  {spot.thumbnail_image_urls ? spot.thumbnail_image_urls.map(url => 
    <div className="thumbnail-image-wrapper">
      <div className="overlay-wrapper"></div>
      <img src={url} alt="Thumbnail photo" />
    </div>) : ''}
</div>

Where I have a separate div to hold the overlay.我有一个单独的 div 来保存覆盖层。 I was thinking that I could have this overlay-wrapper initially have a display: none (or something else to that effect), and then when the user hovers over an image, it becomes visible.我想我可以让这个overlay-wrapper最初有一个display: none (或其他类似效果),然后当用户将鼠标悬停在图像上时,它变得可见。 However, I believe that this would also put an overlay on the hovered image, which is not what I want.但是,我相信这也会在悬停的图像上叠加一个叠加层,这不是我想要的。

Help is appreciated!帮助表示赞赏!

Update更新

I've added a CSS filter with a short transition as an idea for a straightforward overlay, in place of changing opacity .我添加了一个带有短过渡的 CSS filter ,作为直接覆盖的想法,而不是改变opacity


At the risk of oversimplifying this, you can use CSS negation to handle this task.冒着过于简单化的风险,您可以使用 CSS 否定来处理此任务。

From MDN :来自MDN

The :not() CSS pseudo-class represents elements that do not match a list of selectors. :not() CSS 伪类表示与选择器列表不匹配的元素。 Since it prevents specific items from being selected, it is known as the negation pseudo-class .由于它阻止选择特定项目,因此称为否定伪类

 .thumbnail-photos-wrapper:hover .thumbnail-image-wrapper:not(:hover) { filter: brightness(.33); } .thumbnail-image-wrapper { transition: filter 0.3s ease-in-out; } /* For styling puroses only — ignore */ .thumbnail-photos-wrapper { display: inline-block; }
 <div class="thumbnail-photos-wrapper"> <div class="thumbnail-image-wrapper"> <img src=http://placekitten.com/50/50 alt="Thumbnail photo" /> </div> <div class="thumbnail-image-wrapper"> <img src=http://placekitten.com/50/50 alt="Thumbnail photo" /> </div> <div class="thumbnail-image-wrapper"> <img src=http://placekitten.com/50/50 alt="Thumbnail photo" /> </div> </div>

jsFiddle js小提琴

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

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