欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Javascript实现图片随机漂浮

程序员文章站 2022-05-20 17:52:39
...
<html>
<head>
</head>
<body>
	<div id="img" style="position:absolute;width:200; height:151"><img src="pic.jpg" border="0"></div>

<script LANGUAGE="JavaScript">
var xPos = 20;
var yPos = 10;
img.style.left= xPos;
img.style.top = yPos;
var step = 1;
var delay = 30; 
var width,height,Hoffset,Woffset;
var y = 1;
var x = 1;
var interval;
img.visibility = "visible";
function changePos() 
{
  width = document.body.clientWidth;
  height = document.body.clientHeight;
  Hoffset = img.offsetHeight;
  Woffset = img.offsetWidth;
  if (y) 
  {
    yPos = yPos + step;
  }
  else 
  {
    yPos = yPos - step;
  }
  if (yPos < 0) 
  {
    y = 1;
    yPos = 0;
  }
  if (yPos >= (height - Hoffset)) 
  {
    y = 0;
    yPos = (height - Hoffset);
  }
  if (x) 
  {
    xPos = xPos + step;
  }
  else 
  {
    xPos = xPos - step;
  }
  if (xPos < 0) 
  {
    x = 1;
    xPos = 0;
  }
  if (xPos >= (width - Woffset)) 
  {
    x = 0;
    xPos = (width - Woffset);
  }
  img.style.left = xPos + document.body.scrollLeft;
  img.style.top = yPos + document.body.scrollTop;
}
function start() 
{
  interval = setInterval('changePos()', delay);
}
function pause_resume() 
{
    clearInterval(interval);
}
start();
</script>
</body>
</html>
 
  • Javascript实现图片随机漂浮
            
    
    博客分类: JavaScript/Jquery JavaScript 
  • 大小: 11.5 KB
相关标签: JavaScript