简体   繁体   English

HTML / CSS / jQuery:背景图像不透明度

[英]HTML/CSS/jQuery: background-image opacity

Let's say I have a div, and that div should have a background-image:url(foobar.png). 假设我有一个div,那个div应该有一个background-image:url(foobar.png)。 Also, however, foobar.png's opacity should be set to 40% so that the background image is translucent. 但是,foobar.png的不透明度应设置为40%,以便背景图像是半透明的。 How can I do this? 我怎样才能做到这一点?

If this is not possible without JavaScript, is there an example script I can refer to? 如果没有JavaScript这是不可能的,那么我可以参考一个示例脚本吗? Something like this? 像这样的东西?

jQuery.fn.fadedBgImg = function(url, opacity) {

    // Create block element that fills `this` element

    // Set z-index of said element to lowest

    // Set opacity of said element to 40%

    // Insert said element into parent
}

Use CSS to set the opacity: 使用CSS设置不透明度:

.translucent {
    opacity: 0.4;
    filter: alpha(opacity = 40); /* For IE */
}

Edit: 编辑:

Yes, opacity sets the opacity for the entire element, not just the content. 是的, opacity设置整个元素的不透明度,而不仅仅是内容。 To work around this, you can have the content overlay the background and wrap them both in a common parent: 要解决此问题,您可以让内容覆盖背景并将它们包装在一个共同的父级中:

<div id="container">
  <div id="background" class="translucent"></div>
  <div id="content"></div>
</div>

And use CSS like this: 并使用这样的CSS:

#container {
    position: relative;
    width: 200px;
    height: 200px;
}

#background, #content {
    position: absolute;
    top: 0;
    left: 0;
}

Try this .. Little changes in the HTML structure .. and instead of using background image use normal image and set it backwards with low opacity. 试试这个.. HTML结构的细微变化..而不是使用背景图像使用普通图像并向后设置低不透明度。

 .my_div{width:300px;height:300px;position:relative;} .my_div div.back_image{display:block; position:absolute; width: 100%; height:100%;top:0px ; left:0px;opacity:0.8;z-index:1;} .my_div div.back_image img {width: 100%; height:100%;} .my_div div.front_text{position:absolute; width: 100%; height:100%;top:0px ; left:0px;opacity:1;z-index:99;padding:10px;color:#ffffff;box-sizing: border-box;} 
 <div class="my_div"> <div class="back_image"><img src="https://newevolutiondesigns.com/images/freebies/black-wallpaper-10.jpg"></div> <div class="front_text"> <h2>Testing Title</h2> <p>Lorem Ipsum content testing. This is Prakash Rao </p> </div> </div> 

I find this is the easiest method: In your graphics editor, set the transparency on foobar.png to 60% and save it as a 24 bit png file. 我发现这是最简单的方法:在图形编辑器中,将foobar.png上的透明度设置为60%并将其保存为24位png文件。 If you need to serve this document to IE6 and don't want to use a png fix, this isn't a solution. 如果您需要将此文档提供给IE6并且不想使用png修复,则这不是解决方案。

Otherwise, opacity in web browsers is such an annoying thing to deal with in terms of cross-browser support, and dealing with child elements becoming transparent is a typical issue as I recall. 否则,在跨浏览器支持方面,Web浏览器中的不透明性是一件非常烦人的事情,处理子元素变得透明是我记忆中的一个典型问题。

Unfortunately I don't have any scripts that solve this handy. 不幸的是,我没有任何解决这个问题的脚本。

edit: I see you edited, and I can tell you're not as unaware of what you're doing as I originally expected. 编辑:我看到你编辑了,我可以告诉你并不像我原先预料的那样不知道你在做什么。 Don't be offended if my advice seems a little elementary, haha. 如果我的建议看起来有点基本,请不要被冒犯,哈哈。

While there's no direct support for setting background-image opacity, you can specify multiple background-image values , which are rendered with the first one closest to the viewer. 虽然没有直接支持设置背景图像不透明度,但您可以指定多个背景图像值 ,这些使用与查看器最接近的第一个进行渲染。

Thus, if you are for some strange reason unable to simply edit the opacity of the image in an image editor and serve up the modified image, you could try something like: 因此,如果您出于某种奇怪的原因无法在图像编辑器中简单地编辑图像的不透明度并提供修改后的图像,您可以尝试以下方法:

semitransparent = "images/white50.png"; //a 1x1 pixel image, white at 50% transparency.
myElement.style.backgroundImage = "url('"+semitransparent+"'), url('"+backgroundImage+"')";

This example assumes you're generally rendering over a white page background; 此示例假设您通常渲染白页背景; you may wish to change the color of the semitransparent image if you're trying to simulate semitransparency with some other color partially bleeding "through." 你可能希望改变半透明图像的颜色,如果你试图模拟半透明,其他颜色部分出血“通过”。

 In IE, a !DOCTYPE must be added for the :hover selector to work on other elements than the a element.



img { opacity: 0.4; filter: alpha(opacity=40); //forIE* and earlier

img:hover { opacity: 1.0; filter: alpha(opacity=100); //forIE* and earlier

<img src="klematis.jpg" width="150" height="113" alt="klematis">

<img src="klematis2.jpg" width="150" height="113" alt="klematis">

尝试使用css更改不透明度:

opacity:0.4

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

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