current position:Home>Vue Click to switch the background color randomly (small demo)

Vue Click to switch the background color randomly (small demo)

2022-04-29 14:06:46ohm_ coder

<template>
  <div class="pageBgc" @click="changeBgc" :style="{background:colorIndex}">
      <ul>
          <li> Click me to switch colors </li>
      </ul>
  </div>
</template>

<script>
export default {
 
  data() {
    return {
        colorSet : ['gold','skyblue','red'],
        colorIndex:0
    };
  },
 
  methods: {
      changeBgc(){
          let colorRender = Math.floor(Math.random() * this.colorSet.length); // random number 
          this.colorIndex = this.colorSet[colorRender]; // Get the colors in the array 
          
      }
  },
};
</script>

<style scoped lang="less">
    .pageBgc{
        width: 100%;
        height: 100vh;
        background: skyblue;
    }
</style>



 

copyright notice
author[ohm_ coder],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291229085530.html

Random recommended