php抽奖的数字滚动器,JS数字抽奖游戏实现方法

新年网页抽奖程序

* {margin:0; padding:0;}

ul,li {list-style-type:none;}

body {overflow:hidden;}

#back {width:100%; height:100%;

background:#f5f5f5; position:absolute; z-index:1;

}

#box {width:360px; height:100px;

position:absolute; z-index:3; top:50%; left:50%;

margin-top:-50px; margin-left:-180px; text-align:center;

}

#box1,#box2,#box3 {width:100px; height:100px;

line-height:100px;

float:left; background:#321c24;

border:10px #321c24 solid;

border-radius:50%; position:relative; overflow:hidden;

}

#box1 ul,#box2 ul,#box3 ul {color:#fff; font-size:68px;

font-family:"Arial Black"; text-align:center;

width:100px; height:100px; line-height:100px;

position:absolute; top:0; left:0;

}

#box1 ul li,#box2 ul li,#box3 ul li {

width:100px; height:100px;

background:red; border-radius:50%;

}

var AIR = {

$: function (id)

{

return typeof id === "string" ? document.getElementById(id) : id;

},

$$: function (elem, oParent)

{

return (oParent || document).getElementsByTagName(elem);

},

addEvent: function (oElement, sEvent, fnHandler)

{

oElement.addEventListener ? oElement.addEventListener(sEvent, fnHandler, false) : oElement.attachEvent("on" + sEvent, fnHandler)

},

removeEvent: function (oElement, sEvent, fnHandler)

{

oElement.removeEventListener ? oElement.removeEventListener(sEvent, fnHandler, false) : oElement.detachEvent("on" + sEvent, fnHandler)

},

getElementClient: function (){

var arr = [];

if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth){

arr.push(document.documentElement.clientWidth);

arr.push(document.documentElement.clientHeight);

return arr;

}

},

getStyle: function (obj, attr)

{

return parseFloat(obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj, null)[attr])

},

startMove: function (obj, pos, onEnd)

{

clearInterval(obj.timer);

var _this = this;

obj.timer = setInterval(function ()

{

_this.doMove(obj, pos, onEnd)

}, 30)

},

doMove: function (obj, pos, onEnd)

{

var iCurL = this.getStyle(obj, "left");

var iCurT = this.getStyle(obj, "top");

var iSpeedL = (pos.left - iCurL) / 5;

var iSpeedT = (pos.top - iCurT) / 5;

iSpeedL = iSpeedL > 0 ? Math.ceil(iSpeedL) : Math.floor(iSpeedL);

iSpeedT = iSpeedT > 0 ? Math.ceil(iSpeedT) : Math.floor(iSpeedT);

if (pos.left == iCurL && pos.top == iCurT)

{

clearInterval(obj.timer);

onEnd && onEnd()

}

else

{

obj.style.left = iCurL + iSpeedL + "px";

obj.style.top = iCurT + iSpeedT + "px";

}

}

}

function Draw (obj, num)

{

this.obj = obj;

this.num = num;

this.data = [];

this.result = [];

this.show = 0;

this.btn = true;

this.timer = true;

this.h = 0;

this.uh = 0;

this.initialize();

}

