以下是 jquery实现天空白云飘动代码 的示例演示效果:
部分效果截图:
HTML代码(index.html):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery实现天空白云飘动代码</title>
<link type="text/css" href="css/style.css" rel="stylesheet" />
</head>
<body>
<div id="mainBody">
<div id="cloud1" class="cloud"></div>
<div id="cloud2" class="cloud"></div>
</div>
<div id="weather">
<img src="images/cloud.png" width="300">
</div>
<script src="js/jquery-1.7.min.js" type="text/javascript"></script>
<script src="js/common.js" type="text/javascript"></script>
</body>
</html>
JS代码(common.js):
// Cloud Float...var $main = $cloud = mainwidth = null;
var offset1 = 450;
var offset2 = 0;
var offsetbg = 0;
$(document).ready(function (){
$main = $("#mainBody");
$body = $("body");
$cloud1 = $("#cloud1");
$cloud2 = $("#cloud2");
mainwidth = $main.outerWidth();
}
);
/// 椋樺姩setInterval(function flutter(){
if (offset1 >= mainwidth){
offset1 = -580;
}
if (offset2 >= mainwidth){
offset2 = -580;
}
offset1 += 1.1;
offset2 += 1;
$cloud1.css("background-position",offset1 + "px 30px")$cloud2.css("background-position",offset2 + "px 340px")}
,70);
/*setInterval(function bg(){
if (offsetbg >= mainwidth){
offsetbg = -580;
}
offsetbg += 0.6;
$body.css("background-position",-offsetbg + "px 0")}
,90 );
*/
CSS代码(style.css):
@charset "utf-8";*{margin:0;padding:0;}
body,html{width:100%;height:100%;}
body{background:url("../images/body_bg.jpg") repeat-x;}
#mainBody{position:relative;width:100%;height:100%;}
.cloud{position:absolute;top:0px;left:0px;width:100%;height:100%;background:url("../images/cloud.png") no-repeat;z-index:1;opacity:0.8;}
#cloud2{z-index:3;}
#weather{background:url(../images/sun.png) no-repeat;border-radius:90px;width:150px;height:150px;position:absolute;left:440px;top:30px;z-index:2;}
#weather img{margin:30px -90px;}