javascript - How to get random element in Vue.js -


i have 3 hero images , content , want display them randomly when user refreshes page!

basically trying display random div using jquery on loading page, issue size of hero image large , using jquery, these 3 images start loading in dom affects speed of page.

is there solution in vue.js loading 1 specific div @ time, not 3 divs when user refreshes page!?

<div class="slider-item"> <img src="../../../static/img/slides/slide1.png" alt="hello">         <div class="carousel-caption">             <h2>lipsum heading</h2>                 <h4>lipsum dummy content body test dummy goes here.</h4>         </div>     </div> 

jquery code:

mounted() {  var random = math.floor(math.random() * $('.slider-item').length);  $('.slider-item').eq(random).show(); }, 

everything pretty straight forward. randomize link you've chosen in vue.

const app = new vue({    el: '#app',    data: {      images: [        'http://via.placeholder.com/350x150',        'http://via.placeholder.com/200x140',        'http://via.placeholder.com/200x100'      ],      selectedimage: ''    },    created () {      const idx = math.floor(math.random() * this.images.length);      this.selectedimage = this.images[idx]    }  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.js"></script>  <div id="app">    <img v-bind:src="selectedimage" />  </div>


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -