requestAnimationframe的使用方法

$(function () {
    var WIDTH = Math.floor(document.documentElement.clientWidth * 0.45),//屏幕宽度*0.45来设置li的宽度
        $behindList = $(".behind ul li"),//福袋父级
        $frontList = $(".front ul li"),
        $behindBag = $(".moveing-left"),//后方运动盒子
        $frontBag = $(".moveing-right"),//前方运动盒子
        $topBag = $(".behind img"),//后方每个福袋
        $bottomBag = $(".front img"),//前方每个福袋
        $btn = $("#btn"),//游戏按钮
        $hook = $("#hook"),//钩子
        data = {count: 5},//次数
        bool = true,//点击开关,ture为可点击
        rem = 75,
        left, right, index,//福袋运动值,福袋下标
        times, timer;

    $(".behind li,.front li").css("width", WIDTH + "px");//设置li的宽度
    init();

    function init() {
        window.requestAnimationFrame = window.requestAnimationFrame || function (fn) {
            return setTimeout(fn, 1000 / 60);//1000 / 60意思是一秒钟60帧
        };
        //cancelAnimationFrame是清除requestAnimationFrame
        window.cancelAnimationFrame = window.cancelAnimationFrame || clearTimeout;

        (function move() {
            //后方福袋的运动
            left = parseFloat($behindBag.css("margin-left"));//获取margin-left并去掉单位
            left = left - 1.5;//改变margin-left值
            $behindBag.css("margin-left", left + "px");
            left <= -WIDTH && $behindBag.css("margin-left", "0");//如果margin-left值到达屏幕宽度则归零

            //前方福袋的运动
            right = parseFloat($frontBag.css("margin-right"));//获取margin-right并去掉单位
            right = right - 1.5;//改变margin-right值
            $frontBag.css("margin-right", right + "px");
            right <= -WIDTH && $frontBag.css("margin-right", "0");//如果margin-right值到达屏幕宽度则归零

            timer = requestAnimationFrame(move);
        })();
    }

    //开始游戏
    $btn.click(function () {
        if (!bool) return;
        if (data.count <= 0) {
            alert("没有次数了");
            return;
        }
        bool = false;
        data.count--;
        $("#change").html(data.count);

        var random = Math.floor(Math.random() * 490 + 130);//130 - 620之间随机,因为这两个值是左右边界
        $(".left,.right").addClass("up");//张开钩子

        $hook.animate({left: random / rem + "rem"}, 1000);//左右值
        clearTimeout(times);
        //上下值
        if (random > 380) {//上面
            $hook.animate({top: -100 / rem + "rem"}, 1000);
            times = setTimeout(function () {
                reback1();
            }, 2000);
        } else {//下面
            $hook.animate({top: 50 / rem + "rem"}, 1000);
            times = setTimeout(function () {
                reback2();
            }, 2000);
        }
    });

    //判断钩子和福袋的距离
    function reback1() {
        for (var i = 0; i < $behindList.length; i++) {
            if ($hook.find("img").length) break;//如果钩子上已经有福袋跳出循环
            if ($hook.position().left - 130 <= $behindList.eq(i).position().left && $behindList.eq(i).position().left <= $hook.position().left - 50) {
                //获取福袋
                !$hook.find("img").length && $hook.append($topBag.eq(i));//如果未抓取到福袋才抓取
                $topBag.eq(i).css({//重设福袋大小
                    width: 91 / rem + "rem",
                    height: 102 / rem + "rem"
                });

                //钩子返回,弹出红包
                index = i;
                $hook.animate({top: -220 / rem + "rem"}, 1000, function () {
                    win();
                    $behindList.eq(index).append($topBag.eq(index));//把福袋放回原位
                    index = undefined;
                });
            } else {
                setTimeout(function () {
                    return reback1();
                });
            }
        }
    }

    function reback2() {
        for (var i = 0; i < $frontList.length; i++) {
            if ($hook.find("img").length) break;//如果钩子上已经有福袋跳出循环
            var l = $frontList.eq(i).position().left - 200;
            if ($hook.position().left - 130 <= l && l <= $hook.position().left) {
                //获取福袋
                !$hook.find("img").length && $hook.append($bottomBag.eq(i));//如果未抓取到福袋才抓取
                $bottomBag.eq(i).css({//重设福袋大小
                    width: 139 / rem + "rem",
                    height: 156 / rem + "rem"
                });

                //钩子返回,弹出红包
                index = i;
                $hook.animate({top: -220 / rem + "rem"}, 1000, function () {
                    win();
                    $frontList.eq(index).append($bottomBag.eq(index));
                    index = undefined;
                });
            } else {
                setTimeout(function () {
                    return reback2();
                });
            }
        }
    }

    //中奖信息提示
    $("#close,.win,.btn").click(function () {
        bool = true;
        $(".left,.right").removeClass("up");
        cancelAnimationFrame(timer);
        init();
    });

    //奖品展示
    var show = new Swiper(".swiper-container", {
        direction: "horizontal",//水平方向滑动。 vertical为垂直方向滑动
        loop: false,//是否循环
        slidesPerView: "auto"//自动根据slides的宽度来设定数量
    });
});










 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值