动策划人员策划这个抽奖页面,用于app内。
当时,这个转盘布局我踩坑了,我本以为这么简单的布局应该不用绝对定位的,是我想多了!然后改为绝对定位来实现,因为要简单些。
抽奖转盘
一、九个格子和开始按钮,页面布局的实现思路
这个用绝对定位,小格子相对于大转盘定位,这个我就给个简单例子就好了哈,我相信你们能懂起的,如果没理解到我再详说。
标注
如上图所示,大框为父容器,九个小格子为子容器
<div class="parent"> <div class="child child1"></div> <div class="child child2"></div> <div class="child child3"></div> ....... <div class="child child9" id="start"></div> </div> <style> .parent{ position: relative; .child{ position: absolute; } .child1{ top: 0; left: 0; } ...... .active{ background-color: darkgoldenrod; } } </style>
二、转动效果实现:(下面贴出vue文件的html和js代码,css代码没有。因为全贴出来太多了,如果想看详细代码,就到我的github仓库去观看或者下载)
转动前.png
转动后.png
app.vue
// template <template> <div id="rotary-table"> <div class="award" v-for="(award,index) in awards" :class="['award'+index,{'active': index==current}]"> {{award.name}} </div> <div id="start-btn" @click="start">开始</div> </div> </template> // js export default { name: 'get-award', data() { return { current: 0, awards: [ // 奖品数组 { id: 1, name: '空' }, { id: 2, name: '眼镜' }, { id: 3, name: '包' }, { id: 4, name: '笨驴' }, { id: 5, name: '书' }, { id: 6, name: '手链' }, { id: 7, name: '美女' }, { id: 8, name: 'iphone' } ], speed: 200, // 速度 diff: 15, // 速度增加的值 award: {}, // 抽中的奖品 time: 0 // 记录开始抽奖时的时间 }; }, methods: { start(){ // 开始抽奖 this.drawAward(); this.time=Date.now(); this.speed=200; this.diff=15; }, drawAward(){ // 请求接口, 这里我就模拟请求后的数据(请求时间为2s) setTimeout( ()=> { this.award={ id: '4', name: '笨驴', }; }, 2000 ); this.move(); }, move(){ window.timeout=setTimeout( ()=> { this.current++; if ( this.current > 7 ) { this.current=0; } // 若抽中的奖品id存在,则开始减速转动 if ( this.award.id && ( Date.now() - this.time ) / 1000 > 2 ) { this.speed +=this.diff; // 转动减速 // 若转动时间超过4秒,并且奖品id等于小格子的奖品id,则停下来! if ( ( Date.now() - this.time ) / 1000 > 4 && this.award.id==this.awards[ this.current ].id ) { clearTimeout( window.timeout ); setTimeout( ()=> { alert( this.award.name ); }, 0 ); return; } // 若抽中的奖品不存在,则加速转动 } else { this.speed -=this.diff; // 转动加速 } this.move(); }, this.speed ); } } };
结尾发言
如果没有理解到,可以留言问我哈。这是我专门写的小demo,希望能帮到大家。谢谢!
代码仓库地址:https://github.com/lingziyb/get-award
抽奖大转盘演示:http://www.sucaihuo.com/php/3301.html
function getRand($proArr, $proCount) {
$result='';
$proSum=0;
foreach ($proCount as $key=> $val) {
if ($val <=0) {
continue;
} else {
$proSum=$proSum + $proArr[$key];
}
}
foreach ($proArr as $key=> $proCur) {
if ($proCount[$key] <=0) {
continue;
} else {
$randNum=mt_rand(1, $proSum);
if ($randNum <=$proCur) {
$result=$key;
break;
} else {
$proSum -=$proCur;
}
}
}
unset($proArr);
return $result;
}
$prizes=array(
0=> array(
"id"=> 0, //中奖id
"name"=> "谢谢参与", //奖品名称
"count"=> "100", //库存
),
1=> array(
"id"=> 1,
"name"=> "罗浮山门票",
"count"=> "40",
),
2=> array(
"id"=> 2,
"name"=> "罗浮山嘉宝田温泉体验券",
"count"=> "40",
),
3=> array(
"id"=> 3,
"name"=> "精美旅游书籍《山水酿惠州》",
"count"=> "40",
),
4=> array(
"id"=> 4,
"name"=> "碧海湾漂流门票",
"count"=> "40",
),
5=> array(
"id"=> 5,
"name"=> "南昆山门票",
"count"=> "40",
),
6=> array(
"id"=> 6,
"name"=> "云顶温泉精美礼品",
"count"=> "40",
),
);
源码来源:https://gitee.com/lanrenzhijia/codes/d342lk1yw5nm8x09euqag50
个是我用htmlcssrScrip写出来的东西,它包括两个部分,一个是上面的转盘,一个是下面的计时器。
·点击spin旋转,它就转,stop停止,停止以后就会反馈,就是获得了什么,学习两个小时。获得这个以后就可以start timer,开始学习。
·这个是重置,重置timer,计时器。如果学完了两个小时再转一次,转完以后又是学习两个小时,再开计时器,学完又重置一遍,再转一下,学习30分钟,再去学30分钟,再开,再关。
这么一个东西要怎么做?等一下我教你们怎么做。
·首先先在桌面新建一个文本文档,给它命名成learning,learning,will,叫什么?学习的转盘,带有WIS,WIS什么timer,计时timer,计时器。
·把它的后缀改一下,改成html覆盖,保存。
·保存完了以后用vs code打开,打开完以后就是一个空白的页面。
·接下来敲代码的时间,写一个前端代码,实现刚刚那个小东西。
·首先把框架给卸了,然后是Id doctyper,html,这个好像是浏览器之类的问题的,管它HML后,这是language的缩写,它就等于English的缩写。
·下面是made,chaset,这是设置一个字符编码的UTF-8,name,这是一个4口属性。
·内容是content,content的宽度是等于设备的宽度。
·它的initial SCA,SCA就是它的初始规模,是等于1.0,就是跟网页的大小是一样的。
·接下来是一个title,就是标题,标题就是learning,learning,will学习的,转盘带有time,就是带有计时器的一个学习的转盘。
·下面是CS的内容,就是style样式,这是CSS的内容。
·接下来就是到波底,接下来就是body的内容,body,head还没结束是吧?不对,我的head去哪了?问题好大,这里有一个head,我是觉得是先写大皇家是比较好的,就是the head。
·head里面包括几个部分,就是title,标题还有style,就是CSS,body,body里面,波波底下面是h,是HGL结束了。
·body里面包含两个东西,两个div就是两个块,这块一个是用来做抽奖的转盘的,一个是用来做timer,就是计时器。
·记下来是叫script东西,就是script,screw script。
·这就是整个的框架,就是这里是CSS的地方,这里是title,它的标题就是网页上它给出的标题,就是地址栏那里的标题。
·这里是两个div,就是两个块,到时候就是用两个容器来装抽奖的转盘和计时器。
·这里是加入script的代码。
大概就讲到这里。
*请认真填写需求信息,我们会在24小时内与您取得联系。