vue3弹幕

效果图

 

<template>
  <div class="build-page">
    <div class="mivebidss">
      <div class="mive-box" ref="listbox">
        <div
          :style="item.style"
          v-for="(item, index) in buildList.list"
          :key="index"
          >{{ item.text }}</div>
      </div>
    </div>
    <div class="input-build">
      <input type="text" v-model="buildText" placeholder="发表你的弹幕吧!" @keyup.enter="sendBuild" />
      <button @click="sendBuild">发送</button>
    </div>
  </div>
</template>

<script lang="ts">
import { reactive, ref } from "@vue/reactivity";
import { onMounted } from "vue";
export default {
  setup() {
    let { buildText, buildList, sendBuild, listbox, times } = buildFan();
    return { buildText, buildList, sendBuild, listbox, times };
  },
};
function buildFan() {
  const buildText = ref("");
  const buildList: any = reactive({
    list: [],
  });
  const listbox = ref();
  let times:any = ref(null);
  let boxHeight: number;
  onMounted(() => {
    boxHeight = listbox.value.offsetHeight;
  });
  function sendBuild() {
    if (buildText.value != "") {
      buildList.list.push({
        text: buildText.value,
        style: {
          top: Math.random() * (boxHeight - 30 - 0) + 0 + "px",
          color: "#" + Math.random().toString(16).substr(2, 6).toUpperCase(),
          fontSize: Math.floor(Math.random() * (30 - 12) + 12) + "px",
          left: 600 + "px",
          setLefr: 600,
        },
      });
      if (times) clearInterval(times);
      times = setInterval(function () {
        for (let i = 0; i < buildList.list.length; i++) {
          buildList.list[i].style.setLefr -= Math.random() * 5 + 1;
          buildList.list[i].style.left = buildList.list[i].style.setLefr + "px";
          if (buildList.list[i].style.setLefr < -400) {
            buildList.list.splice(i, 1);
            i--;
          }
          if (buildList.list.length < 1) {
            clearInterval(times);
            times = null;
          }
        }
      }, 17);
    } else {
      buildText.value = "";
    }
    buildText.value = "";
  }

  return { buildText, buildList, sendBuild, listbox, times };
}
</script>

<style scoped>
.build-page {
  height: 100%;
  width: 100%;
}
.mivebidss {
  width: 600px;
  height: 350px;
  margin: 0 auto;
  background-color: oldlace;
  margin-bottom: 10px;
}
.mive-box {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}
.mive-box > div {
  position: absolute;
  left: 700px;
  overflow: hidden;
}
.input-build {
  display: flex;
  width: 600px;
  margin: 0 auto;
}
.input-build > input {
  flex: 1;
}
</style>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天气晚来秋~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值