6_30 事件委托&简易版飞机大战小游戏

1.事件委托
1.1 概念

当页面元素不存在的时候,而此时想先给未知的元素绑定事件,但是单纯的绑定事件又不能触发,那是因为未找到的绑定的对象,所以此时你需要将事件委托给其祖先元素。

1.2 例子

//css部分
<link rel="stylesheet" href="https://www.bootcss.com/p/buttons/css/buttons.css">
    <style>
        .button {
            margin-right: 10px;
        }
    </style>
    //body内的内容
<body>
    <div class="container">
        <button class="button button-primary button-pill button-small">ADD a Button</button>
    </div>

    <script src="./js/jquery.js"></script>
    <script>
        $(".container").on("click", "button", function () {
            let bt = $("<button/>").html("ADD a Button").addClass(
                "button button-primary button-pill button-small");
            $(".container").append(bt)
        })
    </script>
</body>

1.3 例子的运行结果

初始图
初始图
点击添加后的结果在这里插入图片描述
2.简易版飞机大战小游戏
2.1 css代码

<style>
        .container {
            width: 500px;
            height: 80vh;
            margin: 10vh auto;
            background-color: black;
            position: relative;
            overflow: hidden;
        }

        .plane {
            width: 100px;
            height: 50px;
            /* background-color: red; */
            position: absolute;
            left: calc(50% - 40px);
            bottom: 10px;

        }

        .bullet {
            width: 20px;
            height: 10px;
            background-color: red;
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值