Draw.prototype = {

initialize: function ()

{

this.createArr ();

this.createElement ();

this.closeEvent ();

this.startDraw ();

},

createElement: function ()

{

for(var j=0; j

var ul = document.createElement("ul");

for(var i=0; i<10; i++){

var li = document.createElement("li");

li.innerHTML = i;

ul.appendChild(li)

}

this.obj[j].appendChild(ul);

this.obj[j].btn = true;

AIR.$$("ul",this.obj[j])[0].innerHTML += AIR.$$("ul",this.obj[j])[0].innerHTML;

}

var UL = AIR.$$("ul",this.obj[0])[0];

this.h = AIR.getStyle(AIR.$$("li",UL)[0],"height");

this.uh = AIR.$$("li",UL).length * this.h

},

randomSort: function (a, b) {

return Math.random()>.5 ? -1 : 1;

},

createArr: function ()

{

for(var i=0; i

this.data.push(i);

}

this.data.sort(this.randomSort);

},

closeEvent: function ()

{

document.οnmοusedοwn=document.οnmοusemοve=document.οncοntextmenu=function()

{

return false;

}

},

startDraw: function ()

{

var _this = this;

document.onkeyup = function ( ev )

{

var ev = ev || window.event;

if(ev.keyCode == 13 || ev.keyCode == 32){

if(_this.btn && _this.timer){

if(_this.obj[_this.obj.length-1].btn){

_this.Play ();

_this.btn = !_this.btn;

_this.timer = !_this.timer;

}

}else{

if(_this.obj[_this.obj.length-1].btn){

_this.Stop ();

_this.btn = !_this.btn;

_this.timer = !_this.timer;

}

}

return false;

}else{

return false;

}

}

},

Play: function ()

{

if(this.timer && this.btn){

var t = 0;

for(var i=0; i

this.obj[i].btn = false;

this.playTimer (this.obj[i],t);

t += 1500;

}

}else{

return false;

}

},

playTimer: function (obj,t)

{

var _this = this;

setTimeout(function(){

_this.Move (obj);

},t)

},

Del: function (a)

{

for(var i=0; i

if(a == this.data[i]){

this.data.splice(i,1);

}

}

},

Stop: function ()

{

if(!this.timer && !this.btn){

var n = this.num + 1;

var r = this.data[Math.floor(Math.random() * (0-n) + n)];

this.show = r;

this.Del (r);

r = r.toString().split("");

var c = this.obj.length - r.length;

if(r.length < this.obj.length){

for(var i=0; i

r.unshift(0)

}

}

this.result = r;

//document.title = r+" : "+this.data;

var t = 0;

for(var i=0; i

this.obj[i].btn = false;

this.obj[i].index = i;

this.obj[i].num = this.result[this.obj[i].index];

this.stopTimer (this.obj[i],t);

t += 1500;

}

}

},

stopTimer: function (obj,t)

{

var _this = this;

setTimeout(function(){

_this.showResult (obj);

},t)

},

showResult: function (obj)

{

var _this = this;

this.timer = true;

this.btn = true;

obj.btn = false;

obj.vh = -obj.num * this.h;

obj.timeOut = setInterval(function(){

obj.speed -= 1;

if(obj.speed == 1){

clearInterval(obj.timeOut);

clearInterval(obj.timer);

obj.timer = setInterval(function(){

if(obj.ul.offsetTop >= obj.vh){

clearInterval(obj.timer);

AIR.startMove(obj.ul,{left:0,top:obj.vh},function(){

obj.btn = true;

var set = true;

for(var i=0; i<_this.obj.length i>

if(!_this.obj[i].btn){

set = false;

}

}

if(set){

_this.Open(_this.show)

}

});

}

obj.ul.style.top = obj.ul.offsetTop + obj.speed +"px";

},30);

}

},100);

},

Open: function (num)

{

document.title += " "+ num;

},

Move: function (obj)

{

var _this = this;

var obj = obj;

obj.btn = false;

obj.timer = null;

obj.speed = 1;

obj.ul = AIR.$$("ul",obj)[0];

obj.ul.style.height = this.uh +"px";

obj.timer = setInterval(function(){

if(obj.ul.offsetTop > 0){

obj.ul.style.top = -(_this.uh/2) +"px";

}

obj.ul.style.top = obj.ul.offsetTop + obj.speed +"px";

},30);

obj.timeOut = setInterval(function(){

obj.speed += 1;

if(obj.speed == 30){

clearInterval(obj.timeOut);

obj.btn = true;

}

},300)

}

}

var initialize = function ()

{

new Draw ([AIR.$("box1"),AIR.$("box2"),AIR.$("box3")],100);

reSize ();

}

var reSize = function ()

{

var v = AIR.getElementClient();

AIR.$$("img",AIR.$("back"))[0].width = v[0];

AIR.$$("img",AIR.$("back"))[0].height = v[1];

}

AIR.addEvent(window,"load",initialize);

AIR.addEvent(window,"resize",reSize);

20153291274950386.jpg

100
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的Vue数字滚动抽奖实现示例: 1. 在template中定义一个数字滚动的容: ```html <template> <div class="number-roller"> <div class="number" v-bind:style="numberStyle">{{ number }}</div> </div> </template> ``` 2. 在data中定义数字滚动的数值和样式: ```javascript export default { data() { return { number: 0, numberStyle: { transform: 'translateY(0px)', transition: 'transform 0.3s ease-out' } } } } ``` 3. 在computed中计算数字滚动的样式和值: ```javascript computed: { numberStyle() { return { transform: `translateY(${-this.number * 30}px)`, transition: 'transform 0.3s ease-out' } } } ``` 这里假设数字是在0到9之间滚动,每个数字之间的间距为30像素。当数字改变时,计算属性会重新计算数字滚动的样式。 4. 在methods中添加一个"startRolling"方法,以触发数字滚动动画: ```javascript methods: { startRolling() { let count = 0; let interval = setInterval(() => { this.number = Math.floor(Math.random() * 10); // 随机生成一个数字 count++; if (count > 10) { // 滚动10次后停止滚动 clearInterval(interval); this.$emit('rolling-complete', this.number); // 触发抽奖完成事件,传递中奖号码 } }, 300); } } ``` 这里使用setInterval方法每隔0.3秒随机生成一个数字滚动10次后停止滚动,并通过$emit方法触发抽奖完成事件,将中奖号码传递给父组件。 5. 在父组件中使用"NumberRoller"组件,并监听抽奖完成事件: ```html <template> <div> <button @click="startLottery">开始抽奖</button> <number-roller v-if="rolling" @rolling-complete="handleRollingComplete"></number-roller> </div> </template> ``` 6. 在父组件中定义"startLottery"方法,以开始抽奖: ```javascript methods: { startLottery() { this.rolling = true; }, handleRollingComplete(number) { this.rolling = false; alert(`恭喜您中奖了,中奖号码为${number}`); } } ``` 这里使用"rolling"变量控制数字滚动组件的显示和隐藏,当抽奖完成时,显示中奖号码的提示框。 这样就完成了一个简单的Vue数字滚动抽奖实现。你可以根据实际需求进行更加丰富的自定义和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值