问一下使用jQuery如何让一张图片生成在随机位置,并往随机位置移动,一直循环

weixin_42619522 2019-05-03 04:50:43
在移动时,鼠标点击图片便移动到另一位置。
...全文
338 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
天际的海浪 2019-05-03
  • 打赏
  • 举报
回复

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
	<title> 页面名称 </title>
</head>
<body>
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>
<img id="img" src="1.png" width="100" height="100" border="0" style="position: absolute;">
<script type="text/javascript">
var img = $("#img");
function move() {
	img.stop(true).animate({
		left: Math.floor(Math.random()*($(document).width()-img.width())),
		top: Math.floor(Math.random()*($(document).height()-img.height()))
	}, 3000, move);
}
img.click(function(event){
	move();
});
move();
</script>
</body>
</html>

87,994

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