vue背景色随机生成

背景色随机生成


背景色随机生成,写着玩的,打开定时,更好玩

在这里插入图片描述

<template>
  <div class="box">
    <!-- <el-input v-model="input" placeholder="请输入内容"></el-input> -->
    <div class="top">
      <div
        class="boxlist"
        v-for="(item, index) in lists"
        :key="index"
        v-bind:style="{
          background: item,
        }"
        @click="addcolor(item)"
      ></div>
    </div>
    <el-button type="primary" @click="add">主要按钮</el-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      Number: "",
      lists: [],
      input: "",
    };
  },
  mounted() {
    // 进入时调用
    this.randoms();
    // this.sss();
  },
  methods: {
    sss() {
      var that = this;
      setInterval(() => {
        that.randoms();
      }, 1000);
    },
    randoms() {
      this.lists = [];
      // 循环
      for (var i = 0; i < 27; i++) {
        // 把获取到的值赋值给 Number
        let R = Math.floor(Math.random() * 130 + 110);
        let G = Math.floor(Math.random() * 130 + 110);
        let B = Math.floor(Math.random() * 130 + 110);

        let R1 = Math.floor(Math.random() * 130 + 110);
        let G1 = Math.floor(Math.random() * 130 + 110);
        let B1 = Math.floor(Math.random() * 130 + 110);

        this.Number1 = "rgb(" + R + "," + G + "," + B + ")";
        this.Number2 = "rgb(" + R1 + "," + G1 + "," + B1 + ")";
        this.Number =
          "linear-gradient(120deg, " +
          this.Number1 +
          " 0%, " +
          this.Number2 +
          " 100%)";
        // 下舍入(0-1随机数 乘以 255)转换为16进制
        //   Math.floor(Math.random() * 255).toString(16) +
        //   Math.floor(Math.random() * 255).toString(16) +
        //   Math.floor(Math.random() * 255).toString(16);
        // 追加到 lists 中
        this.lists.push(this.Number);
      }
    },
    add() {
      // 点击清空 lists
      this.lists = [];
      // 调用封装函数
      this.randoms();
    },
    addcolor(item) {
      // 颜色在 input 框中显示
      this.input = item;
      // 背景色改变
      this.$refs.top.style.backgroundColor = item;
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.568);
  // background-color: rgb(196, 191, 191);
  .top {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    .boxlist {
      width: 150px;
      height: 150px;
      margin: 25px;
      p {
        width: 100%;
        height: 30px;
        background-color: #fff;
        line-height: 30px;
      }
    }
  }
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue中,可以通过动态绑定样式来实现随机颜色。根据提供的引用内容,可以有三种方法来实现随机颜色的效果。 第一种方法是通过指定颜色组中的纯背景色。你可以创建一个包含不同颜色的数组,然后使用Math.random()方法来生成一个随机索引,从颜色数组中选择一个颜色。将这个颜色应用到元素的背景色属性上即可。 第二种方法是通过RGB赋值。你可以使用Math.random()方法生成一个0到255之间的随机数,并将这个随机数分别赋值给RGB的红、绿、蓝三个分量。然后使用模板字符串将这些分量拼接成一个RGB颜色值,并将这个颜色值应用到元素的背景属性上。 第三种方法是通过指定颜色组中的渐变背景色。你可以创建一个包含不同渐变颜色的数组,然后使用Math.random()方法生成一个随机索引,从颜色数组中选择一个渐变颜色。将这个渐变颜色应用到元素的背景图像属性上即可。 具体实现的代码可以参考以下示例: 方法一(纯背景色): ```javascript data() { return { randomRgb() { let colorList = [ "#87CEFF", "#5CACEE", "#63B8FF", "#7EC0EE", "#A4D3EE", "#C6E2FF", "#B9D3EE" ]; let index = Math.floor(Math.random() * colorList.length); return { backgroundColor: colorList[index] }; } }; } ``` 方法二(RGB赋值): ```javascript data() { return { randomRgb() { let R = Math.floor(Math.random() * 130 + 110); let G = Math.floor(Math.random() * 130 + 110); let B = Math.floor(Math.random() * 130 + 110); return { background: `rgb(${R}, ${G}, ${B}, .5)` }; } }; } ``` 方法三(渐变背景色): ```javascript data() { return { randomRgb() { let colorList = [ "linear-gradient(120deg, #89f7fe 0%, #66a6ff 100%)", "linear-gradient(120deg, #22E1FF 0%, #1D8FE1 48%, #625EB1 100%)", "linear-gradient(120deg, #3D4E81 0%, #5753C9 48%, #6E7FF3 100%)", "linear-gradient(120deg, #209cff 0%, #68e0cf 100%)", "linear-gradient(120deg, #96deda 0%, #50c9c3 100%)", "linear-gradient(120deg, #007adf 0%, #00ecbc 100%)", "linear-gradient(120deg, #6a11cb 0%, #2575fc 100%)" ]; let index = Math.floor(Math.random() * colorList.length); return { backgroundImage: colorList[index] }; } }; } ``` 以上就是在Vue中实现随机颜色的三种方法。根据你的需求选择其中一种即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

唐飞滚滚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值