HTML 之 事件与事件对象和动画

事件

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>事件</title>
        <style type="text/css">
            #redDiv{
                width: 200px;
                height: 200px;
                background-color: red;
            }
        </style>
    </head>
    <body>
        <div id="redDiv"></div>
        <form action="" id="reg">
            <input type="text" id="userName">
            <input type="submit" id="submit">
            <input type="reset" id="reset">
        </form>
    </body>
    <script type="text/javascript">
        var redDiv = document.getElementById("redDiv");
        var reg = document.getElementById("reg");
        var userName = document.getElementById("userName");
        // 鼠标事件
        redDiv.onmousemove = function(){
            console.log("鼠标移动的时候触发");
        }
        redDiv.onclick = function(){
            console.log("单击事件");
        }
        redDiv.ondblclick = function(){
            console.log("双击事件");
        }
        redDiv.onmousedown = function(){
            console.log("鼠标按下事件");
        }
        redDiv.onmouseup = function(){
            console.log("鼠标抬起事件");
        }
        redDiv.oncontextmenu = function(){
            console.log("鼠标右击事件");

            // 阻止默认右键弹出右键栏
            return false;
        }
        // over 和 out 会在鼠标从 redDiv 的父级移到子级的时候触发,先触发 out 再触发 over
        // leave 和 enter 从父级移到子级不会触发,只有鼠标离开/移入整个 redDiv 的时候才会触发
        redDiv.onmouseover = function(){
            console.log("over 移入");
        }
        redDiv.onmouseout = function(){
            console.log("out 移出");
        }
        redDiv.onmouseleave = function(){
            console.log("leave 移出");
        }
        redDiv.onmouseenter = function(){
            console.log("enter 移入");
        }
        // 键盘事件
        document.onkeydown = function(){
            console.log("down键盘按下");
        }
        document.onkeypress = function(){
            console.log("press 键盘按下");
        }
        document.onkeyup = function(){
            console.log("键盘抬起事件");
        }
        // 表单
        userName.onchange = function(){
            console.log("change内容发生改变之后触发");
        }
        userName.oninput = function(){
            console.log("input内容发生变化之后触发");
        }
        userName.onfocus = function(){
            console.log("聚焦");
        }
        userName.onblur = function(){
            console.log("失焦");
        }
        // 给 form 表单绑定事件
        reg.onsubmit = function(){
            console.log("提交");

            // 阻止默认跳转
            return false;
        }
        reg.onreset = function(){
            console.log("重置");

            // 阻止默认重置
            return false;
        }
        // window 事件
        window.onresize = function(){
            console.log("窗口尺寸发生变化的时候触发");
        }
        window.onscroll = function(){
            console.log("窗口内容滚动的时候触发");
        }
        window.onload = function(){
            console.log("窗口加载完毕之后触发");
        }
    </script>
</html>

事件对象

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>事件对象</title>
        <style type="text/css">
            .redDiv{
                width: 200px;
                height: 200px;
                background-color: red;
            }
            p{
                margin: 0;
                height: 100px;
                margin-left: 100px;
                background-color: blue;
            }
        </style>
    </head>
    <body>
        <div class="redDiv">
            <p></p>
        </div>
    </body>
    <script type="text/javascript">
        // 鼠标事件 -> 位置
        var redDiv = document.getElementsByClassName("redDiv")[0];
        redDiv.onclick = function(){
            var evObj = window.event || ev;
            // 触发事件的元素(触发事件的元素不一定是绑定事件的元素)
            console.log(evObj.target);
            // 鼠标距离窗口顶部的坐标
            console.log(evObj.clientY);
            // 鼠标距离页面顶部的坐标
            console.log(evObj.pageY);
            // 鼠标距离 target 顶部的坐标
            console.log(evObj.offsetY);
        }
        // press 区分大小写,但不支持特使按键
        // down 不区分大小写,支持特殊按键
        document.onkeypress = function(ev){
            console.log("press" + ev.keyCode);
        }
        document.onkeydown = function(ev){
            // keyCode 标识哪一个按键
            console.log(ev.keyCode);
            if (ev.keyCode == 66) {
                console.log("按了 B");
            }
            // 判断组合键
            if (ev.keyCode == 66 && ev.metaKey) {
                console.log("按了 command + B");
            }
        }
    </script>
