2021.8.10今日小結

今天學習了幾個基本的事件問題,鼠標滾輪事件,用鍵盤控制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>

 

 

定時器,好比圖片輪播圖時候就能夠使用。this

 setInterval(function(){          
                index++;          //若是index計數器大於了圖片數量將從新開始
                if(index>=imgArr.length){
                    index=0;
                }
                img.src = imgArr[index];
            },2000);//時間兩秒鐘一次
相關文章
相關標籤/搜索