2017-09-21 94 views
-1

我想在我的鼠標點擊的位置做一個gif img,但由於某種原因gif不想到我請求的位置style.top和style.left。有人能幫我嗎?位置:絕對不是與style.top和style.left工作

這裏我嘗試創建並將鼠標點擊放置gif img。

setInterval(checkCursor, 1); 
    function checkCursor(){ 
     document.body.onclick = function(){ 
      console.log(parseFloat(cursorX) + ', ' + cursorY); 

      var explo = document.createElement("img"); 
      explo.src = "explosive.gif?" + new Date().getTime(); 
      explo.style.position = "absolute"; 
      explo.style.top = cursorY; 
      explo.style.left = cursorX; 
      explo.style.pointerEvents = "none"; 
      document.body.appendChild(explo); 
      setTimeout(function(){explo.remove();}, 800); 
     } 
    } 

在這裏我要尋找的鼠標位置

var cursorX; 
     var cursorY; 
     document.onmousemove = function(e){ 
      cursorX = e.pageX-100; 
      cursorY = e.pageY-100; 
     } 

-100是集中在鼠標

這是所有代碼的gif文件:

<!DOCTYPE html> 
<html> 
    <head> 
     <title> Menu </title> 
     <style> 
      #nav { 
       display: block; 
       position: fixed; 
       top: 0; 
       left: 0; 
       overflow-x: hidden; 
       transition: 0.5s; 
       background-color: grey; 
       color: black; 
       width: 0; 
       height: 100%; 
      } 
      nav a { text-decoration: none; cursor: pointer; display: block; color: white; padding-top: 5px; padding-bottom: 5px; padding-right: 50px; padding-left: 30px; } 
      nav a:hover { background-color: black; transition: 0.3s; } 
      #menu { transition: 0.3s; } 
      section { position: absolute; z-index: -1; width: 99%; height: 98%; } 

      #header { 
       background-color: red; 
       color: black; 
       text-align: center; 
       margin-left: 25%; 
       width: 50%; 
      } 
     </style> 
    </head> 
    <body bgcolor="black"> 
     <video id="videoplayback" style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; opacity: 0.0;" src ="videoplayback.mp4" loop/></video> 
     <section id="body"> 
      <nav id="nav"> 
       <span id="menu">Menu</menu> 
       <a id="btncls"onclick="closeMenu()" style="float: right; padding: 5px; margin: 5px; font-size: 10px;">X</a> 
       <div style="top: 50px; position: fixed;" id="menu"> 
        <a onclick="openIndex()">Home</a> 
        <a onclick="openClub()">Club</a> 
        <a href="http://www.helenpark.nl">Helenparkhurst</a> 
       </div> 
      </nav> 
      <section id="index"> 
       <header> 
        <button onclick="openMenu()">Open menu</button> 
       </header> 
      </section><section id="club"> 
       <header> 
        <button onclick="openMenu()">Open menu</button> 
       </header><article> 
        <div id="choice" style="width: 100%; height: 100%;"><button onclick="join()">Join</button> | <button id="leave" onclick="leave()">Never</button></div> 
        <p id="header"> 
         <span style="font-size: 32px">Welcome to the Megumin club !!!</span> 
        </p> 
       </article><footer> 

       </footer> 
      </section> 
     </section> 
     <script> 
      document.getElementById("menu").style.visibility = "hidden"; 

      document.getElementById("club").style.visibility = "hidden"; 

      function openIndex(){ 
       document.getElementById("club").style.visibility = "hidden"; 
       document.getElementById("index").style.visibility = "visible"; 

       closeMenu(); 
      } 
      function openClub(){ 
       document.getElementById("index").style.visibility = "hidden"; 
       document.getElementById("club").style.visibility = "visible"; 

       closeMenu(); 
      } 

      function openMenu(){ 
       document.getElementById("nav").style.width = "17%"; 
       document.getElementById("menu").style.opacity = "1.0"; 
       document.getElementById("btncls").style.opacity = "1.0"; 
       document.getElementById("menu").style.visibility = "visible"; 
      } 
      function closeMenu(){ 
       document.getElementById("nav").style.width = "0"; 
       document.getElementById("menu").style.opacity = "0.0"; 
       document.getElementById("btncls").style.opacity = "0.0"; 
       setTimeout(function(){document.getElementById("menu").style.visibility = "hidden";}, 500); 
      } 

      var cursorX; 
      var cursorY; 
      document.onmousemove = function(e){ 
       cursorX = e.pageX-100; 
       cursorY = e.pageY-100; 
      } 

      function join(){ 
       var video = document.getElementById("videoplayback"); 
       video.play(); 

       document.getElementById('choice').remove(); 

       setInterval(checkCursor, 1); 
       function checkCursor(){ 
        document.body.onmouseup = function(){ 
         console.log(parseFloat(cursorX) + ', ' + cursorY); 

         var explo = document.createElement("img"); 
         explo.src = "explosive.gif?" + new Date().getTime(); 
         explo.style.position = "absolute"; 
         explo.style.top = cursorY; 
         explo.style.left = cursorX; 
         explo.style.pointerEvents = "none"; 
         document.body.appendChild(explo); 
         setTimeout(function(){explo.remove();}, 800); 

         document.getElementById("videoplayback").style.opacity = "0.1"; 
         setTimeout(function(){document.getElementById("videoplayback").style.opacity = "0.0";}, 1000); 
        } 
       } 
      } 
      function leave(){ 
       document.getElementById("choice").style.position = "relative"; 
       var rand1 = Math.floor(Math.random() * (600 - 30 + 1)) + 30; 
       document.getElementById("choice").style.top = rand1; 
       var rand2 = Math.floor(Math.random() * (1300 - 30 + 1)) + 30; 
       document.getElementById("choice").style.left = rand2; 
       console.log(rand1+', '+rand2); 
      } 
     </script> 
    </body> 
</html> 
+1

你能提供出該問題的最低限度,例如工作? – jhpratt

+0

如果你在'document.body.onclick'函數內'console.log(cursorY)'和'console.log(cursorX)'會發生什麼?你也會用當前的功能泄漏內存。創建你的'checkCursor()'函數,並將它分配給'document.body.onclick',而不是把它放在setInterval函數中。 – kyle

+0

我會得到一個正常的號碼,因爲我在我的瀏覽器中沒有要求NaN –

回答

1

您需要添加px的座標:

explo.style.top = cursorY + "px"; 
explo.style.left = cursorX + "px"; 
+0

謝謝,我知道你不得不放置一個單元,但是當我把這個新的html放在它不再工作,所以這是我的錯,thx解決 –

0

這看起來像一個CSS問題。

position: relative;添加到body元素。

body { 
    position: relative; 
} 

如果您的身體元素爲空,您還需要將其拉伸至窗口的高度。

html, 
body { 
    height: 100%; 
} 
+0

@ noah-snoeks對你有用嗎? – Dedering

+0

不,不幸的是沒有(srry爲壞克和拼寫xD) –