vue動態(tài)綁定background的方法
background是background-color,background-image,background-repeat,background-attachment,background-position,background-size等屬性的縮寫。
這篇文章我用動態(tài)綁定background-image來舉例。
我們都知道普通的css中寫background-image是這樣的:
background-image: url("./登錄頁bg.png");
但在vue中用style寫background-image時無法顯示:
<div class="login-container" style="{ background-image: url("./登錄頁bg.png")}"></div>
為什么呢?答:因為這樣寫url會被解析成字符串,取不出來,所以需要動態(tài)的綁定,以下有三種寫法:
寫法一:
<div class="login-container" :style="{ backgroundImage: `url(${require('./登錄頁bg.png')})` }"> </div>
寫法二:
<div class="login-container" :style="{ backgroundImage: loginBackEcho.fileContext ? `url(${loginBackEcho.fileContext})` : `url(${loginUrl})`, }" ></div> // 簡寫script: props: { loginBackEcho: { type: Object, default: () => {}, }, }, data() { return { loginUrl: require("./登錄頁bg.png"), }; }
寫法三:
<div class="login-container" :style="{ backgroundImage: `url(${imgss})` }" ></div> // 簡寫script: import imgss from "./登錄頁bg.png"; data() { return { imgss: imgss, } }
到此這篇關于vue動態(tài)綁定background的文章就介紹到這了,更多相關vue動態(tài)綁定background內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue+openlayer5獲取當前鼠標滑過的坐標實現(xiàn)方法
在vue項目中怎么獲取當前鼠標劃過的坐標呢?下面通過本文給大家分享實現(xiàn)步驟,感興趣的朋友跟隨小編一起看看吧2021-11-11關于vue中路由的跳轉和參數(shù)傳遞,參數(shù)獲取
這篇文章主要介紹了關于vue中路由的跳轉和參數(shù)傳遞,參數(shù)獲取方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03vue2與vue3雙向數(shù)據(jù)綁定的區(qū)別說明
這篇文章主要介紹了vue2與vue3雙向數(shù)據(jù)綁定的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04