</html>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>拖拽控制元素移动</title>
        <style type="text/css">
            .box{
                width: 1920px;
                height: 1920px;
            }
            .move{
                width: 100px;
                height: 100px;
                background-color: red;
                position: absolute;
                top: 0;
                left: 0;
            }
        </style>
    </head>
    <body>
        <div class="box"><div class="move"></div></div>

    </body>
    <script type="text/javascript">
        var box = document.getElementsByClassName("box")[0];
        var move = document.getElementsByClassName("move")[0];

        box.onmousemove = function(){
            var evObj = window.event;
            move.style.left = - 50 + evObj.pageX + "px" ;
            move.style.top = - 50 + evObj.pageY + "px";
            // console.log(evObj.pageY);
            // console.log(evObj.pageX);
            console.log(move.style.left);
            console.log(move.style.top);
        }
    </script>
</html>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style type="text/css">
            .box{
                width: 1000px;
                height: 500px;
            }
            .move{
                width: 100px;
                height: 100px;
                background-color: red;
                position: absolute;
                top: 450px;
                left: 450px;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <div class="move"></div>
        </div>
    </body>
    <script type="text/javascript">
        var move = document.getElementsByClassName("move")[0];
        document.onkeypress = function(ev){
            // console.log("press" + ev.keyCode);
            // console.log(move.style.top);
            // console.log(move.style.left);
            if (ev.keyCode == 119) {
                move.style.top = move.offsetTop - 5 + "px";
            } else if (ev.keyCode == 115) {
                move.style.top = move.offsetTop + 5 + "px";
            } else if (ev.keyCode == 97) {
                move.style.left = move.offsetLeft - 5 + "px";
            } else if (ev.keyCode == 100) {
                move.style.left = move.offsetLeft + 5 + "px";
            }
            // if (ev.keyCode == 38) {
            //  move.style.top = move.offsetTop - 5 + "px";
            // } else if (ev.keyCode == 40) {
            //  move.style.top = move.offsetTop + 5 + "px";
            // } else if (ev.keyCode == 37) {
            //  move.style.left = move.offsetLeft - 5 + "px";
            // } else if (ev.keyCode == 39) {
            //  move.style.left = move.offsetLeft + 5 + "px";
            // }
        }
    </script>
</html>

动画

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>动画</title>
        <style type="text/css">
            .red{
                width: 100px;
                height: 100px;
                background-color: red;
                position: absolute;
                left: 0;
                top: 0;
                /*动画名称*/
                animation-name: run;
                /*动画时长*/
                animation-duration: 1s;
                /*动画运动方式*/
                animation-timing-function: linear;
                /*动画延时*/
                /*animation-delay: 1s;*/
                /*动画次数*/
                /*infinite 无限*/
                animation-iteration-count: 1;
                /*动画方向*/
                /*reverse 反向*/
                /*normal 正常*/
                /*alternate 单数正向,双数反向*/
                /*alternate-reverse 双数正向,单数反向*/
                animation-direction: normal;
                /*
                    forwards 动画结束的时候停留在当前位置
                    backwards 在动画延时期间,元素的位置在动画起始的位置
                    both 包含以上两个效果
                */
                animation-fill-mode: both;
            }
            @keyframes run{
                0%{
                    left: 0;
                }
                50%{
                    left: 50px;
                }
                100%{
                    left: 1080px;
                    top: 500px;
                }
            }
        </style>
    </head>
    <body>
        <dic class="red"></dic>
    </body>
</html>
<!doctype html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>动画版轮播图</title>
        <style type="text/css">
            @keyframes run{
                0%{
                    transform: translateX(0px);
                }
                20%{
                    transform: translateX(0px);
                }
                25%{
                    transform: translateX(-20%);
                }
                45%{
                    transform: translateX(-20%);
                }
                50%{
                    transform: translateX(-40%);
                }
                70%{
                    transform: translateX(-40%);
                }
                75%{
                    transform: translateX(-60%);
                }
                95%{
                    transform: translateX(-60%);
                }
                100%{
                    transform: translateX(-80%);
                }
            }
            .wrap{
                width: 60%;
                height: 380px;
                border: solid 5px black; 
                /*overflow: hidden;*/
            }
            .box{
                height: 100%;
                width: 500%;
                animation: 10s linear run infinite;
            }
            .box .item{
                width: 20%;
                height: 100%;
                float: left;
                text-align: center;
                color: white;
                font-size: 70px;
                display: flex;
                align-items: center;
                justify-content: center;
            }
            .item1{
                background-color: red;
            }
            .item2{
                background-color: blue;
            }
            .item3{
                background-color: green;
            }
            .item4{
                background-color: pink;
            }

        </style>
    </head>
    <body>
        <div class="wrap">
            <div class="box">
                <div class="item item1">1</div>
                <div class="item item2">2</div>
                <div class="item item3">3</div>
                <div class="item item4">4</div>
                <div class="item item1">1</div>
            </div>
        </div>
    </body>
</html>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>方向键控制移动</title>
        <style type="text/css">
            .box{
                position: relative;
                border: 5px yellow solid;
                margin: 20px auto;
                width: 600px;
                height: 480px;
                background: url("bg.png") 100%;
                animation: run 2s linear infinite;
            }
            @keyFrames run{
                from{
                    background-position: 0 0;
                }
                to{
                    background-position: 0 960px;
                }
            }
            .item{
                width: 45px;
                height: 80px;
                background-color: purple;
                position: absolute;
                top: 0;
                left: 0;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <div class="item"></div>
        </div>
    </body>
    <script type="text/javascript">
        var item = document.getElementsByClassName("item")[0];
        var box = document.getElementsByClassName("box")[0];
        var key = [false, false, false, false];
        document.onkeydown = function(ev){
            if (ev.keyCode > 36 && ev.keyCode < 41) {
                key[ev.keyCode - 37] = true;
            }
        }
        document.onkeyup = function(ev){
            key[ev.keyCode - 37] = false;
        }
        setInterval(function(){
            var x = item.offsetLeft + (key[0] * - 1) + key[2];
            var y = item.offsetTop + (key[1] * - 1) + key[3];
            var maxX = box.clientWidth- item.offsetWidth;
            x = x < 0 ? 0 : (x > maxX ? maxX : x);
            var maxY = box.clientHeight- item.offsetHeight;
            y = y < 0 ? 0 : (y > maxY ? maxY : y);
            item.style.left = x + "px";
            item.style.top = y + "px";
        }, 20);
    </script>
</html>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>拖拽</title>
        <style type="text/css">
            .item{
                width: 100px;
                height: 100px;
                background-color: purple;
                position: absolute;;
                left: 0;
                top: 0;
            }
            .box{
                width: 150px;
                height: 150px;
                background-color: pink;
                margin: 100px auto;
            }
            .item1{
                width: 100px;
                height: 100px;
                border-radius: 50px;
                background-color: purple;
                position: absolute;
                left: 200;
                top: 200;
            }
            .box1{
                width: 150px;
                height: 150px;
                border-radius: 75px;
                background-color: pink;
                margin: 200px auto;
            }
        </style>
    </head>
    <body>
        <div class="item"></div>
        <div class="box"></div>
        <div class="item1"></div>
        <div class="box1"></div>
    </body>
    <script type="text/javascript">
        var item = document.getElementsByClassName("item")[0];
        var box = document.getElementsByClassName("box")[0];
        var item1 = document.getElementsByClassName("item1")[0];
        var box1 = document.getElementsByClassName("box1")[0];

        item.onmousedown = function(e){
            var x = e.offsetX;
            var y = e.offsetY;
            document.onmousemove = function(ev){
                item.style.left = ev.pageX - x + "px";
                item.style.top = ev.pageY - y + "px";
                if (item.offsetLeft + item.offsetWidth > box.offsetLeft && item.offsetLeft < box.offsetLeft + box.offsetWidth && item.offsetTop + item.offsetHeight > box.offsetTop && item.offsetTop < box.offsetTop + box.offsetHeight) {
                    box.style.backgroundColor = "#00FF00";
                } else{
                    box.style.backgroundColor = "pink";
                }
            }
        }
        item1.onmousedown = function(e){
            var x = e.offsetX;
            var y = e.offsetY;
            document.onmousemove = function(ev){
                item1.style.left = ev.pageX - x + "px";
                item1.style.top = ev.pageY - y + "px";
                var A = Math.abs((item1.offsetLeft + item1.offsetWidth / 2) - (box1.offsetLeft + box1.offsetWidth / 2));
                var B = Math.abs((item1.offsetTop + item1.offsetHeight / 2) - (box1.offsetTop + box1.offsetHeight / 2));
                var C = Math.sqrt(A * A + B * B);
                // console.log(A);
                // console.log(B);
                // console.log(C);
                if (C <= item1.offsetWidth / 2 + box1.offsetWidth / 2){
                    box1.style.backgroundColor = "#00FF00";
                } else{
                    box1.style.backgroundColor = "pink";
                }
            }
        }
        document.onmouseup = function(){
            document.onmousemove = null;
        }
    </script>
</html>
<!doctype html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>打飞机</title>
        <style type="text/css">
            .box{
                position: relative;
                border: 5px black solid;
                width: 300px;
                height: 480px;
                margin: 20px auto;
                background: url("bg.png") 0px 0px / 100%;
                animation:run 4s linear infinite;
            }
            @keyframes run{
                from{
                    background-position: 0px 0px;
                }
                to{
                    background-position: 0px 960px;
                }
            }

            .item{
                width: 60px;
                height: 60px;
                background-color: gray;
                position: absolute;
                top: 0px;
                left: 0px;
                background: url("fj.jpeg") 0px 0px / 100% ;
            }
            .zd{
                width: 5px;
                height: 20px;
                background-color: red;
                position: absolute;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <div class="item"></div>
        </div>

    </body>
    <script type="text/javascript">
        var item = document.getElementsByClassName('item')[0];
        var box = document.getElementsByClassName('box')[0];
        var key = [false, false, false, false];

        document.onkeydown = function(ev){
            key[ev.keyCode - 37] = true;
        }

        document.onkeyup = function(ev){
            key[ev.keyCode - 37] = false;
        }
        setInterval(function(){
            var speed = 3;
            var iLeft = item.offsetLeft + ((key[0]* -1) + key[2]) * speed;
            var iTop = item.offsetTop + ((key[1]* -1) + key[3]) *speed;
            // 限制Left
            var maxX = box.clientWidth - item.offsetWidth;
            iLeft = iLeft < 0 ? 0 : (iLeft > maxX ? maxX : iLeft);
            // 限制Top
            var maxY = box.clientHeight - item.offsetHeight;
            iTop = iTop < 0 ? 0 : (iTop > maxY ? maxY : iTop);

            item.style.left =  iLeft + "px";
            item.style.top =  + iTop + "px";
        }, 20);


        // 每隔0.5创建子弹
        setInterval(function(){
            var zd = document.createElement("div");
            zd.className = "zd";
            box.appendChild(zd);    
            zd.style.top = item.offsetTop - zd.offsetHeight + "px";
            zd.style.left = item.offsetLeft+item.offsetWidth/2 - zd.offsetWidth/2 + "px";
            var move = setInterval(function(){
                zd.style.top = zd.offsetTop - 3 + "px";
                if (zd.offsetTop <= -zd.offsetHeight) {
                    zd.remove();
                    clearInterval(move);
                }
            }, 20);
        }, 500);
    </script>
</html>

http://blog.csdn.net/huzongnan/article/list

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值