简体   繁体   English

angular2中的背景图像不透明度

[英]Background image opacity in angular2

I'm trying to change the opacity of a background image, by setting the RGBA. 我正在尝试通过设置RGBA来更改背景图像的不透明度。 However the opacity of the background image does not change. 但是,背景图像的不透明度不会改变。 Below is the code snippet i'm using with. 以下是我正在使用的代码段。

Changing the background image opacity this way works in HTML, but not when I try it with angular2. 以这种方式更改背景图像的不透明度可在HTML中使用,但是当我尝试使用angular2时不起作用。

import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    styles: [`

.background-image {
  z-index: -1;
  position: absolute;
  top: 0px;
  left: 0px;
  width: 100%;
  height: 100%;
  background-size: cover;
  background-position: center;
  background-color: rgba(0, 0, 0, 0.9);
},

`],
    template:
            `
            <h1>Hello Angular</h1>
              <div class="background-image" style="background-image : url('http://v4.pingendo.com/assets/photos/food/cover-1.jpg')">
              </div>
            `,
})
export class AppComponent { }

Just wanted to say thanks to everyone for jumping in an helping to solve the problem 只是想感谢大家为解决问题提供的帮助

Try CSS Image Opacity / Transparency like, 尝试使用CSS图像不透明度/透明度,

background-image {
    opacity: 0.4;
    filter: alpha(opacity=40); /* For IE8 and earlier */
}

You could use CSS Pseudo-elements, as in any html. 您可以像使用任何html一样使用CSS伪元素。 This is not related to Angular2. 这与Angular2无关。

Plunker 柱塞

@Component({
    selector: 'my-app',
    styles: [`
        .background-image{
          position: absolute;
          top: 0px;
          left: 0px;
          width: 100%;
          height: 100%;
          z-index:-2;
        }
        .background-image:after {
          content: ' ';
          z-index: -1;
          top: 0px;
          left: 0px;
          width: 100%;
          height: 100%;
          position: absolute;
          background-size: cover;
          background-position: center;
          background-color: rgba(0, 0, 0, 0.2);
        },
    `],
    template:
          `
            <h1>Hello Angular</h1>
            <div class="background-image" style="background-image : url('http://v4.pingendo.com/assets/photos/food/cover-1.jpg')">
            </div>
          `,
})
export class AppComponent { }

Just to close out the thread, I ended up having to manually darken the image in Photoshop :(. 只是为了结束线程,我最终不得不在Photoshop中手动使图像变暗:(。

Hopefully this isn't an angular 2 bug. 希望这不是一个有角度的2 bug。

I prefer not to go for opacity/filter. 我不想去不透明/过滤器。

I think best way to put opacity to the background image which support most of the browser is as follows: 我认为将不透明度添加到支持大多数浏览器的背景图像的最佳方法如下:

  1. Using img tag with 100% height and width with lowest z-index and add opacity or other css whatever you want. 使用具有100%高度和宽度的img标签以及最低的z-index,并添加不透明度或其他任何所需的CSS。 Using img tag gives it visibility for search engine as well. 使用img标签也可以使其在搜索引擎中可见。
  2. If I don't want search engine visibility then I use multiple background. 如果我不希望搜索引擎可见,那么我使用多个背景。

    background:background: rgba(255, 255, 255, 0.37),url('img/img.png'); 背景:背景:rgba(255,255,255,0.37),url('img / img.png');

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

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