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

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>
主站蜘蛛池模板: 宝应县| 拜城县| 思南县| 葫芦岛市| 牙克石市| 普兰店市| 华阴市| 涪陵区| 章丘市| 扬州市| 盐池县| 毕节市| 芦溪县| 阿巴嘎旗| 兴文县| 饶河县| 湘西| 封开县| 永泰县| 弥勒县| 宜丰县| 曲松县| 巢湖市| 锦屏县| 海兴县| 浦城县| 宜宾市| 孟连| 伊春市| 于田县| 广灵县| 正镶白旗| 杭锦后旗| 叶城县| 德安县| 泾川县| 措勤县| 青岛市| 锡林郭勒盟| 清徐县| 武功县|