翻牌样式的抽奖怎么做

抽奖是很多商家吸引客户的一种手段,随着技术的不断完善,抽奖的方式越来越简单了,除了有传统的人工化的抽奖方式,智能化抽奖的形式有转盘式和翻牌式,今天就给大家带来抽奖翻牌样式的开发流程,翻牌式的抽奖大体需要,九宫格的卡牌,卡牌正面展示所有的奖品,然后讲卡牌翻转过去,打乱顺序,点击抽奖。

这个需求本身其实不难,主要是分为三步;

1卡牌翻转

我们先在dom中渲染9个卡牌。

 

  1. <view class="card-module">
  2. <view class="card {{showClass ? 'change' : ''}}>
  3. <view class="front card-item">{{cardItem.front}}</view>
  4. <view class="back card-item">{{cardItem.back}}</view>
  5. </view>
  6. </repeat>
  7. </view>

在数据中生成模拟卡牌数据

 

  1. cardData: [
  2. {
  3. animationData: {},
  4. front: '正面1',
  5. back: '反面1'
  6. },
  7. ...
  8. ...
  9. {
  10. animationData: {},
  11. front: '正面9',
  12. back: '反面9'
  13. }
  14. ],
  15. showClass: false,

在样式中把卡牌的基本样式渲染出来

 

  1. .card-module{
  2. padding: 45rpx;
  3. display: flex;
  4. flex-direction: row;
  5. flex-wrap: wrap;
  6. transform:translate3d(0,0,0);
  7. .card{
  8. width: 200rpx;
  9. height: 200rpx;
  10. line-height: 200rpx;
  11. text-align: center;
  12. color: #fff;
  13. margin: 10rpx;
  14. position:relative;
  15. overflow:hidden;
  16. .card-item{
  17. position:absolute;
  18. left:0;
  19. top:0;
  20. width:100%;
  21. height:100%;
  22. transition:all .5s ease-in-out;
  23. transform-style:preserve-3d;
  24. backface-visibility:hidden;
  25. box-sizing:border-box;
  26. }
  27. .front{
  28. background-color: red;
  29. transform: rotateY(0deg);
  30. z-index:2;
  31. }
  32. .back{
  33. background-color: #009fff;
  34. transform: rotateY(180deg);
  35. z-index:1;
  36. }
  37. }
  38. .card.change{
  39. .front{
  40. z-index:1;
  41. transform: rotateY(180deg);
  42. }
  43. .back{
  44. z-index:2;
  45. transform: rotateY(0deg);
  46. }
  47. }
  48. }

这里有些css属性可能需要大部补充学习一下

css3 backface-visibility 属性

定义和用法  backface-visibility 属性定义当元素不面向屏幕时是否可见。  如果在旋转元素不希望看到其背面时,该属性很有用。

CSS3 perspective 属性

perspective 属性定义 3D 元素距视图的距离,以像素计。该属性允许您改变 3D 元素查看 3D 元素的视图。  当为元素定义 perspective 属性时,其子元素会获得透视效果,而不是元素本身。

2卡牌打乱

由于业务上是抽奖使用的,所以选择的方案是:翻转后,卡牌收回到中间的卡牌中间,然后再让卡牌回到原来的位置。 微信小程序的动画接口使用方式是在dom对象上面加上animation对象。  dom

 

  1. <view class="card-module">
  2. <view class="card {{showClass ? 'change' : ''}} animation="{{cardItem.animationData}}" >
  3. <view class="front card-item">{{cardItem.front}}</view>
  4. <view class="back card-item">{{cardItem.back}}</view>
  5. </view>
  6. </repeat>
  7. </view>

script

 

  1. allMove () {
  2. // 110 是卡牌宽度加边距
  3. this.methods.shuffle.call(this, 110)
  4. let timer = setTimeout(() => {
  5. clearTimeout(timer)
  6. this.methods.shuffle.call(this, 0)
  7. this.$apply()
  8. }, 1000)
  9. },
  10. // 洗牌
  11. shuffle (translateUnit) {
  12. let curCardData = this.cardData
  13. curCardData.map((item, index) => {
  14. let animation = wepy.createAnimation({
  15. duration: 500,
  16. timingFunction: 'ease'
  17. })
  18. animation.export()
  19. switch (index) {
  20. case 0:
  21. animation.translate(translateUnit, translateUnit).step()
  22. break
  23. case 1:
  24. animation.translate(0, translateUnit).step()
  25. break
  26. case 2:
  27. animation.translate(-translateUnit, translateUnit).step()
  28. break
  29. case 3:
  30. animation.translate(translateUnit, 0).step()
  31. break
  32. case 4:
  33. animation.translate(0, 0).step()
  34. break
  35. case 5:
  36. animation.translate(-translateUnit, 0).step()
  37. break
  38. case 6:
  39. animation.translate(translateUnit, -translateUnit).step()
  40. break
  41. case 7:
  42. animation.translate(0, -translateUnit).step()
  43. break
  44. case 8:
  45. animation.translate(-translateUnit, -translateUnit).step()
  46. break
  47. }
  48. item.animationData = animation.export()
  49. })
  50. this.cardData = curCardData
  51. this.$apply()
  52. },

