简体   繁体   English

如何在图像或div区域上方放置RGBA背景?

[英]How to put RGBA background above the image or div area?

I need to use RGBA background above this grid. 我需要在此网格上方使用RGBA背景。 I used some of the jQuery to interect with div area. 我使用了一些jQuery与div区域相交。 But I don't know how to put this background-color in image while I'm using jQuery. 但是我不知道在使用jQuery时如何将背景色显示在图像中。 Is there a way I can do this using jQuery ? 有什么办法可以使用jQuery吗? or any other methods ? 或任何其他方法?

 // Grid function $(function() { $(".single_grid").hover(function() { var panelId = $(this).attr("data-panelId"); $("#" + panelId).css({}).toggle(); }) $(".single_grid").mouseover(function() { var imageId = $(this).attr("data-imageId"); $("#" + imageId).css({ opacity: "0.3" }) }) $(".single_grid").mouseleave(function() { var imageId = $(this).attr("data-imageId"); $("#" + imageId).css({ opacity: "1" }) }) }) 
 .grid_panel { width: 100%; height: auto; } #sub_grid_panel { height: 760px; margin: 0 auto; width: 1125px; } #sub_grid_panel li { float: left; } .single_grid { width: 280px; height: 200px; overflow: hidden; position: relative; top: 0px; left: 0px; } .image_hover_preview h3 { text-align: center; display: block; margin-bottom: 5px; font-weight: normal; font-family: 'RalewayLight'; color: #FF8500; } .image_hover_preview { position: absolute; top: 65px; width: 100%; display: none; } div.image_hover_preview ai { color: #333; font-size: 20px; padding: 5px 10px; background: #FF8500; color: #FFFFFF; } div.image_hover_preview { text-align: center; } 
 <li class="single_grid" data-panelId="panel1" data-imageId="image1"> <div class="grid_column_bar"> <img src="img/grid/grid1.jpg" alt="" id="image1"> <div class="image_hover_preview" id="panel1"> <h3>Lorem ipsum dolor</h3> <a href=""><i class="fa fa-search-plus"></i></a> <a href=""><i class="fa fa-link"></i></a> </div> </div> </li> <li class="single_grid" data-panelId="panel2" data-imageId="image2"> <div class="grid_column_bar"> <img src="img/grid/grid2.jpg" alt="" id="image2"> <div class="image_hover_preview" id="panel2"> <h3>Lorem ipsum dolor</h3> <a href=""><i class="fa fa-search-plus"></i></a> <a href=""><i class="fa fa-link"></i></a> </div> </div> </li> 

我在这张图片中使用了不透明度

I'm not entirely sure what you wanted since your code snippet didn't work at all. 由于您的代码段根本不起作用,因此我不确定您想要什么。

This changes the background color: 这将更改背景颜色:

$(element).css({
    backgroundColor:"rgba(50,50,50,.5)"
});

To put the color on top of the background image, you can use gradients: 要将颜色放在背景图像的顶部,可以使用渐变:

$(element).css({
    backgroundImage:"linear-gradient(rgba(50,50,50,.5),rgba(50,50,50,.5)),url(background.jpg)"
});

Personally, I'm a fan of this little box-shadow: inset trick. 就个人而言,我是这个小box-shadow: inset的粉丝box-shadow: inset技巧。

Just add a background like you normally would and on top of that, 只需像平常一样添加背景,然后,
add a box-shadow property like so : 添加一个box-shadow属性,如下所示:

.element {
  background-image: url(myBgImage.jpg);
  box-shadow: inset 0px 0px 0px 1000px rgba(51,51,51,0.3);
}  

Note: the spread-radius of 1000px is arbitrarily chosen. 注意:可以任意选择1000px的扩展半径。 Just make sure it exceeds the width/height of your element. 只要确保它超过元素的宽度/高度即可。

Box Shadow Generator 盒子阴影发生器

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

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