Bhaskararao Gummidi
Bhaskararao Gummidi

Reputation: 2593

Vue js dynamic binding background image style not working

Background image not applying to this div when i was calling changeBgImage function on load.I searched through the web but not getting the desired result.Please help me

<div class="home_search" id="home_search" v-bind:style="{ background: `url(${imageUrl}) no-repeat center` }">
  <HeaderComponent />
</div>

changeBgImage () {
  let rand = Math.round(Math.random() * 4);
  this.imageUrl = "../assets/img/home_" + rand + ".jpg";
  console.log(this.imageUrl)

}

Upvotes: 1

Views: 637

Answers (1)

Un1
Un1

Reputation: 4132

You have to add width and height to the tag (or container)

Example: https://codepen.io/anon/pen/vzyjww?editors=1010

<div 
  :style="{ background: `url(${imageUrl}) no-repeat center` }" 
  style="width: 350px; height: 150px"
>
</div>

Upvotes: 2

Related Questions