如图,如何在网页中用Javascript实现该图片沿窗口四周循环浮动,求详细代码
1个回答
展开全部
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>_</title>
<style type="text/css">
html, body{margin:0; padding:0; height:100%;}
#img1{position:absolute;}
</style>
<script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(function(){
var dir = 1, size = 10, interval = 30;
var width = $(document.body).width(), height = $(document.body).height();
var img = $("#img1"), w = img.width(), h = img.height(), offset = img.offset();
setInterval(function(){
switch(dir){
case 1:
offset.left += size;
if(offset.left + w >= width){
offset.left = width - w;
dir++;
}
break;
case 2:
offset.top += size;
if(offset.top + h >= height){
offset.top = height - h;
dir++;
}
break;
case 3:
offset.left -= size;
if(offset.left <= 0){
offset.left = 0;
dir++;
}
break;
case 4:
offset.top -= size;
if(offset.top <= 0){
offset.top = 0;
dir++;
}
break;
}
if(dir > 4){
dir = 1;
}
img.offset(offset);
}, interval);
$(window).resize(function(){
width = $(document.body).width();
height = $(document.body).height();
});
});
</script>
<body>
<img id="img1" src="star.jpg" />
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询