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,
}
}到此這篇關(guān)于vue動態(tài)綁定background的文章就介紹到這了,更多相關(guān)vue動態(tài)綁定background內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vite3 Svelte3構(gòu)建Web應(yīng)用報錯process is not def
這篇文章主要介紹了Vite3 Svelte3構(gòu)建Web應(yīng)用報錯:'process is not defined'解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Vue3訪問頁面時自動獲取數(shù)據(jù)的方法實現(xiàn)
本文介紹了在Vue3中如何利用生命周期鉤子函數(shù)和定時器實現(xiàn)訪問頁面時自動獲取數(shù)據(jù)的方法,這種方法適用于需要在頁面加載時即時更新數(shù)據(jù)顯示的場景,感興趣的可以了解一下2024-11-11
element的el-date-picker組件實現(xiàn)只顯示年月日時分效果(不顯示秒)
最近遇到這樣的需求使用element的el-date-picker組件,只顯示時分,不顯示秒,下面小編給大家分享element的el-date-picker組件實現(xiàn)只顯示年月日時分效果,感興趣的朋友一起看看吧2024-08-08
vue實現(xiàn)在一個方法執(zhí)行完后執(zhí)行另一個方法的示例
今天小編就為大家分享一篇vue實現(xiàn)在一個方法執(zhí)行完后執(zhí)行另一個方法的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
daisyUI解決TailwindCSS堆砌class問題詳解
這篇文章主要為大家介紹了daisyUI解決TailwindCSS堆砌class問題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07

