js_事件(2)

拖拽实例

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <title>Examples</title>
        <style type="text/css">
            #box{
                width: 100px;
                height: 100px;
                background: red;
                position: absolute;
                border-radius: 50px;
            }
        </style>
        <script type="text/javascript">
            //第一步:鼠标点击下去
            //第二步:鼠标开始移动,然后div跟着鼠标一起动
            //第三步:鼠标抬起,拖拽结束
            window.onload = function(){
                var oDiv = document.getElementById("box");
                oDiv.onmousedown = function(ev){
                    var oEvent = ev || event;
                    var oDivX = oEvent.clientX - oDiv.offsetLeft;   //获取鼠标点下去时与div的相对距离
                    var oDivY = oEvent.clientY - oDiv.offsetTop;

                    document.onmousemove = function(ev){           //移动事件,只要鼠标移动就会触发
                        var oEvent = ev || event;
                        var X = oEvent.clientX;
                        var Y = oEvent.clientY;
                        // document.title = "X:"+X+"  Y:"+Y;

                        oDiv.style.left = X - oDivX + "px";
                        oDiv.style.top = Y - oDivY + "px";
                    }
                    document.onmouseup = function(){
                        document.onmousemove = null;
                        document.onmouseup = null;
                    }
                    //return false;  //阻止默认行为
                }


            }

        </script>
    </head>
    <body>

        <div id="box"></div>

    </body>
</html>

onresize事件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <title>Examples</title>
        <style type="text/css">

        </style>
        <script type="text/javascript">

            //onresize事件:当窗口或者框架的大小变化时在window或者框架上触发
            var num = 0;
            window.onload = window.onresize = function(){

                document.title = ++num;
            }

        </script>
    </head>
    <body>

    </body>
</html>

onscroll事件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <title>Examples</title>
        <style type="text/css">

        </style>
        <script type="text/javascript">

            //onscroll事件:当滚动条变化时就会触发;
            var num = 0;
            window.onload = window.onscroll = function(){

                document.title = ++num;
            }

        </script>
    </head>
    <body style="height:5000px">

    </body>
</html>

(onscroll事件实例1)div居中向右显示

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <title>Examples</title>
        <style type="text/css">
            *{
                margin: 0px;
                padding: 0px;
            }
            #box{
                width: 100px;
                height: 100px;
                background: red;
                position: absolute;
                right: 0px;
            }
        </style>
        <script type="text/javascript">

            window.onload = function(){

                var oDiv = document.getElementById("box");
                var H = document.documentElement.clientHeight || document.body.clientHeight;

                oDiv.style.top = (H-oDiv.clientHeight)/2 + "px";

            }

        </script>
    </head>
    <body>

        <div id="box"></div>

    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <title>Examples</title>
        <style type="text/css">
            *{
                margin: 0px;
                padding: 0px;
            }
            #box{
                width: 100px;
                height: 100px;
                background: red;
                position: absolute;
                right: 0px;
            }
        </style>
        <script type="text/javascript">

            window.onload = window.onscroll = function(){

                var oDiv = document.getElementById("box");
                var H = document.documentElement.clientHeight || document.body.clientHeight;

                var num = document.documentElement.scrollTop || document.body.scrollTop

                oDiv.style.top = (H-oDiv.clientHeight)/2 + num + "px";

            }

        </script>
    </head>
    <body style="height:5000px">

        <div id="box"></div>

    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <title>Examples</title>
        <style type="text/css">
            *{
                margin: 0px;
                padding: 0px;
            }
            #box{
                width: 100px;
                height: 100px;
                background: red;
                position: absolute;
                right: 0px;
            }
        </style>
        <script type="text/javascript">

            window.onload = window.onscroll = function(){

                var oDiv = document.getElementById("box");
                var H = document.documentElement.clientHeight || document.body.clientHeight;

                var num = document.documentElement.scrollTop || document.body.scrollTop

                var end = (H-oDiv.clientHeight)/2 + num;

                //作业:完善向上拉滑动的效果
                //注意以下几个小问题:
                //1:定时器的关闭问题
                //2:oDiv.offsetTop取值的问题
                setInterval(function(){
                    if(oDiv.offsetTop >= end){
                        oDiv.style.top = oDiv.offsetTop + 0 + "px";
                    }else{
                        oDiv.style.top = oDiv.offsetTop + 1 + "px";
                    }
                },1);

            }

        </script>
    </head>
    <body style="height:5000px">

        <div id="box"></div>

    </body>
