Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>抽奖</title>
<style type="text/css">
    body{
        margin: 0px; padding: 0px; background: #d1d1d1;
    }
    ul,li,ol,dl,dd,dt{
        list-style: none; margin: 0px auto;
    }
    .drawBox{ width:590px; height: 406px; margin: 0px auto; position: relative; background: #d1d1d1; margin-top: 10px;; }
    .drawBox ul li{  width: 186px; height: 124px; position: absolute; background:#fff;
		display: block;
		border-radius: 8px;
		box-sizing:border-box;
		font-size: 12px;
		text-align: center;
		transition: all 0.5s ease-in;
		opacity: 0;
	}
	.drawBox ul li img.goods{
		width: 146px; height: 72px; display: block; margin: 18px auto 4px; border-radius: 8px;
	}
	.drawBox ul li span.info{
		clear: both;
		color: #393939;
	}
	.drawBox ul li.active{
		box-shadow: 0px 10px 20px rgb(255 180 78 / 80%);
		-webkit-transform:scale(1.1,1.1);
    	-moz-transform:scale(1.1,1.1);
	}
	.drawBox ul li.active::before{
		content: '';
		display: block;
		position: absolute;
		border-radius: 8px;
		width: 180px; height: 120px;
		left: 0px;
		top: 0px;
		border: 4px solid #ffb44e;
	}
    .drawBox #btnDraw{ 
		width: 172px; 
		height: 64px; 
		position: absolute; 
		top: 50%; left: 50%; 
		margin-left: -86px;
		margin-top: -32px;
		background-image: linear-gradient(to right, #f66236 ,#f0a041);
		box-shadow: 3px 6px 10px rgb(255 180 78 / 80%);
		font-weight: bold;
		border-radius: 30px;
		border: 2px solid #ffb44e;
		cursor: pointer;
		color: #fff;
		font-size: 18px;
		outline: none;
	}
	.anmPulse{
		-webkit-animation:pulse .8s .1s ease both;
		-moz-animation:pulse .8s .1s ease both;
	}
	@-webkit-keyframes pulse{
		0%{-webkit-transform:scale(1)}
		50%{-webkit-transform:scale(1.1)}
		100%{-webkit-transform:scale(1)}
	}
	@-moz-keyframes pulse{
		0%{-moz-transform:scale(1)}
		50%{-moz-transform:scale(1.1)}
		100%{-moz-transform:scale(1)}
	}
</style>
</head>

<body>
<div id="lottery">
    <div class="drawBox" id="drawBox">
        <ul>
            <li>
				<img src="https://www.processon.com/assets/images/activity/210601/draw/imgPomember.png" alt="" class="goods">
				<span class="info">一年processOn会员</span>
			</li>
            <li>
				<img src="https://www.processon.com/assets/images/activity/210601/draw/imgPomember.png" alt="" class="goods">
				<span class="info">一年processOn会员</span>
			</li>
            <li>
				<img src="https://www.processon.com/assets/images/activity/210601/draw/imgPomember.png" alt="" class="goods">
				<span class="info">一年processOn会员</span>
			</li>
            <li>
				<img src="https://www.processon.com/assets/images/activity/210601/draw/imgPomember.png" alt="" class="goods">
				<span class="info">一年processOn会员</span>
			</li>
            <li>
				<img src="https://www.processon.com/assets/images/activity/210601/draw/imgPomember.png" alt="" class="goods">
				<span class="info">一年processOn会员</span>
			</li>
            <li>
				<img src="https://www.processon.com/assets/images/activity/210601/draw/imgPomember.png" alt="" class="goods">
				<span class="info">一年processOn会员</span>
			</li>
            <li>
				<img src="https://www.processon.com/assets/images/activity/210601/draw/imgPomember.png" alt="" class="goods">
				<span class="info">一年processOn会员</span>
			</li>
            <li>
				<img src="https://www.processon.com/assets/images/activity/210601/draw/imgPomember.png" alt="" class="goods">
				<span class="info">一年processOn会员</span>
			</li>
        </ul>
        <button id="btnDraw">立即抽奖</button>
    </div>

</div>


<script type="text/javascript" src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>
</script>
</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                (function luckDrawCode(w){
    var luckDrawCode = {
        parentElm:null, //父级盒子
        goodsListElm:null,
        _data:{},
        data:{
            lielmPx:[
                {x:0,y:0}
            ],
            index:0, //下标
            isRun:false, //是否运行
            runTimePx:600, //运行间隔
            timer:null, //定时器对象
            timeDoOncer:null,
            timePx:100, // 时间增减值
            turnsNum:0, // 圈数
            lastIndex:null, //落点KEY
            lastTurnsNum: 3, //最后圈数
            istwinkle: true, //是否闪烁
            istwinkleNum:0, // 闪烁次数
            istwinkleMax:5
        },
        // 参数初始化
        pmsInit:function(fn){
            var px = 16,goodselmW=186,goodselmH=124,_x = 0,_y = 0,
             _node = 3, //节点
             _nodePlusNum = 2, //节点关系
             _status = 1; // 1右 2 下 3左 4上
            for(var i=0;i<8;i++){
                var _index = i + 1; // 商品编号 1 开始
                if (_index >= 8){ break; }
                if (_index%_node === 0 && _index <= (_node + _nodePlusNum)){ // 节点
                    _status ++;
                    _node += _nodePlusNum;
                }
                switch(_status){
                    case 1:
                        _x += (goodselmW+px);
                        break;
                    case 2:
                        _y += (goodselmH+px);
                        break;
                    case 3:
                        _x -= (goodselmW+px);
                        break;
                    case 4:
                        _y -= (goodselmH+px);
                        break;
                }
                this.data.lielmPx[_index] = {x:_x,y:_y};
            }
            typeof fn && fn();
            console.log(this.data.lielmPx);
        },
        init:function(_this){
            // 初始化赋值
            // jQuery.extend(this._data, this.data);
            luckDrawCode.parentElm = _this;
            luckDrawCode.goodsListElm = _this.children('ul');
            this.pmsInit(function(){
                _this.children('ul').children('li').each(function(i, n){
                    $(this).css({
                        left:luckDrawCode.parentElm.width() / 2 - $(this).width() / 2,
                        top:luckDrawCode.parentElm.height() / 2 - $(this).height() / 2,
                        opacity: 0
                    });
                })
                luckDrawCode.firstAnm(); //初始化动画
            });
        },
        // el 当前按钮DOM对象
        run:function(el){
            if (!luckDrawCode.data.isRun){
                return false;
            }
            // 初始化
            this.data.timer = setTimeout(function(){
                doSomeThing();
                console.log(luckDrawCode.data.index);
            },this.data.runTimePx);

            function doSomeThing(){
                // 参数设置
                luckDrawCode.data.index ++;
                luckDrawCode.data.parentElm = el
                // 中奖啦
                luckDrawCode.checkIndex(function(goodsIndex){
                    // 中奖操作
                    luckDrawCode.popGoods(goodsIndex);
                    // luckDrawCode.reset();
                });
                // DOM改变
                luckDrawCode.changeUi();
                // 
                luckDrawCode.changeTimePx(); // 递减

                // 下次执行
                luckDrawCode.run(el);
            }
        },
        // 节流
        timeDoOnce:function(fn, time){
            clearTimeout(this.timeDoOncer);
            var _time = typeof time === 'number' ? time : 1000;
            this.timeDoOncer = setTimeout(function(){
                typeof fn === 'function' && fn();
            }, _time);
        },
        // 参数重置
        reset:function(){
            this.data.index = 0;
            this.data.turnsNum = 0;
            this.data.lastIndex = null;
            this.changeUi();
            console.log('reset', this.data)
        },
        // 点击动画
        clickAnm:function(){
            this.parentElm.children('#btnDraw').addClass('anmPulse');
            this.timeDoOnce(function(){
                luckDrawCode.parentElm.children('#btnDraw').removeClass('anmPulse');
            });
        },
        // 初始化动画
        firstAnm:function(){
            var _time = 800 / 8
            this.goodsListElm.children('li').each(function(i, n){
                var _thisElm = $(this);
                setTimeout(function(){
                    _thisElm.css({
                        left:luckDrawCode.data.lielmPx[i].x,
                        top:luckDrawCode.data.lielmPx[i].y,
                        opacity: 1
                    });
                },_time);
                _time += 800 / 8;
            });
            setTimeout(function(){
                luckDrawCode.twinkleAnm(300);
            }, 900);
        },
        // 闪烁效果
        twinkleAnm:function(twinkleTime){
            console.log('开启闪烁');
            var _this = this;
            if (!_this.data.istwinkle || _this.data.istwinkleNum >= _this.data.istwinkleMax){
                _this.goodsListElm.children('li').removeClass('active');
                return false;
            }
            setTimeout(function(){
                if (_this.goodsListElm.children('li').eq(0).hasClass('active')){
                    _this.goodsListElm.children('li').removeClass('active');
                }else {
                    _this.goodsListElm.children('li').addClass('active');
                }
                _this.data.istwinkleNum ++;
                _this.twinkleAnm(twinkleTime);
            },twinkleTime);
        },
        endAnm:function(){

        },
        popGoods:function(goodsIndex){
            alert('中奖啦'+goodsIndex);
        },
        // 检测INDEX
        checkIndex:function(fn){
            if (this.data.index >= 8){
                this.data.index = 0; // 重置index
                this.data.turnsNum ++; // 累加圈数
                console.log('圈数',this.data.turnsNum, this.data.lastIndex, this.data.index >= this.data.lastIndex)
            }
            // 临界点判断 
            if (this.data.turnsNum >= this.data.lastTurnsNum && this.data.lastIndex !== null && this.data.index >= this.data.lastIndex){
                this.data.isRun = false;
                typeof fn === 'function' && fn(this.data.index);
            }

        },
        // 改变时间点
        changeTimePx:function(){
            // 满足圈数
            if (this.data.turnsNum >= this.data.lastTurnsNum){
                this.data.runTimePx += this.data.timePx;
                if (this.data.runTimePx >= 1000){
                    this.data.runTimePx = 1000;
                }
            } else {
                this.data.runTimePx -= this.data.timePx;
                if (this.data.runTimePx <= 100){
                    this.data.runTimePx = 100;
                }
            }
        },
        // 改变UI
        changeUi:function(){
            this.goodsListElm.children('li').eq(this.data.index).addClass('active').siblings('li').removeClass('active');
        },
        //获得最后落点INDEX
        getLastIndex:function(){
            this.data.lastIndex = Math.floor(Math.random() * 8);
            console.log('结束点', this.data.lastIndex)
            return  
            rturn
            $.ajax({
                type: "POST",
                url: "some.php",
                data: "name=John&location=Boston",
                success: function(msg){
                    alert( "Data Saved: " + msg );
                }
            });
        }
    };
    jQuery.fn.extend({
        luckDraw:function(){
            luckDrawCode.init(this);
            this.children('#btnDraw').on('click',function(e){
                // console.log(this, luckDrawCode)
                if(luckDrawCode.data.isRun){
                    return false;
                }
                luckDrawCode.data.istwinkle = false;
                luckDrawCode.clickAnm();
                luckDrawCode.reset();
                luckDrawCode.data.isRun = true;
                luckDrawCode.getLastIndex();
                luckDrawCode.run(this);
            });
        }
    });
    return luckDrawCode;
})(window,jQuery);
$("#drawBox").luckDraw();
              
            
!
999px

Console