官术网_书友最值得收藏!

Style binding (continued)

Coming back to our header image, we can use thev-binddirective with thestyleargumentto bind a value to thestyleattribute.

index.html:

<p class="header-img" v-bind:style="headerImageStyle"></p>

headerImageStyleis an expression that evaluates to a CSS rule that sets the background image to the correct URL. It sounds very confusing, but when you see it working, it will be quite clear.

Let's now createheaderImageStyleas a data property. When binding to a style attribute, you can use an object where the properties and values are equivalent to the CSS properties and values.

app.js:

data: {
  ...
  headerImageStyle: {
    'background-image': 'url(sample/header.jpg)'
  }
},

Save the code, refresh the page, and the header image will be shown:

Figure 2.6. Page including header image

Inspect the page with your browser Dev Tools and notice how thev-binddirective has evaluated:

<p class="header-img" style="background-image: url('sample/header.jpg');"></p>
主站蜘蛛池模板: 庆阳市| 天水市| 威海市| 博客| 通化市| 尤溪县| 兴国县| 石家庄市| 屏东县| 红原县| 舟山市| 铜川市| 栾城县| 洞口县| 车致| 都江堰市| 金溪县| 都江堰市| 辛集市| 镶黄旗| 龙山县| 灵武市| 交城县| 金川县| 禹城市| 湘西| 辛集市| 房产| 卓资县| 阿荣旗| 科技| 合水县| 绥德县| 托里县| 怀柔区| 延长县| 呈贡县| 巴林左旗| 城市| 罗江县| 蕲春县|