代码之家  ›  专栏  ›  技术社区  ›  BKFighter

需要帮助修复图像JS的动态位置

  •  0
  • BKFighter  · 技术社区  · 11 年前

    我一直在做一个JS项目,太阳围绕着一个圆圈中的图像,根据它的位置,较高的索引div将改变不透明度,使其变暗或变亮。我的问题是:;当太阳在某一点上做圆周运动时,它不再做圆周运动了。我尝试了很多方法来解决这个问题,但都没有效果。我的文档代码如下:

    <style>
      sun{
        position: absolute;
        z-index: 1;
      }
      dark-light{
        z-index: 2;
      }
    
    </style>
    

    CSS:^JS:v

    <script>
    
    
    
      move();
    
      //calls move
    
    
      function move(){
       //set rot
        var rot = 180;
    
        var pictureToDisplay = prompt('Please insert link to picture to     display','URL Necessary to function properly, but not required. Press enter to continue.');
        //asks user to insert picture link for STATIONARY picture
        var img = document.getElementById('img');
    
        if (pictureToDisplay == 'URL Necessary to function properly, but not required. Press enter to continue.'){
    
        }else{
        img.src = pictureToDisplay;
        }
        //Sets stationary picture
        window.setInterval( function() {
    
          //Repeats every 75 milliseconds forever.
    
          var obj = document.getElementById('sun');
        // obj. is equal to id sun
          var top = (Math.sin(rot)*500)+500;
          var left = (Math.cos(rot)*500)+500;
          //uses var rot, sine, and cosine to determine moving sun position
          var toppx = top + 'px';
          var leftpx = left + 'px';
          //adds the px after those values
          obj.style.top = toppx;
          obj.style.left = leftpx;
          //attempts to set position of obj (id sun)
          var darkToLight = -0.5+(top/500);
          //determines opacity of div using var top
    
          //document.write(rot+' = rot : ');
    
          var lightDiv = document.getElementById('dark-light');
          // same as var obj, but with the div
          lightDiv.style.opacity = darkToLight;
          //sets lightDiv to opacity
          //document.write(toppx,' ,',leftpx,' : ');
    
    
        rot = (rot+0.01) % 360;
          //moves rot up by 0.01, modulate 360
        }, 75);
        //back to top of setInterval
      }
    
    
    </script>
    

    注:我不知道JQuery。 编辑:所有的位置是绝对的,0,0是页面的左上角。我确定不去处理消极的事情。太阳对书页来说应该是绝对的,因为它什么都没有。

    1 回复  |  直到 11 年前
        1
  •  0
  •   BKFighter    11 年前

    好的,我发现我显然不太擅长css,在添加了之后 style="position:absolute"; 它现在运行良好。