简体   繁体   English

使用Bootstrap 4在文本上覆盖图像

[英]Overlay Image with text with Bootstrap 4

I'm very new to web development so have mercy with your responses. 我是Web开发的新手,所以请留意您的回答。

I have a grid of images that I want to modify. 我有一个要修改的图像网格。 When the mouse hovers over an image, I want an overlay with text to appear over the image (this may require the cell in the grid to expand to contain all the text). 当鼠标悬停在图像上时,我希望在图像上出现一个带有文本的叠加层(这可能要求网格中的单元格展开以包含所有文本)。

When the image with the overlay is clicked, it should open a modal (I already have this working) with the full text and info inside. 单击带有叠加层的图像时,它应该打开一个包含全文和信息的模式(我已经在使用它了)。

All changes need to look smooth with transitions (overlay shouldn't just be there when the mouse touches, it should animate in, etc.) when they enter/exit. 当它们进入/退出时,所有更改都需要在过渡时看起来平滑(过渡不仅应该在鼠标触摸时出现,还应该在其中进行动画处理,等等)。

I'm not sure what the right terminology is for this, so I'm struggling to find info searching on Google. 我不确定该使用什么正确的术语,因此我正在努力寻找在Google上搜索的信息。 So, if you could provide me with some resources to learn this, or provide some examples, it'd be much appreciated. 因此,如果您可以为我提供一些学习的资源或提供一些示例,将不胜感激。 Thanks in advance :) 提前致谢 :)

Edit: Here's close to what I want to happen There will be an image, like this: 编辑:这接近于我要发生的事情会有一个图像,像这样: 网格中的图像

After the mouse hover over this image, an overlay should animate in to look like this: 鼠标悬停在此图像上之后,应当使覆盖层动画化,如下所示: 重叠显示在网格中图像的上方

The difference between this and what I want, is I want to show text instead of an icon, and I also want the cell in the grid upon which the mouse is hovering to expand to more pleasantly present the text that will be shown on the overlay. 这和我想要的东西之间的区别是,我想显示文本而不是图标,并且我还希望鼠标悬停在其上的网格中的单元格展开以更舒适地呈现将在覆盖层上显示的文本。

You can do this with just css first you need to wrap your image in a div and set the position to relative. 您可以仅使用CSS来执行此操作,首先需要将图像包装在div中并将位置设置为相对。 Then place the image and the overlay inside of it. 然后将图像及其叠加层放置在其中。 Then you can use css transitions to achieve the desired effect. 然后,您可以使用CSS过渡来达到所需的效果。 You will set the original opacity of the overlay to 0 and set the hover opacity to 1. Below is an example. 您将叠加层的原始不透明度设置为0,并将悬停不透明度设置为1。下面是一个示例。 Since you haven't posted any code I can't tell what your markup will be so I just made an example. 由于您尚未发布任何代码,因此我无法告知您的标记是什么,所以我举了一个例子。

 .img-container{ position:relative; display:inline-block; } .img-container .overlay{ position:absolute; top:0; left:0; width:100%; height:100%; background:rgb(0,170,170); opacity:0; transition:opacity 500ms ease-in-out; } .img-container:hover .overlay{ opacity:1; } .overlay span{ position:absolute; top:50%; left:50%; transform:translate(-50%,-50%); color:#fff; } 
 <div class="img-container"> <img src="https://placehold.it/300x300"> <div class="overlay"> <span>overlay content</span> </div> </div> 

Set image as "block" 将图像设置为“块”

 .img-container{ position:relative; display:inline-block; } .img-container img{ display:block } .img-container .overlay{ position:absolute; top:0; left:0; width:100%; height:100%; background:rgb(0,170,170); opacity:0; transition:opacity 500ms ease-in-out; } .img-container:hover .overlay{ opacity:1; } .overlay span{ position:absolute; top:50%; left:50%; transform:translate(-50%,-50%); color:#fff; } 
 <div class="img-container"> <img src="https://placehold.it/300x300"> <div class="overlay"> <span>overlay content</span> </div> </div> 

听起来好像您在寻找工具提示,请参阅https://getbootstrap.com/docs/4.0/components/tooltips/

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

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