h5+原生js实现简单的跑马灯抽奖

在教别人html和js入门的时候顺手写了两个demo,她说有点无趣,我想了想写了个女孩子喜欢的九宫格跑马灯效果。虽然略显简陋但是可以玩,涉及知识点:函数和匿名函数的使用、数组和对象、js创建和删除节点、setInterval和setTimeout定时器、onclick点击事件、for和forEach循环、Math.random随机数。

上代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>幸运大转盘</title>
</head>
<body>
    <div id="big-cube"></div>
    <button id="go-go">抽奖</button>
</body>
<style>
    #big-cube{
        width: 350px;
        height: 350px;
    }
    .prize{
        width: 100px;
        height: 100px;
        border: 1px solid #999;
        margin: 5px;
        text-align: center;
        line-height: 100px;
        float: left;
        border-radius: 8px;
    }
    .noting{
        border: 1px solid #bbb;
        color: red;
    }
    #prize-title{
        color: #666;
        font-weight: 500;
        font-size: 1.1rem;
        background: #fbd4aa;
        font-style: oblique;
        border: none;
    }
</style>
<script>
    //预设奖品
    // var default_my_prize = 2;
    var setTime;
    var cycle = [0,1,2,5,8,7,6,3];
    var prizeList = [
        {name:"自行车", isPrize:true},
        {name:"谢谢参与", isPrize:false},
        {name:"跳跳糖", isPrize:true},
        {name:"谢谢参与", isPrize:false},
        {name:"幸运大转盘", isPrize:false, id:"prize-title"},
        {name:"谢谢参与", isPrize:false},
        {name:"佩奇公仔", isPrize:true},
        {name:"感谢参与", isPrize:false},
        {name:"迪奥口红", isPrize:true},
    ];
    document.getElementById('go-go').onclick = function(){
        if(setTime){
            return false;
        }
        var index = 0;
        var count = 0;
        var my_prize = document.getElementsByClassName('prize'); //获得所有的跑马灯奖品
        for(let i=0; i<my_prize.length; i++){
            my_prize[i].style.background = '';
            my_prize[i].style.color = '';
        }
        //定时器开始
        setTime = setInterval(function(){
            //从第二个跑马灯开始,每走一个跑马灯就把上一个跑马灯的颜色恢复白色
            if(index>0){
                my_prize[cycle[index-1]].style.background = '';
                my_prize[cycle[index-1]].style.color = '';
            }

            //跑完最后一个跑马灯又从第一个重新开始跑
            // if(index > (my_prize.length-1)){
            if(index > 7){
                index = 0;
            }

            //跑到哪个跑马灯就把哪个变为红色背景
            my_prize[cycle[index]].style.background = '#e6722aab';
            my_prize[cycle[index]].style.color = 'white';

            //当前跑的跑马灯的下标+1
            index++;

            //总计数器+1
            count++;

            //有预设奖品
            if(typeof default_my_prize!='undefined'){
                
                //至少跑35下,跑到预设的奖品就停下
                if(count>40 && (index-1)==default_my_prize){
                    clearInterval(setTime); //清除定时器
                    
                    //弹出中奖提示
                    if(my_prize[cycle[index-1]].className=='prize'){
                        setTimeout(()=>{alert('恭喜你抽中了['+my_prize[cycle[index-1]].innerHTML+']')},100);
                    }
                    setTime = undefined;
                }
                //中断程序运行,防止走预设奖品后的代码
                return false;
            }

            //没有预设奖品的算法,跑15次后随机获得奖品
            if(count>40 && parseInt(Math.random()*10)>8 ){
                clearInterval(setTime);
                //弹出奖品的时候排除掉谢谢惠顾
                if(my_prize[cycle[index-1]].className=='prize'){
                    setTimeout(()=>{alert('恭喜你抽中了['+my_prize[cycle[index-1]].innerHTML+']')},100);
                }
                setTime = undefined;
            }
        }, 90);
    }

    function createPrizeItem(prizeList){
        //删除所有奖品节点
        document.querySelectorAll('.prize').forEach(function(e){ 
            e.remove();
        })

        var prizeBox = document.getElementById('big-cube');
        //循环创建奖品节点
        for(let list of prizeList){
            var ele = document.createElement('div');
            ele.innerHTML = list.name ?? '谢谢参与';
            ele.className = list.isPrize? 'prize' : 'prize noting';
            if(list.id){
                ele.id = list.id;
            }
            prizeBox.append(ele);
        }
    }
    createPrizeItem(prizeList);
</script>
</html>

页面效果

总得来说麻雀虽小五脏俱全,就是不太美观。

PS:抽奖结果一般是服务器下发的,在点抽奖那一刻后端就已经给了结果,页面上只是花里胡哨跑几圈再指向最终结果。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个基本的搜索框下拉列表的实现步骤: 1. HTML结构:在HTML中创建一个输入框和一个下拉列表的容器,下拉列表的容器初始设置为隐藏状态。 ```html <div class="search-container"> <input type="text" placeholder="请输入搜索关键字"> <div class="search-list-container"> <ul class="search-list"></ul> </div> </div> ``` 2. CSS样式:设置输入框的样式和下拉列表容器的样式,下拉列表容器的初始状态设置为 `display:none` 。 ```css .search-container { position: relative; } input[type="text"] { width: 300px; height: 30px; padding: 5px; border: 1px solid #ccc; border-radius: 5px; } .search-list-container { position: absolute; top: 35px; left: 0; z-index: 99; width: 300px; max-height: 200px; overflow-y: auto; background: #fff; border: 1px solid #ccc; border-radius: 5px; display: none; } ``` 3. JS交互:监听输入框的键盘输入事件,在输入框中输入内容时,向后台发送请求获取匹配的搜索结果,将结果渲染到下拉列表中,并将下拉列表容器设置为显示状态。 ```javascript const input = document.querySelector('input[type="text"]'); const searchListContainer = document.querySelector('.search-list-container'); const searchList = document.querySelector('.search-list'); input.addEventListener('input', function(e) { const keyword = e.target.value; if (keyword.trim()) { // 发送请求获取匹配的搜索结果 const searchResults = getSearchResults(keyword); renderSearchList(searchResults); searchListContainer.style.display = 'block'; } else { searchList.innerHTML = ''; searchListContainer.style.display = 'none'; } }); function getSearchResults(keyword) { // 向后台发送请求获取搜索结果 const results = ['搜索结果1', '搜索结果2', '搜索结果3']; return results; } function renderSearchList(results) { let html = ''; results.forEach(result => { html += `<li>${result}</li>`; }); searchList.innerHTML = html; } ``` 以上就是一个简单的搜索框下拉列表的实现方法,您可以根据自己的需求进行样式和交互的修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值