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

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>
主站蜘蛛池模板: 黄山市| 封开县| 土默特右旗| 五家渠市| 土默特右旗| 阿拉善右旗| 老河口市| 宁安市| 巢湖市| 白朗县| 深圳市| 南昌市| 翼城县| 济宁市| 永和县| 安化县| 勃利县| 梁山县| 东方市| 韩城市| 徐州市| 驻马店市| 高台县| 衡东县| 麻江县| 宣化县| 平顺县| 玛曲县| 黄山市| 北海市| 贡山| 沙坪坝区| 滦南县| 嘉祥县| 浏阳市| 福安市| 宾阳县| 德保县| 乳山市| 寿光市| 全椒县|