</html>

运动停止实例

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <title>Examples</title>
        <style type="text/css">
            #box{
                width: 100px;
                height: 100px;
                background: red;
                position: absolute;

            }
        </style>
        <script type="text/javascript">

            window.onload = function(){

                var oDiv = document.getElementById("box");

                setInterval(function(){
                    if(oDiv.offsetLeft >= 900){
                        oDiv.style.left = oDiv.offsetLeft + 0 + "px";
                    }else{
                        oDiv.style.left = oDiv.offsetLeft + 10 + "px";
                    }
                },100);

            }

        </script>
    </head>
    <body>

        <div id="box"></div>

    </body>
</html>

事件冒泡

<!DOCTYPE html>
<!--
    事件流:事件流就是描述页面接收事件的顺序,比如给几个层叠在一起的元素添加点击事件,那么当点击最里层的元素时,这时并不仅仅
            只触发点的点击的最里层的那个元素,而是层叠在点击范围内的元素都会触发,
    事件流有两种模式:冒泡与捕获

    事件冒泡:从里向外挨个触发, 现代的浏览器默认情况都是冒泡模型
    事件捕获:是从外向里挨个触发

 -->
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <title>Examples</title>
        <style type="text/css">
            #div1{
                width: 500px;
                height: 500px;
                background: red;
            }
            #div2{
                width: 100px;
                height: 100px;
                background: blue;
                position: absolute;
                top: 200px;
                left: 200px;
            }
        </style>
        <script type="text/javascript">

            window.onload = function(){

                var oDiv1 = document.getElementById("div1");
                var oDiv2 = document.getElementById("div2");

                oDiv1.onclick = function(){
                    alert("div1事件111111111111111");
                }
                oDiv2.onclick = function(){
                    alert("div2事件222222222222222");
                }

                document.body.onclick = function(){
                    alert("body事件333333333333333");
                }

            }

        </script>
    </head>
    <body>

        <div id="div1">
            <div id="div2">

            </div>
        </div>

    </body>
</html>

阻止冒泡

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <title>Examples</title>
        <style type="text/css">

        </style>
        <script type="text/javascript">

            window.onload = function(){

            }

        </script>
    </head>
    <body><!DOCTYPE html>
<!--

    事件流:事件流就是描述页面接收事件的顺序,比如给几个层叠在一起的元素添加点击事件,那么当点击最里层的元素时,这时并不仅仅
            只触发点的点击的最里层的那个元素,而是层叠在点击范围内的元素都会触发,
    事件流有两种模式:冒泡与捕获

    事件冒泡:从里向外挨个触发, 现代的浏览器默认情况都是冒泡模型
    事件捕获:是从外向里挨个触发,

 -->
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <title>Examples</title>
        <style type="text/css">
            #div1{
                width: 500px;
                height: 500px;
                background: red;
            }
            #div2{
                width: 100px;
                height: 100px;
                background: blue;
                position: absolute;
                top: 200px;
                left: 200px;
            }
        </style>
        <script type="text/javascript">

            window.onload = function(){

                var oDiv1 = document.getElementById("div1");
                var oDiv2 = document.getElementById("div2");

                oDiv1.onclick = function(){
                    alert("div1事件111111111111111");
                }
                oDiv2.onclick = function(ev){
                    var oEvent = ev || event;
                    //oEvent.stopPropagation();           //阻止冒泡  不兼容i8
                    //oEvent.cancelBubble = true;         //阻止冒泡  兼容i8

                    if(oEvent.stopPropagation){    //阻止冒泡
                        oEvent.stopPropagation();
                    }else{
                        oEvent.cancelBubble = true;
                    }

                    alert("div2事件222222222222222");
                }

            }

        </script>
    </head>
    <body>

        <div id="div1">
            <div id="div2">

            </div>
        </div>

    </body>
</html>

    </body>
</html>

