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

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>
主站蜘蛛池模板: 普洱| 丹凤县| 嘉定区| 乌兰察布市| 财经| 封开县| 双城市| 龙州县| 改则县| 陈巴尔虎旗| 出国| 西乌珠穆沁旗| 进贤县| 永寿县| 郸城县| 忻城县| 筠连县| 宝兴县| 泸西县| 册亨县| 长武县| 蓝田县| 贵阳市| 郯城县| 宝应县| 辽中县| 商南县| 海盐县| 龙泉市| 农安县| 大兴区| 出国| 沾益县| 蚌埠市| 福贡县| 清流县| 吐鲁番市| 江北区| 灵寿县| 利川市| 横山县|