Why is this not working ? pushing a box using mouse pointer

后端 未结 4 1260
旧时难觅i
旧时难觅i 2021-01-28 16:23

I\'m a beginner in Javascript, and trying to make a simple script that pushes a box using the mouse pointer. But unfortunately it isn\'t working for some reason, i hope you guys

4条回答
  •  [愿得一人]
    2021-01-28 16:37

    Lastly you are better off adding stuff onload instead of having the script live in the body

    Here is a script that lives in the head of the page, the rest of the issues are already solved by other ppl here

    var pushBox = function(e){
        if(e.pageX >= box.offsetLeft){
            box.style.left = (parseInt(box.style.left,10) + 1) + "px";
        }
    },box;
    window.onload=function() {
      box = document.getElementById("box");
      document.addEventListener("mousemove" , pushBox);
    }
    

提交回复
热议问题