事件绑定(一)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <title>Examples</title>
        <style type="text/css">
            #box{
                width: 100px;
                height: 100px;
                background: red;
            }
        </style>
        <script type="text/javascript">

            window.onload = function(){

                // var oDiv = document.getElementById("box");
                // oDiv.onclick = function(){
                //     alert("第一个点击事件");
                // }
                // oDiv.onclick = function(){      //第二次添加的事件会把第一次添加覆盖掉
                //     alert("第二个点击事件");
                // }


                //解决覆盖问题
                // var oDiv = document.getElementById("box");
                // oDiv.onclick = function(){
                //     alert("第一个点击事件");
                // }

                // if(typeof oDiv.onclick){  //判断oDiv有没有点击事件
                //     var fin = null;
                //     fin = oDiv.onclick;
                // }

                // oDiv.onclick = function(){
                //     fin();
                //     alert("第二个点击事件");
                // }



                //事件绑定
                //自定义事件 addEventListener(要处理的事件名,事件处理程序的函数,布尔值);
                //第三个参数布尔值:如果值是true就表示在捕获阶段调用事件处理函数
                //                  如果值是false就表示在冒泡阶段调用事件处理函数
                var oDiv = document.getElementById("box");
                oDiv.addEventListener("click", function(){alert("第一次添加的事件")}, false);
                oDiv.addEventListener("click", function(){alert("第二次添加的事件")}, false);


            }

        </script>
    </head>
    <body>

        <div id="box"></div>

    </body>
</html>

事件绑定(二)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <title>Examples</title>
        <style type="text/css">
            #div1{
                width: 500px;
                height: 500px;
                background: red;
            }
            #div2{
                width: 100px;
                height: 100px;
                background: blue;
                position: absolute;
                left: 200px;
                top: 200px;
            }
        </style>
        <script type="text/javascript">

            window.onload = function(){

                //事件绑定
                //自定义事件 addEventListener(要处理的事件名,事件处理程序的函数,布尔值);
                //第三个参数布尔值:如果值是true就表示在捕获阶段调用事件处理函数
                //                  如果值是false就表示在冒泡阶段调用事件处理函数


                var oDiv1 = document.getElementById("div1");
                var oDiv2 = document.getElementById("div2");

                // oDiv1.addEventListener("click",function(){alert("div1的点击事件11111")},false);
                // oDiv2.addEventListener("click",function(){alert("div2的点击事件22222")},false);

                oDiv1.addEventListener("click",function(){alert("div1的点击事件11111")},true);
                oDiv2.addEventListener("click",function(){alert("div2的点击事件22222")},true);

                oDiv1.addEventListener("click",function(){alert("div1的点击事件33333")},false);
                oDiv2.addEventListener("click",function(){alert("div2的点击事件44444")},false);

            }

        </script>
    </head>
    <body>

        <div id="div1">
            <div id="div2"></div>
        </div>

    </body>
</html>

事件绑定(三)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <title>Examples</title>
        <style type="text/css">
            #box{
                width: 100px;
                height: 100px;
                background: red;
            }
        </style>
        <script type="text/javascript">

            window.onload = function(){


                //事件绑定
                //自定义事件 addEventListener(要处理的事件名,事件处理程序的函数,布尔值);
                //第三个参数布尔值:如果值是true就表示在捕获阶段调用事件处理函数
                //                  如果值是false就表示在冒泡阶段调用事件处理函数



                var oDiv = document.getElementById("box");
                //oDiv.addEventListener("click", function(){alert("第一次添加的事件")}, false); // addEventListener() 不兼容i8

                // attachEvent("事件处理程序名称",事件处理程序的函数)  //可以ie  但不兼容Ie11
                // oDiv.attachEvent("onclick",function(){alert("第一次添加的事件")});


                //兼容处理
                if(oDiv.addEventListener){
                    oDiv.addEventListener("click", function(){alert("第一次添加的事件")}, false);
                }else{
                    oDiv.attachEvent("onclick",function(){alert("第一次添加的事件")});
                }

            }

        </script>
    </head>
    <body>

        <div id="box"></div>

    </body>
</html>

事件绑定(解除绑定)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <title>Examples</title>
        <style type="text/css">
            #box{
                width: 100px;
                height: 100px;
                background: red;
            }
        </style>
        <script type="text/javascript">

            window.onload = function(){

                function show(){
                    alert("div被点击了");
                }
                function move(){
                    alert("鼠标移入了");
                }

                var oDiv = document.getElementById("box");

                //addEventListener();
                //attachEvent

                //绑定事件
                //oDiv.addEventListener("click",show,false);   //绑定点击事件
                //oDiv.addEventListener("mouseover",move,false);

                //解除事件绑定:  removeEventListener()
                //oDiv.removeEventListener("click",show,false);
//****************************************************************************************************************
                //绑定事件
                oDiv.attachEvent("onclick",show);
                //解除事件绑定
                oDiv.detachEvent("onclick",show);
            }
        </script>
    </head>
    <body>
        <div id="box"></div>
    </body>
