- Full-Stack Vue.js 2 and Laravel 5
- Anthony Gore
- 127字
- 2021-07-02 19:57:20
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>
推薦閱讀
- Building a Game with Unity and Blender
- 程序員面試筆試寶典
- C語言程序設計(第2版)
- Cassandra Data Modeling and Analysis
- Redis Essentials
- 計算機應用基礎教程(Windows 7+Office 2010)
- 精通MySQL 8(視頻教學版)
- SciPy Recipes
- Building Serverless Architectures
- ActionScript 3.0從入門到精通(視頻實戰版)
- 零基礎看圖學ScratchJr:少兒趣味編程(全彩大字版)
- 超簡單:用Python讓Excel飛起來(實戰150例)
- 金融商業數據分析:基于Python和SAS
- 自己動手構建編程語言:如何設計編譯器、解釋器和DSL
- Swift 2 Design Patterns