点击页面随机生成不同颜色的点点

19 篇文章 0 订阅
12 篇文章 1 订阅
<template>
  <div @click="setRandom($event)">这是about页面
    <span ref="circle" class="circle" :style='style'></span>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        style: {}
      };
    },
    created() {},
    mounted() {},
    beforeRouteEnter(to, from, next) {
      console.log(to)
      console.log(from)
      if (from.name == 'Home') {
        console.log('从Home页面进入')
      } else if (from.name == 'Second') {
        console.log('从second页面进入')
        alert('从second页面进入')
      }
      next()
    },
    methods: {
      setRandom(event) {
        // 设置随机数字
        let random = Math.round(Math.random() * 100);
        // 设置随机颜色
        let color = this.setColor()
        var e = event || window.event;
        let left = e.clientX 
        let top = e.clientY

        var style = {
          width: `${random}px`,
          height: `${random}px`,
          background: `rgb${color}`,
          left:`${left}px`,
          top:`${top}px`
        }
        this.style = style
      },
      setColor() {
        //rgb颜色随机
        var r = Math.floor(Math.random() * 256);
        var g = Math.floor(Math.random() * 256);
        var b = Math.floor(Math.random() * 256);
        var rgb = '(' + r + ',' + g + ',' + b + ')';
        return rgb;
      }
    }

  };
</script>
<style scoped>
  div {
    width: 1000px;
    height: 1000px;
    position: relative;
  }

  .circle {
    display: block;
    border-radius: 50%;
    position: absolute;
  }
</style>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值