卡牌翻转

dom代码

 

  1. <view class="card-module">
  2. <view class="card {{showClass ? 'change' : ''}} {{curIndex === index ? 'getprize' : ''}}" @tap="itemChage({{cardItem}}, {{index}})" animation="{{cardItem.animationData}}" >
  3. <view class="front card-item">{{cardItem.front}}</view>
  4. <view class="back card-item">{{cardItem.back}}</view>
  5. </view>
  6. </repeat>
  7. </view>

script代码

 

  1. data中定义一个curIndex = -1的对象
  2. data = {
  3. curOpen: -1,
  4. }
  5. methods = {
  6. // 抽奖
  7. itemChage (item, curIndex) {
  8. this.cardData[curIndex].front = 'iphone x'
  9. console.log(item, curIndex)
  10. this.curOpen = curIndex
  11. }
  12. }

less

 

  1. .card.getprize{
  2. .front{
  3. z-index:2;
  4. transform: rotateY(0deg);
  5. }
  6. .back{
  7. z-index:1;
  8. transform: rotateY(180deg);
  9. }
  10. }

那我们是不是可以在卡牌中也使用二维数组布局属性

 

  1. resetData () {
  2. const total = 9 // 总数
  3. const lineTotal = 3 // 单行数
  4. curCardData.map((item, index) => {
  5. let curCardData = this.cardData
  6. let x = index % lineTotal
  7. let y = parseInt(index / lineTotal)
  8. item.twoArry = {x, y}
  9. })
  10. }

在位移动画中使用二维布局的差值进行位移

 

  1. // 洗牌
  2. shuffle (translateUnit) {
  3. let curCardData = this.cardData
  4. curCardData.map((item, index) => {
  5. let animation = wepy.createAnimation({
  6. duration: 500,
  7. timingFunction: 'ease'
  8. })
  9. animation.export()
  10. const translateUnitX = translateUnit * (1 - item.twoArry.x)
  11. const translateUnitY = translateUnit * (1 - item.twoArry.y)
  12. animation.translate(translateUnitX, translateUnitY).step()
  13. item.animationData = animation.export()
  14. })
  15. this.cardData = curCardData
  16. this.$apply()
  17. },

以上就是卡牌式抽奖的技术流程。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以帮你写一个uniapp的抽奖代码,以下是一个简单的示例: ``` <template> <view class="container"> <view class="card" v-for="(item, index) in cards" :key="index" :class="{ flipped: item.flipped }" @click="flipCard(index)"> <view class="front">{{ item.front }}</view> <view class="back">{{ item.back }}</view> </view> </view> </template> <script> export default { data() { return { cards: [ { front: '面1', back: '奖品1', flipped: false }, { front: '面2', back: '奖品2', flipped: false }, { front: '面3', back: '奖品3', flipped: false }, // 可以添加更多的面和奖品 ], }; }, methods: { flipCard(index) { if (!this.cards[index].flipped) { // 奖品逻辑处理 console.log(this.cards[index].back); // 面 this.cards[index].flipped = true; } }, }, }; </script> <style> .container { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; height: 400px; } .card { width: 80px; height: 120px; margin: 10px; perspective: 800px; cursor: pointer; } .front, .back { width: 100%; height: 100%; position: absolute; display: flex; justify-content: center; align-items: center; font-size: 16px; color: #fff; } .front { background-color: #f0f0f0; } .back { background-color: #ff0000; transform: rotateY(180deg); } .flipped .front { transform: rotateY(180deg); } .flipped .back { transform: rotateY(0); } </style> ``` 这是一个简单的抽奖代码示例,点击面时会将其转,并打印出对应的奖品信息。你可以根据自己的需求进行修改和扩展。 注意:以上代码是一个基础示例,实际使用时需要根据自己的需求进行修改和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值