角动态背景图像

时间:2016-06-01 17:37:47

标签: css ipad angular styles

在html模板中,我有这种风格的动态图像:

<div style="background: url('/img/{{item.img}}'); width: 200px; height: 150px"></div>

适用于网络浏览器和Android浏览器。但是,使用“style =”动态的背景图像不会在iPad上显示。

我总是可以使用img标签创建动态图像,但我正在为iPad寻找样式/ css解决方案。

4 个答案:

答案 0 :(得分:57)

改为使用

<div [ngStyle]="{background: 'url(/img/' + item.img + ')', width: '200px', height: '150px'"></div>

<div [style.background]="'url(/img/' + item.img + ')'"
    [style.width.px]="200" [style.height.px]="150p"></div>

另见In RC.1 some styles can't be added using binding syntax

答案 1 :(得分:9)

你应该这样做,因为+GünterZöchbauer第二部分答案会产生不需要的补充风格背景位置:初始和背景大小:至少在Safari上初始化。

请注意,我只设置了背景图片样式:

questions

答案 2 :(得分:3)

<div id="component-id" [style.background-image]="'url(www.domain.com/path/'+bgImageVariable+')'">
    <!-- ... -->
</div>

<div id="component-id" [style.background-image]="'url('+bgImageVariable+')'">
    <!-- ... -->
</div>

您可以通过在单个变量中添加URL路径来进行第二种使用。 例如

bgImageVariable="www.domain.com/path/img.jpg";

答案 3 :(得分:0)

使用它并根据需要设置div标签的高度和宽度

<div id="component-id" [style.backgroundImage]="'url('+bgImageVariable+')'"></div>