Javascript 需要帮助固定图像JS的动态位置吗
我一直在做一个JS项目,在这个项目中,太阳绕着一张图像转一圈,根据它的位置,一个更高的索引div会改变不透明度,使它变暗或变亮。我的问题是;当太阳在某一点上作圆周运动时,它就不再作圆周运动了。我尝试了很多方法来解决这个问题,但都没有用。我的文档代码如下所示:Javascript 需要帮助固定图像JS的动态位置吗,javascript,image,dynamic,position,trigonometry,Javascript,Image,Dynamic,Position,Trigonometry,我一直在做一个JS项目,在这个项目中,太阳绕着一张图像转一圈,根据它的位置,一个更高的索引div会改变不透明度,使它变暗或变亮。我的问题是;当太阳在某一点上作圆周运动时,它就不再作圆周运动了。我尝试了很多方法来解决这个问题,但都没有用。我的文档代码如下所示: <style> sun{ position: absolute; z-index: 1; } dark-light{ z-index: 2; } </style> CSS:
<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。
编辑:all的位置是绝对的,0,0是页面的左上角。我确保不去处理消极的东西。sun对页面应该是绝对的,因为它什么都没有。好吧,我发现我显然不太擅长css,在添加了style=position:absolute;它现在可以正常工作。这没有帮助,因为没有给出页面和sun的方向和css,旋转公式可以工作,但您必须考虑方向,即点为0,0,加上如何处理负坐标,以及所有这些与html布局和css样式的关系,例如,太阳的位置是绝对的,如果是在什么范围内?对不起,@Nikos M。all的位置是绝对的,0,0是页面的左上角。我确保不去处理消极的东西。太阳对页面应该是绝对的,因为它不存在,所以你必须考虑太阳的尺寸半径加上从左上角到中心的距离,从中心减去太阳半径的一半coords@NikosM.这就是数学。sinrot*500+500+obj.height/2?由sin和cos给出的极坐标\n假设中心在0,0,所以你需要先做两件事旋转中心到旋转中心我假设这是地球,得到极坐标,然后将太阳平移一半半径,使其中心正好位于极点,记住css坐标由左上角给出。最后检查css动画和css转换,它们可以以更高的效率完成同样的事情