VUE游戏九宫格

<!-- 九宫格 -->
<template>
  <div class="jiu">
    <div class="content">
      <div class="head">
        <div class="icon" @click="backFn">
          <i class="iconfont icon-fanhui1"></i>
        </div>
        <div class="txt">榜单</div>
      </div>
      <div class="img">
        <img src="../../assets/images/activity/txt.png" alt />
      </div>
      <div class="tip">
        <div class="tip-txt">刚刚用户铃**小,抽中了小米手机</div>
      </div>
      <div class="wrap">
        <div class="jifen">我的积分96756</div>
      </div>
      <div class="pan">
        <div
          class="item"
          v-for="item in  [1,2,3,4,5,6,7,8,9]"
          :key="item"
          :class="[index===item && item!==5?'active':'',item===5?'anniu':'']"
        >
          <div class="img" v-if="item!==5">
            <img :src="require('../../assets/images/activity/l'+item+'.png')" alt />
          </div>
          <div class="now" v-if="item===5" @click="startLottery">立即抽奖</div>
          <div class="n-txt" v-if="item===5">消耗1500积分</div>
          <div class="txt" v-if="item!==5">{{item}}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: "积分转盘",
      index: 0, // 当前转动到哪个位置,起点位置
      count: 10, // 总共有多少个位置
      timer: 0, // 每次转动定时器
      speed: 200, // 初始转动速度
      times: 0, // 转动次数
      cycle: 50, // 转动基本次数:即至少需要转动多少次再进入抽奖环节
      prize: -1, // 中奖位置
      click: true,
      showToast: false,
      toastType: "luck"
    };
  },
  //生命周期 - 创建完成(访问当前this实例)
  created() {},
  //生命周期 - 挂载完成(访问DOM元素)
  mounted() {},
  methods: {
    backFn() {
      this.$getBackPathData();
    },
    // 开始抽奖
    startLottery() {
      if (!this.click) {
        //避免多次点击
        return;
      }
      this.closeToast(); //关闭提示
      this.speed = 200; //初始化速度
      this.click = false; //重新开始点击(这里要优化)
      this.startRoll(); //开始转动
    },

    // 开始转动
    startRoll() {
      this.times += 1; // 转动次数
      this.oneRoll(); // 转动过程调用的每一次转动方法,这里是第一次调用初始化
      // 如果当前转动次数达到要求 && 目前转到的位置是中奖位置
      if (this.times > this.cycle + 10 && this.prize === this.index) {
        clearTimeout(this.timer); // 清除转动定时器,停止转动
        this.prize = -1;
        this.times = 0;
        this.click = true;
        this.showToast = true;
        this.toastType = "comeOn";
        console.log("你已经中奖了");
      } else {
        if (this.times < this.cycle) {
          this.speed -= 10; // 加快转动速度
        } else if (this.times === this.cycle) {
          // 随机获得一个中奖位置(调接口获取)
          const index = parseInt(Math.random() * 10, 0);
          this.prize = index;
          //   if (this.prize > 7) {
          //     this.prize = 7;
          //   }
          console.log(`中奖位置${this.prize}`);
        } else if (
          this.times > this.cycle + 10 &&
          ((this.prize === 0 && this.index === 9) ||
            this.prize === this.index + 1)
        ) {
          this.speed += 110;
        } else {
          this.speed += 20;
        }
        if (this.speed < 40) {
          this.speed = 40;
        }
        this.timer = setTimeout(this.startRoll, this.speed);
      }
    },

    // 每一次转动
    oneRoll() {
      let index = this.index; // 当前转动到哪个位置
      const count = this.count; // 总共有多少个位置
      index += 1;
      if (index > count - 1) {
        index = 0;
      }
      this.index = index;
    },
    // 关闭弹出框
    closeToast() {
      this.showToast = false;
    }
  }
};
</script>
<style scoped lang="scss">
/* @import url(); 引入css类 */
.jiu {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  height: 100%;
  background: linear-gradient(
    159deg,
    rgba(22, 3, 50, 1) 0%,
    rgba(38, 0, 65, 1) 100%
  );
  .content {
    background: url("../../assets/images/activity/bgjiu.png") no-repeat;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    height: 100%;
    .head {
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 0 30px;
      position: relative;
      height: 98px;
      box-sizing: border-box;
      .icon {
        position: absolute;
        left: 30px;
        > .iconfont {
          font-size: 70px;
          color: #ffffff;
        }
      }
      .txt {
        font-size: 32px;
        font-weight: bold;
        color: #ffffff;
      }
    }
    .img {
      text-align: center;
      padding-top: 20px;
      img {
        width: 646px;
        height: 122px;
      }
    }
    .tip {
      display: flex;
      justify-content: center;
      padding-top: 24px;
      padding-bottom: 30px;
      .tip-txt {
        width: 546px;
        height: 50px;
        background: rgba(255, 255, 255, 0.22);
        border-radius: 8px;
        font-size: 28px;
        color: rgba(254, 189, 215, 1);
        text-align: center;
        line-height: 50px;
      }
    }
    .wrap {
      display: flex;
      justify-content: center;
      .jifen {
        width: 366px;
        height: 60px;
        background: linear-gradient(
          84deg,
          rgba(213, 64, 114, 0) 0%,
          rgba(200, 56, 114, 0.98) 51%,
          rgba(232, 88, 123, -0.16) 100%
        );
        text-align: center;
        line-height: 60px;
        color: #ffffff;
      }
    }
    .pan {
      margin: 30px 16px;
      width: 716px;
      height: 716px;
      background: url("../../assets/images/activity/abg.png") no-repeat;
      background-size: 100% 716px;
      display: flex;
      justify-content: space-around;
      flex-wrap: wrap;
      box-sizing: border-box;
      padding: 62px 60px;
      .item {
        width: 186px;
        height: 186px;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        background: url("../../assets/images/activity/chang.png") no-repeat
          center/100%;
        &.active {
          background: url("../../assets/images/activity/zhong.png") no-repeat
            center/100%;
        }
        &.anniu {
          background: url("../../assets/images/activity/chou.png") no-repeat
            center/100%;
        }
        .img {
          img {
            width: 88px;
            height: 88px;
          }
        }
        .txt {
          font-size: 26px;
          color: rgba(127, 62, 62, 1);
          line-height: 73px;
        }
        .now {
          font-size: 36px;
          font-weight: bold;
          color: rgba(236, 64, 46, 1);
          line-height: 72px;
        }
        .n-txt {
          font-size: 22px;
          color: rgba(236, 64, 46, 1);
        }
      }
    }
  }
}
</style>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现九宫格拖动播放视频需要使用 Vue 和一些第三方插件。以下是一个简单的实现步骤: 1. 安装 vue-grid-layout 插件,该插件可以帮助我们实现可拖动的九宫格布局。 ``` npm install vue-grid-layout --save ``` 2. 在 Vue 组件中引入 vue-grid-layout,并初始化网格布局。具体可以参考 vue-grid-layout 文档。 ```html <template> <vue-grid-layout :layout="layout" :col-num="3" :row-height="100" :is-draggable="true" :is-resizable="false"> <div v-for="(video, index) in videos" :key="index" :data-grid="{x: video.x, y: video.y, w: video.w, h: video.h}"> <video :src="video.src" controls></video> </div> </vue-grid-layout> </template> <script> import VueGridLayout from 'vue-grid-layout' export default { components: { VueGridLayout, }, data() { return { layout: [], videos: [ { src: 'video1.mp4', x: 0, y: 0, w: 1, h: 1 }, { src: 'video2.mp4', x: 1, y: 0, w: 1, h: 1 }, { src: 'video3.mp4', x: 2, y: 0, w: 1, h: 1 }, { src: 'video4.mp4', x: 0, y: 1, w: 1, h: 1 }, { src: 'video5.mp4', x: 1, y: 1, w: 1, h: 1 }, { src: 'video6.mp4', x: 2, y: 1, w: 1, h: 1 }, { src: 'video7.mp4', x: 0, y: 2, w: 1, h: 1 }, { src: 'video8.mp4', x: 1, y: 2, w: 1, h: 1 }, { src: 'video9.mp4', x: 2, y: 2, w: 1, h: 1 }, ], } }, created() { // 初始化九宫格布局 this.layout = this.videos.map(video => { return { x: video.x, y: video.y, w: video.w, h: video.h, i: String(this.videos.indexOf(video)) } }) }, } </script> ``` 3. 在 video 标签中添加 controls 属性,可以在视频上添加默认的播放控件。 4. 在组件中处理视频的播放暂停事件。可以使用 videojs 插件,该插件提供了丰富的视频控制 API。 ```html <template> <vue-grid-layout :layout="layout" :col-num="3" :row-height="100" :is-draggable="true" :is-resizable="false"> <div v-for="(video, index) in videos" :key="index" :data-grid="{x: video.x, y: video.y, w: video.w, h: video.h}"> <video ref="videoPlayer" :src="video.src" controls></video> </div> </vue-grid-layout> </template> <script> import VueGridLayout from 'vue-grid-layout' import videojs from 'video.js' export default { components: { VueGridLayout, }, data() { return { layout: [], videos: [ { src: 'video1.mp4', x: 0, y: 0, w: 1, h: 1 }, { src: 'video2.mp4', x: 1, y: 0, w: 1, h: 1 }, { src: 'video3.mp4', x: 2, y: 0, w: 1, h: 1 }, { src: 'video4.mp4', x: 0, y: 1, w: 1, h: 1 }, { src: 'video5.mp4', x: 1, y: 1, w: 1, h: 1 }, { src: 'video6.mp4', x: 2, y: 1, w: 1, h: 1 }, { src: 'video7.mp4', x: 0, y: 2, w: 1, h: 1 }, { src: 'video8.mp4', x: 1, y: 2, w: 1, h: 1 }, { src: 'video9.mp4', x: 2, y: 2, w: 1, h: 1 }, ], } }, created() { // 初始化九宫格布局 this.layout = this.videos.map(video => { return { x: video.x, y: video.y, w: video.w, h: video.h, i: String(this.videos.indexOf(video)) } }) }, mounted() { // 初始化视频播放器 this.videos.forEach((video, index) => { const videoPlayer = videojs(this.$refs[`videoPlayer${index}`], {}, function onPlayerReady() { videoPlayer.on('play', () => { console.log('Video is playing') }) videoPlayer.on('pause', () => { console.log('Video is paused') }) }) }) }, } </script> ``` 注意:上述代码中的 `videojs` 插件需要单独安装。 ``` npm install video.js --save ``` 以上就是使用 Vue 和第三方插件实现九宫格拖动播放视频的基本步骤。当然,您可以根据自己的需求进行扩展和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值