js+css动画实现横向滚动抽奖效果实现

 代码如下: 

<template>

   <div>
    <div class="box" id="parent-width">
      <el-icon class="box-icon">
        <CaretBottom/>
      </el-icon>
      <div class="all-list" :style="animation">
        <div class="list-item" v-for="item in 49">{{ item }}</div>
      </div>
      <el-icon class="box-icon">
        <CaretTop/>
      </el-icon>
    </div>
    当前中奖下标: 30
    <el-button type="primary" @click="start">开 始</el-button>
  </div>
</template>

<script>
export default {
  name: 'test',
  data(){
    return{
      animation:{},
    }
  },
  mounted() {

  },
  methods: {
    start(){
        //计算平移位置  (奖池长度 * 盒子宽度) + (奖池长度 * 外边距) + ((中奖下标 - 奖池长度) * (盒子宽度 + 外边距)) - (父盒子总宽度 / 2) + (盒子宽度 + 外边距)/2
      let jcLength = 49;  // 奖池数组长度
      let boxWidth = 80; // 盒子宽度
      let marginWidth = 20; //盒子之间的间隔
      let parentWidth = document.getElementById('parent-width').offsetWidth; //父盒子总宽度
      let result = (jcLength * boxWidth) + (jcLength * marginWidth) + ((30 - jcLength) * (boxWidth + marginWidth)) - (parentWidth /2) + (boxWidth + marginWidth)/2; //这里默认中奖下标是30
      this.animation = {
          transform: `translate(-${result}px)`,
          transitionDuration: '3s'
      }
      setTimeout(()=>{
        this.animation = {
            transform: "translateX(0) translateY(0)",
            willChange: "transform"
          }
      },6000) //三秒后回到初始位置
    },
  }
};
</script>


<style lang="scss" scoped>
.box, #parent-width{
  width: 600px;
  height: 200px;
  background: rgba(43,55,61,0.37);
  overflow: auto;
  display: flex;
  flex-direction: column
}
.box-icon{
  margin: 0 auto;
}
.all-list{
  width: 7000px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-start;
  overflow-x: auto;
}

.list-item{
  width: 80px;
  height: 100px;
  background: #2c7ef8;
  margin: 10px;
}

</style>

 实现效果如下:

 

  • 15
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值