小程序中实现最简转盘

在这里插入图片描述
html

		<view class="" style="position: relative;">
			<image src="http://jzconfigure.oss-cn-beijing.aliyuncs.com/serviceItem/start_202004171346.png" bindtap="start" class="goImg"></image>
			<image src="http://jzconfigure.oss-cn-beijing.aliyuncs.com/serviceItem/%E7%BB%84%202076%403x%20(1)_202004171343.png" class="bgImg" style="transform:{{'rotate(' + rotate + 'deg)'}};"></image>
			<view class="contain" style="transform:{{'rotate(' + rotate + 'deg)'}};">
				<view wx:for="{{prizeList}}" class="couItem" wx:key="index" style="">
					<view class="couName">{{item.prizeName}}</view>
					<image src="{{item.imageUrl}}" class="couponIMg"></image>
				</view>
			</view>

		</view>

js
在这里插入图片描述

 // data里面数据
roate:0,
prizeList:[
          {id: 442, imageUrl: "http://jz.yusucar.com/serviceItem/c4ae945507c3584197a55944a837c481_202004161321.png", prizeName: "🎧耳机"},
          {id: 443, imageUrl: "http://jz.yusucar.com/serviceItem/7cc2c1393f902206c3b4dcf736c66ae0_202004161321.png", prizeName: "车衣" },
          {id: 444, imageUrl: "http://jz.yusucar.com/serviceItem/e48add6019d2a3ac08df6d23688ec4de_202004161322.png", prizeName: "摆件"},
          {id: 445, imageUrl: "http://new-store.oss-cn-beijing.aliyuncs.com/serviceItem/10_202004091328.png", prizeName: "20元优惠券"},
          {id: 446, imageUrl: "http://new-store.oss-cn-beijing.aliyuncs.com/serviceItem/10_202004091328.png", prizeName: "10元优惠券"},
          {id: 447, imageUrl: "http://jzconfigure.oss-cn-beijing.aliyuncs.com/serviceItem/cry_202004150950.png", prizeName: "谢谢参与"}
        ],

 rand(n, m) {
    var c = m - n + 1;
    return Math.floor(Math.random() * c + n);
  },
start() {
    console.log( this.data.count )
 
    if( this.data.count <= 0){
      wx.showToast({
        title: '您已经参与过该活动',
        icon:'none'
      })
      return;
    }
    console.log(this.flag)
      if( this.flag ){     //flag  是判断不要点击多次  不然会多次触发接口
        return;
      }
      this.flag = true
      this.apiLoding = setInterval(() => {   //设置定时器   目的是为了点击后就开始转  不然等请求完会有延迟
        this.setData({
          rotate: this.data.rotate + 15,  //  这里旋转角度 加随机数  
        })
      }, 100)
      console.log('请求中...')
      app.getApi({
        url: `Activity/getdrawprize?orderId=${this.data.orderId}`,		//这是请求接口操作  会返回哪个中奖了
        isRetry: true
      }).then(res => {
        console.log("getdrawprize", res.data);
      
        let count = this.data.count - 1
        console.log('请求成功...')
        this.setData({
          awardValue:res.data,
          count:count
        })
        console.log( this.data.count )
        var i ;      //  i  是中奖下标  我根据循环拿到了下标    应该有更简单方式    各位随意操作
        for (let index = 0; index < this.data.prizeList.length; index++) {
          if(this.data.prizeList[index].id == res.data.prizeId){
            console.log(index)
            i = index 
          }
        }
        clearInterval(this.apiLoding)			//  拿到下标就清除一开始设置的定时器
        console.log(i)
        //下面这块就是设置转的角度 和  停在哪个位置
        var rotateEnd = 360 * 2 + this.rand(60 * i + 7, 60 * (i + 1) - 7) - 30;   
        var speed = 0;
        this.timerInterval = setInterval(() => {
          speed = Math.ceil(((rotateEnd - this.data.rotate) / 20));
          if (this.data.rotate + speed >= rotateEnd) {
            this.setData({  
              rotate: rotateEnd % 360
            })
            rotateEnd = rotateEnd % 360;
            let prize = Math.floor((Math.abs(this.data.rotate) + 30) / 60);
            if (prize == this.prizeNum) {
              prize = 0;
            }
            console.log(this.data.prizeList[i]);
            
            clearInterval(this.timerInterval);
            setTimeout(()=>{
              this.setData({
                awardInfo:true,
              })
            },1000)
          } else {
            this.setData({
              rotate: this.data.rotate + speed
            })
          }
        }, 100)
      }).catch(res => {
        console.log("getdrawprize", res);
        clearInterval(this.apiLoding)
        clearInterval(this.timerInterval);
        this.flag = false
      })
  },

css

/* 转盘 */
.bgImg{
  width: 616rpx;
  height: 616rpx;
}
.couponIMg{
  width: 75rpx;
  height: 75rpx;
  margin-top: 4rpx;
}
.contain{
  width: 616rpx;
  height: 616rpx;
  position: absolute;
  top: 0;
  left: 0;
}
.goImg{
  
  position: absolute;
  width: 122rpx;
  height: 158rpx;
  top: 215rpx;
  left: 240rpx;
  z-index: 2;
  
}
.couItem{
  height: 308rpx;
  width: 205rpx;
  position: absolute;
  top: 0;
  left: 205rpx;
  padding-top: 70rpx;
  box-sizing: border-box;
  text-align: center;
  transform-origin: center bottom; 
}

.couItem:nth-child(2){
  transform: rotate(-60deg);
}
.couItem:nth-child(3){
  transform: rotate(-120deg);
}
.couItem:nth-child(4){
  transform: rotate(-180deg);
}
.couItem:nth-child(5){
  transform: rotate(120deg);
}
.couItem:nth-child(6){
  transform: rotate(60deg);
}
.couName{
  font-size:24rpx;
  font-weight:bold;
  line-height:48rpx;
  color:#D08E4C;
  width: 202rpx;
  overflow: hidden;
  text-overflow:ellipsis;
  white-space: nowrap;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值