vue项目里面状态闪烁

14 篇文章 0 订阅

 思路就是使用:style,时时改变透明度opacity,最后形成闪烁效果。此处共两种状态,绿色状态呼吸效果,红色状态快速闪烁。

html:

<span>状态</span>
                <span
                  class="point-state"
                  :class="item.state == 'online' ? 'outline' : 'online'"
                  :style="
                    item.state == 'online'
                      ? { opacity: changeOpcity1 }
                      : { opacity: changeOpcity }
                  "
                >
                </span>

 css:

.point-state {
      width: 12px;
      height: 12px;
      border-radius: 100%;
      background: #eee;
      margin-left: 10px;
    }
    .outline {
      background: #d9001b;
    }
    .online {
      background: #10c504;
      box-shadow: 1px 1px 10px #b5ffae;
    }

data:

// 控制绿灯闪烁
      changeOpcity: 1,
      flag: 1,
      // 控制红灯闪烁
      changeOpcity1: 1,
      flag1: 1,

js:

 mounted() {
    this.controlLight();
  },
  methods: {
    // 控制闪烁
    controlLight() {
      setInterval(() => {
        if (this.flag) {
          this.$nextTick(() => {
            this.changeOpcity = this.changeOpcity - 0.1;
          });
        } else {
          this.$nextTick(() => {
            this.changeOpcity = this.changeOpcity + 0.1;
          });
        }
        if (this.changeOpcity < 0.3) {
          this.flag = 0;
        } else if (this.changeOpcity > 0.99) {
          this.flag = 1;
        }
      }, 140);
      setInterval(() => {
        if (this.flag1) {
          this.$nextTick(() => {
            this.changeOpcity1 = this.changeOpcity1 - 0.1;
          });
        } else {
          this.$nextTick(() => {
            this.changeOpcity1 = this.changeOpcity1 + 0.1;
          });
        }
        if (this.changeOpcity1 < 0.3) {
          this.flag1 = 0;
        } else if (this.changeOpcity1 > 0.99) {
          this.flag1 = 1;
        }
      }, 30);
    },
}

效果图:

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值