• 原生JS书写点击按钮让元素移动、停止


    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>点击按钮让元素移动、停止</title>
    <style>
    div{
    width: 100px;
    height: 100px;
    background: red;
    position: absolute;
    border-radius: 50px;
    }
    </style>
    </head>
    <body>
    <button id="btn">开始</button>
    <div id="myDiv" style="top: 50px;left:10px"></div>
    <script>
    let btnMy = document.getElementById("btn");
    let btnDiv = document.getElementById("myDiv");
    let toRight ='on';
    let toBottom ='on';
    //左右移动
    let move = function(){
    let divLeft = parseInt(myDiv.style.left);
    if(toRight == 'on' && divLeft < innerWidth -100){
    myDiv.style.left = parseInt(divLeft) + 1 +'px';
    }else if(divLeft == innerWidth - 100 || toRight == 'off'){
    toRight = 'off';
    myDiv.style.left = parseInt(divLeft) - 1 + 'px';
    if(divLeft == 0){
    toRight = 'on'
    }
    }
    // 上下移动
    let divTop = parseInt(myDiv.style.top)
    if(toBottom == 'on' && divTop < innerHeight -100){
    myDiv.style.top = parseInt(divTop) + 1 +'px';
    }else if(divTop == innerHeight - 100 || toBottom == 'off'){
    toBottom ='off';
    myDiv.style.top = parseInt(divTop) - 1 + 'px';
    if(divTop == 0){
    toBottom ='on'
    }
    }
    }
    // 绑定按钮跟需要移动的元素
    let stop;
    btnMy.onclick = function(){
    console.log(btn.innerText)
    if(btn.innerText == '开始'){
    btn.innerText = '暂停'
    stop = setInterval(move,1)
    }else{
    btn.innerText = '开始';
    clearInterval(stop)
    }
    }
    </script>
    </body>
    </html>
  • 相关阅读:
    python调用php函数
    api响应类
    php 利用socket发送GET,POST请求
    php的异常和处理
    predis操作大全
    PHP 实现HASH表
    PHP 实现Session入库/存入redis
    PHP下使用Redis消息队列发布微博
    jquery 仿163网易图片新闻幻灯片展示
    6种非常酷炫的jquery banner焦点图片幻灯片切换
  • 原文地址:https://www.cnblogs.com/lyl-0667/p/11286766.html
Copyright © 2020-2023  润新知