ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2021.8.10今日小结

2021-08-10 20:32:40  阅读:150  来源: 互联网

标签:function 10 2021.8 小结 100px window document box1 event


今天学习了几个基本的事件问题,鼠标滚轮事件,用键盘控制div移动,键盘事件,比如可以禁用从键盘输入时,输入框不接受某些数字或者字母。拖拽div,定时器。

鼠标滚轮事件
//鼠标滚轮事件
<style> #box1{ width: 100px; height: 100px; background-color: red; } body{ height: 2000px; } </style> <script> window.onload = function(){ let box1 = document.getElementById("box1"); box1.onmousewheel = function(event){ event = event || window.event; if(event.wheelDelta>0 || event.detail<0){ box1.style.height = box1.clientHeight - 10 +"px"; }else{ box1.style.height = box1.clientHeight + 10 +"px"; } event.preventDefault && event.preventDefault(); return false; }; bind(box1,"DOMMouseScroll",box1.onmousewheel); }; function bind(obj ,eventStr , callback){ if(obj.addEventListener){ // 大部分浏览器的兼容方式 obj.addEventListener(eventStr , callback ,false); }else{ // this是谁由调用方式决定 // IE8及以下 obj.addEventListener("on"+eventStr,function(){ callback.call(obj); }); } }; </script> </head> <body> <div id="box1"></div> </body>

 

用键盘控制div移动   
//用键盘控制div移动   
<style> #box1{ width: 100px; height: 100px; background-color: red; position: absolute; } </style> <script> window.onload = function(){ document.onkeydown =function(event){ event = event || window.event; let speed =10; if(event.ctrlKey){ speed = 50; } //37左38上39右40下 switch(event.keyCode){ case 37: box1.style.left = box1.offsetLeft -speed +"px"; break; case 38: box1.style.top = box1.offsetTop -speed +"px"; break; case 39: box1.style.left = box1.offsetLeft +speed +"px"; break; case 40: box1.style.top = box1.offsetTop +speed +"px"; break; } }; }; </script> </head> <body> <div id="box1"></div> </body>

 

 

键盘事件

<script>
        window.onload = function(){
            

            document.onkeydown = function(){
                event = event || window.event;

                if(event.keyCode === 89 && event.ctrlKey){
                    console.log("ctrl和y都被按下");
                }
            };
            // document.onkeyup = function(){
                
            // };


            let input = document.getElementsByTagName("input")[0];
            input.onkeydown = function(event){
                event = event || window.event;
                if(event.keyCode>=48 && event.keyCode<=57){
                    return false;
                }
            };
        };
    </script>
</head>
<body>
    <input type="text">
</body>

 

div拖拽

<style>
        #box1{
            height: 100px;
            width: 100px;
            background-color: red;
            position: absolute;
        }
        #box2{
            height: 100px;
            width: 100px;
            background-color: yellow;
            position: absolute;
            top: 100px;
            left: 100px;
        }
    </style>
    <script>
        window.onload = function(){
            let box1 = document.getElementById("box1");
            
            drag(box1);

            drag(box2);
            
            //提取一个专门用来拖拽的函数
            function drag(obj){
                obj.onmousedown = function(event){

                    event = event || window.event;

                        let ol = event.clientX - obj.offsetLeft;
                        let ot = event.clientY - obj.offsetTop;



                            document.onmousemove = function(event){
                                event = event || window.event;

                                let left = event.clientX-ol;
                                let top = event.clientY-ot;

                                obj.style.left = left + "px";
                                obj.style.top = top +"px";
                            };
                            document.onmouseup = function(){
                                document.onmousemove = null;
                                document.onmouseup = null;
                            };
                            //取消默认行为
                            return false;
                            };
                    };
        };
    </script>
</head>
<body>
    <div id="box1"></div>
    <div id="box2"></div>
</body>

 

 

定时器,比如图片轮播图时候就可以使用。


setInterval(function(){
           index++;
          //如果index计数器大于了图片数量将重新开始 if(index>=imgArr.length){ index=0; }
img.src = imgArr[index]; },2000);//时间两秒钟一次

 

标签:function,10,2021.8,小结,100px,window,document,box1,event
来源: https://www.cnblogs.com/Privatexaio/p/15125698.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有