</html>

解除绑定(匿名函数问题)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <title>Examples</title>
        <style type="text/css">
            #box{
                width: 100px;
                height: 100px;
                background: red;
            }
        </style>
        <script type="text/javascript">

            window.onload = function(){

                var oDiv = document.getElementById("box");

                function show(){
                    alert("胖胖");
                }

                //解除事件绑定时,匿名函数不能被解除
                // oDiv.addEventListener("click",function(){alert("胖胖");},false);
                // oDiv.removeEventListener("click",function(){alert("胖胖");},false);

                oDiv.addEventListener("click",show,false);
                oDiv.removeEventListener("click",show,false);
            }

        </script>
    </head>
    <body>
        <div id="box"></div>
    </body>
</html>

事件绑定(this问题)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <title>Examples</title>
        <style type="text/css">
            #box{
                width: 100px;
                height: 100px;
                background: red;
            }
        </style>
        <script type="text/javascript">

            window.onload = function(){

                var oDiv = document.getElementById("box");

                // oDiv.addEventListener("click",show,false);

                oDiv.attachEvent("onclick",show);

                function show(){
                    alert(this);//用attachEvent绑定中的this,指向的是window  在ie10以下支持
                }
            }
        </script>
    </head>
    <body>
        <div id="box"></div>
    </body>
</html>

封装绑定

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <title>Examples</title>
        <style type="text/css">
            #box{
                width: 100px;
                height: 100px;
                background: red;

            }
        </style>
        <script type="text/javascript">

            window.onload = function(){

                var oDiv = document.getElementById("box");

                function show(){
                    alert("胖胖");
                }
                function move(){
                    alert("鼠标移入了");
                }

                //封装绑定点击事件
                function addEvent(obj,type,fin){
                    if(obj.addEventListener){
                        obj.addEventListener(type,fin,false);
                    }else{
                        obj.attachEvent("on"+type,fin);
                    }
                }
                addEvent(oDiv,"mouseover",move);


                //封装解除
                function removeEvent(obj,type,fin){
                    if(obj.removeEventListener){
                        obj.removeEventListener(type,fin);
                    }else{
                        obj.detachEvent("on"+type,fin);
                    }
                }
                removeEvent(oDiv,"mouseover",move);

            }

        </script>
    </head>
    <body>

        <div id="box"></div>

    </body>
</html>

修改键

<!DOCTYPE html>
<!--

    shiftKey属性:判断是否按下了shift键
    ctrlKey属性:判断是否按下了ctrl键
    altkey属性:判断是否按下了alt键

 -->
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <title>Examples</title>
        <style type="text/css">
            #box{
                width: 100px;
                height: 100px;
                background: red;
            }
        </style>
        <script type="text/javascript">

            window.onload = function(){

                var oDiv = document.getElementById("box");
                oDiv.onclick = function(ev){

                    var oEvent = ev || event;
                    if(oEvent.ctrlKey && oEvent.altKey){
                        oDiv.style.width = "500px";
                    }
                }

            }

        </script>
    </head>
    <body>
        <div id="box"></div>
    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <title>Examples</title>
        <style type="text/css">
            #box{
                width: 100px;
                height: 100px;
                background: red;
                display: none;
            }
        </style>
        <script type="text/javascript">

            window.onload = function(){

                var oBtn = document.getElementById("btn");
                var oDiv = document.getElementById("box");


                oBtn.onclick = function(ev){
                    oDiv.style.display = "block";
                    var oEvent = ev || event;
                    if(oEvent.stopPropagation){    //阻止事件冒泡
                        oEvent.stopPropagation();
                    }else{
                        oEvent.cancelBubble = true;
                    }
                }


                document.onclick = function(){
                    oDiv.style.display = "none";
                }

            }

        </script>
    </head>
    <body>

        <button id="btn">确定</button>
        <div id="box"></div>

    </body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值