以下是 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=gb2312" />
<meta name="keywords" content="JS代码,图片特效,JS广告代码,JS特效代码" />
<meta name="description" content="此代码内容为jquery制作的飞行乌鸦,属于站长常用代码" />
<title>jquery制作的飞行乌鸦</title>
<link rel="stylesheet" type="text/css" href="styles/main.css" />
<style type="text/css">
#stage {
top: 0px;
left: 0px;
z-index: 100;
}
.stage {
position: absolute;
top: 0;
left: 0;
width: 100%;
min-width: 900px;
height: 359px;
overflow: hidden;
}
#bg {
background: #aedfe5 url(images/sky1.png) 0 0 repeat-x;
}
#clouds {
background: transparent url(images/cloud.png) 305px 102px repeat-x;
}
#hill2 {
background: transparent url(images/hill2.png) 0 258px repeat-x;
}
#hill1 {
background: transparent url(images/hill-with-windmill.png) 0 104px repeat-x;
}
#bird {
background: transparent url(images/bird-forward-back.png) 0 0 no-repeat;
position: absolute;
top: 150px;
left: 65px;
width: 180px;
height: 123px;
z-index: 2000;
cursor: pointer;
}
</style>
<!-- IE6 fixes are found in styles/ie6.css -->
<!--[if lte IE 6]><link rel="stylesheet" type="text/css" href="styles/ie6.css" /><![endif]-->
<script src="scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="scripts/jquery-ui-1.7.2.spritely.custom.min.js" type="text/javascript"></script>
<script src="scripts/jquery.spritely-0.2.js" type="text/javascript"></script>
<script type="text/javascript">
(function($) {
$(document).ready(function() {
$('#logo').click(function() {
document.location.href = 'http://www.spritely.net/';
});
$('#bird')
.sprite({fps: 9, no_of_frames: 3})
.spRandom({top: 50, bottom: 200, left: 300, right: 320})
.isDraggable()
.activeOnClick()
.active();
$('#clouds').pan({fps: 30, speed: 0.7, dir: 'left', depth: 10});
$('#hill2').pan({fps: 30, speed: 2, dir: 'left', depth: 30});
$('#hill1').pan({fps: 30, speed: 3, dir: 'left', depth: 70});
$('#hill1, #hill2, #clouds').spRelSpeed(8);
window.actions = {
fly_slowly_forwards: function() {
$('#bird')
.fps(10)
.spState(1);
$('#hill1, #hill2, #clouds')
.spRelSpeed(10)
.spChangeDir('left');
},
fly_slowly_backwards: function() {
$('#bird')
.fps(10)
.spState(2);
$('#hill1, #hill2, #clouds')
.spRelSpeed(10)
.spChangeDir('right');
},
fly_quickly_forwards: function() {
$('#bird')
.fps(20)
.spState(1);
$('#hill1, #hill2, #clouds')
.spRelSpeed(30)
.spChangeDir('left');
},
fly_quickly_backwards: function() {
$('#bird')
.fps(20)
.spState(2);
$('#hill1, #hill2, #clouds')
.spRelSpeed(30)
.spChangeDir('right');
},
fly_like_lightning_forwards: function() {
$('#bird')
.fps(25)
.spState(1);
$('#hill1, #hill2, #clouds')
.spSpeed(40)
.spChangeDir('left');
},
fly_like_lightning_backwards: function() {
$('#bird')
.fps(25)
.spState(2);
$('#hill1, #hill2, #clouds')
.spSpeed(40)
.spChangeDir('right');
}
};
window.page = {
hide_panels: function() {
$('.panel').hide(300);
},
show_panel: function(el_id) {
this.hide_panels();
$(el_id).show(300);
}
}
});
})(jQuery);
</script>
</head>
<body>
<div id="container">
<div id="stage" class="stage">
<div id="tap" class="stage"></div>
<div id="bg" class="stage"></div>
<div id="clouds" class="stage"></div>
<div id="hill2" class="stage"></div>
<div id="hill1" class="stage"></div>
<div id="logo">Spritely</div>
</div>
<div id="bird"></div>
<div id="mainContent">
<h1>Spritely 0.2 Examples</h1>
<div id="panels">
<p>点击下面可控制改变动画效果,你可以灵活应用制作出属于你的效果。<a href="#"></a></p>
<p> </p>
<ul>
<li><a href="#" onclick="page.show_panel('#panel4')">Cool combined actions to modify a whole scene</a></li>
<li><a href="#" onclick="page.show_panel('#panel1')">Moving background elements at fixed speeds; stopping and starting backgrounds & changing direction</a></li>
<li><a href="#" onclick="page.show_panel('#panel2')">Moving background elements at relative speeds</a></li>
<li><a href="#" onclick="page.show_panel('#panel3')">Changing the speed of and stopping and starting sprites; changing sprite 'states'</a></li>
</ul>
<p> </p>
<div id="panel1" class="panel">
<h2>Moving background elements at fixed speeds</h2>
<p>
You can move multiple background elements together, and they will all
move at the same speed...
</p>
<button onclick="$('#hill1, #hill2, #clouds').spSpeed(40);">.spSpeed(40)</button>
<button onclick="$('#hill1, #hill2, #clouds').spSpeed(20);">.spSpeed(20)</button>
<button onclick="$('#hill1, #hill2, #clouds').spSpeed(3);">.spSpeed(3)</button>
<button onclick="$('#hill1, #hill2, #clouds').spSpeed(1);">.spSpeed(1)</button>
<button onclick="$('#hill1, #hill2, #clouds').spStop();">.spStop()</button>
<button onclick="$('#hill1, #hill2, #clouds').spStart();">.spStart()</button>
<button onclick="$('#hill1, #hill2, #clouds').spToggle();">.spToggle()</button>
<button onclick="$('#hill1, #hill2, #clouds').spChangeDir('left');">.spChangeDir('left')</button>
<button onclick="$('#hill1, #hill2, #clouds').spChangeDir('right');">.spChangeDir('right')</button>
</div>
<div id="panel2" class="panel">
<h2>Moving background elements at relative speeds</h2>
<button onclick="$('#hill1, #hill2, #clouds').spRelSpeed(40);">.spRelSpeed(40)</button>
<button onclick="$('#hill1, #hill2, #clouds').spRelSpeed(20);">.spRelSpeed(20)</button>
<button onclick="$('#hill1, #hill2, #clouds').spRelSpeed(3);">.spRelSpeed(3)</button>
<p> </p>
<p>
You can change the relative speed of a number of background elements at once
with the .spRelSpeed() method. However, before you do this, you must set a 'depth'
parameter for each element when you create them with the .pan() method. Please look
at the source code of this page for an example.<br /><br />
The 'depth' method is a value between 0 and 100 where 0 is the horizon (distant
objects which do not move at all) and 100 is for very close-up objects which move
very quickly:<br /><br />
<img id="depth-explained" src="images/depth-explained.jpg" border="0" />
</p>
</div>
<div id="panel3" class="panel">
<h2>Changing the speed of and stopping and starting sprites; changing sprite 'states'.</h2>
<button onclick="$('#bird').fps(24);">.fps(24)</button>
<button onclick="$('#bird').fps(12);">.fps(12)</button>
<button onclick="$('#bird').fps(6);">.fps(6)</button>
<button onclick="$('#bird').spStop();">.spStop()</button>
<button onclick="$('#bird').spStop(true);">.spStop(true) (returns to frame 1)</button>
<button onclick="$('#bird').spStart();">.spStart()</button>
<button onclick="$('#bird').spToggle();">.spToggle()</button>
<button onclick="$('#bird').spState(1);">.spState(1)</button>
<button onclick="$('#bird').spState(2);">.spState(2)</button>
<p> </p>
<p>
To change a sprite's 'state', you need an image with multiple rows, where
each row is a 'state' (1, 2, 3, etc.) and each row contains multiple frames,
like the following image:<br /><br />
<img src="images/bird-forward-back.png" border="0" />
</p>
</div>
<div id="panel4" class="panel">
<h2>Combining actions to modify a whole scene</h2>
<button onclick="actions.fly_slowly_forwards();">actions.fly_slowly_forwards();</button>
<button onclick="actions.fly_quickly_forwards();">actions.fly_quickly_forwards();</button>
<button onclick="actions.fly_like_lightning_forwards();">actions.fly_like_lightning_forwards();</button><br />
<button onclick="actions.fly_slowly_backwards();">actions.fly_slowly_backwards();</button>
<button onclick="actions.fly_quickly_backwards();">actions.fly_quickly_backwards();</button>
<button onclick="actions.fly_like_lightning_backwards();">actions.fly_like_lightning_backwards();</button>
<p>Take a look at the source code to see how this is done. It's amazingly easy to do.</p>
</div>
</div>
</div>
</div>
</body>
</html>
JS代码(jquery.spritely-0.2.js):
/* * jQuery spritely 0.2 beta * http://spritely.net/ * * Documentation:* http://spritely.net/documentation/ * * Copyright 2010,Peter Chater,Artlogic Media Ltd,http://www.artlogic.net/ * Dual licensed under the MIT or GPL Version 2 licenses. * * Change history:* Version 0.2 beta * - added isDraggable method (requires jquery-ui) $('#sprite').sprite().isDraggable({
start:null,stop:function(){
alert('Ouch! You dropped me!')}
);
* - sprites may be set to play a limited number of frames when instantiated,e.g. $('#sprite').sprite({
fps:9,no_of_frames:3,play_frames:30}
) * - sprite speed may be controlled at any point by setting the frames-per-second $('#sprite').fps(20);
* - sprites with multiple rows of frames may have there 'state' changed,e.g. to make the second row of frames * active,use:$('#sprite').spState(2);
- to return to the first row,use $('#sprite').spState(1);
* - background element speed may be controlled at any point with .spSpeed(),e.g. $('#bg1').spSpeed(10) * - background elements may be set to a depth where 100 is the viewer (up close) and 0 is the horizon,e.g.:* $('#bg1').pan({
fps:30,speed:2,dir:'left',depth:30}
);
* $('#bg2').pan({
fps:30,speed:3,dir:'left',depth:70}
);
* relative speed of backgrounds may now be set in a single action with $('#bg1,#bg2').spRelSpeed(20);
* which will make elements closer to the horizon (lower depths) move slower than closer elements (higher depths) */
(function($){
$._spritely ={
// shared methods and variables used by spritely pluginanimate:function(options){
var el = $(options.el);
var el_id = el.attr('id');
options = $.extend(options,$._spritely.instances[el_id] ||{
}
);
if (options.play_frames && !$._spritely.instances[el_id]['remaining_frames']){
$._spritely.instances[el_id]['remaining_frames'] = options.play_frames + 1;
}
if (options.type == 'sprite' && options.fps){
var frames;
var animate = function(el){
var w = options.width,h = options.height;
if (!frames){
frames = [];
total = 0for (var i = 0;
i < options.no_of_frames;
i ++){
frames[frames.length] = (0 - total);
total += w;
}
}
if ($._spritely.instances[el_id]['current_frame'] >= frames.length - 1){
$._spritely.instances[el_id]['current_frame'] = 0;
}
else{
$._spritely.instances[el_id]['current_frame'] = $._spritely.instances[el_id]['current_frame'] + 1;
}
var yPos = $._spritely.getBgY(el);
el.css('background-position',frames[$._spritely.instances[el_id]['current_frame']] + 'px ' + yPos);
if (options.bounce && options.bounce[0] > 0 && options.bounce[1] > 0){
var ud = options.bounce[0];
// up-downvar lr = options.bounce[1];
// left-rightvar ms = options.bounce[2];
// millisecondsel.animate({
top:'+=' + ud + 'px',left:'-=' + lr + 'px'}
,ms).animate({
top:'-=' + ud + 'px',left:'+=' + lr + 'px'}
,ms);
}
}
if ($._spritely.instances[el_id]['remaining_frames'] && $._spritely.instances[el_id]['remaining_frames'] > 0){
$._spritely.instances[el_id]['remaining_frames'] --;
if ($._spritely.instances[el_id]['remaining_frames'] == 0){
$._spritely.instances[el_id]['remaining_frames'] = -1;
}
else{
animate(el);
}
}
else if ($._spritely.instances[el_id]['remaining_frames'] != -1){
animate(el);
}
}
else if (options.type == 'pan'){
if (!$._spritely.instances[el_id]['_stopped']){
if (options.dir == 'left'){
$._spritely.instances[el_id]['l'] = ($._spritely.instances[el_id]['l'] - (options.speed || 1)) || 0;
}
else{
$._spritely.instances[el_id]['l'] = ($._spritely.instances[el_id]['l'] + (options.speed || 1)) || 0;
}
var bp_top = $._spritely.getBgY(el);
$(el).css('background-position',$._spritely.instances[el_id]['l'] + 'px ' + bp_top);
}
}
$._spritely.instances[el_id]['options'] = options;
window.setTimeout(function(){
$._spritely.animate(options);
}
,parseInt(1000 / options.fps));
}
,randomIntBetween:function(lower,higher){
return parseInt(rand_no = Math.floor((higher - (lower - 1)) * Math.random()) + lower);
}
,getBgY:function(el){
if ($.browser.msie){
// fixme - the background-position property does not work// correctly in IE so we have to hack it here... Not ideal// especially as $.browser is depricatedvar bgY = $(el).css('background-position-y') || '0';
}
else{
var bgY = ($(el).css('background-position') || ' ').split(' ')[1];
}
return bgY;
}
,getBgX:function(el){
if ($.browser.msie){
// see note,abovevar bgX = $(el).css('background-position-x') || '0';
}
else{
var bgX = ($(el).css('background-position') || ' ').split(' ')[0];
}
return bgX;
}
}
;
$.fn.extend({
spritely:function(options){
var options = $.extend({
type:'sprite',do_once:false,width:null,height:null,fps:12,no_of_frames:2,stop_after:null}
,options ||{
}
);
var el_id = $(this).attr('id');
if (!$._spritely.instances){
$._spritely.instances ={
}
;
}
if (!$._spritely.instances[el_id]){
$._spritely.instances[el_id] ={
current_frame:-1}
;
}
$._spritely.instances[el_id]['type'] = options.type;
$._spritely.instances[el_id]['depth'] = options.depth;
options.el = this;
options.width = options.width || $(this).width() || 100;
options.height = options.height || $(this).height() || 100;
var get_rate = function(){
return parseInt(1000 / options.fps);
}
if (!options.do_once){
window.setTimeout(function(){
$._spritely.animate(options);
}
,get_rate(options.fps));
}
else{
$._spritely.animate(options);
}
return this;
// so we can chain events}
,sprite:function(options){
var options = $.extend({
type:'sprite',bounce:[0,0,1000] // up-down,left-right,milliseconds}
,options ||{
}
);
return $(this).spritely(options);
}
,pan:function(options){
var options = $.extend({
type:'pan',dir:'left',continuous:true,speed:1 // 1 pixel per frame}
,options ||{
}
);
return $(this).spritely(options);
}
,flyToTap:function(options){
var options = $.extend({
el_to_move:null,type:'moveToTap',ms:1000,// millisecondsdo_once:true}
,options ||{
}
);
if (options.el_to_move){
$(options.el_to_move).active();
}
if ($._spritely.activeSprite){
if (window.Touch){
// iphone method see http://cubiq.org/remove-onclick-delay-on-webkit-for-iphone/9 or http://www.nimblekit.com/tutorials.html for clues...$(this)[0].ontouchstart = function(e){
var el_to_move = $._spritely.activeSprite;
var touch = e.touches[0];
var t = touch.pageY - (el_to_move.height() / 2);
var l = touch.pageX - (el_to_move.width() / 2);
el_to_move.animate({
top:t + 'px',left:l + 'px'}
,1000);
}
;
}
else{
$(this).click(function(e){
var el_to_move = $._spritely.activeSprite;
$(el_to_move).stop(true);
var w = el_to_move.width();
var h = el_to_move.height();
var l = e.pageX - (w / 2);
var t = e.pageY - (h / 2);
el_to_move.animate({
top:t + 'px',left:l + 'px'}
,1000);
}
);
}
}
return this;
}
,isDraggable:function(options){
if (!$(this).draggable){
alert('To use the isDraggable method you need to load jquery-ui.js');
return this;
}
var options = $.extend({
type:'isDraggable',start:null,stop:null,drag:null}
,options ||{
}
);
var el_id = $(this).attr('id');
$._spritely.instances[el_id].isDraggableOptions = options;
$(this).draggable({
start:function(){
var el_id = $(this).attr('id');
$._spritely.instances[el_id].stop_random = true;
$(this).stop(true);
if ($._spritely.instances[el_id].isDraggableOptions.start){
$._spritely.instances[el_id].isDraggableOptions.start(this);
}
}
,drag:options.drag,stop:function(){
var el_id = $(this).attr('id');
$._spritely.instances[el_id].stop_random = false;
if ($._spritely.instances[el_id].isDraggableOptions.stop){
$._spritely.instances[el_id].isDraggableOptions.stop(this);
}
}
}
);
return this;
}
,active:function(){
// the active sprite$._spritely.activeSprite = this;
return this;
}
,activeOnClick:function(){
// make this the active script if clicked...var el = $(this);
if (window.Touch){
// iphone method see http://cubiq.org/remove-onclick-delay-on-webkit-for-iphone/9 or http://www.nimblekit.com/tutorials.html for clues...el[0].ontouchstart = function(e){
$._spritely.activeSprite = el;
}
;
}
else{
el.click(function(e){
$._spritely.activeSprite = el;
}
);
}
return this;
}
,spRandom:function(options){
var options = $.extend({
top:50,left:50,right:290,bottom:320,speed:4000,pause:0}
,options ||{
}
);
var el_id = $(this).attr('id');
if (!$._spritely.instances[el_id].stop_random){
var r = $._spritely.randomIntBetween;
var t = r(options.top,options.bottom);
var l = r(options.left,options.right);
$('#' + el_id).animate({
top:t + 'px',left:l + 'px'}
,options.speed)}
window.setTimeout(function(){
$('#' + el_id).spRandom(options);
}
,options.speed + options.pause)return this;
}
,makeAbsolute:function(){
// remove an element from its current position in the DOM and// position it absolutely,appended to the body tag.return this.each(function(){
var el = $(this);
var pos = el.position();
el.css({
position:"absolute",marginLeft:0,marginTop:0,top:pos.top,left:pos.left}
).remove().appendTo("body");
}
);
}
,spSet:function(prop_name,prop_value){
var el_id = $(this).attr('id');
$._spritely.instances[el_id][prop_name] = prop_value;
return this;
}
,spGet:function(prop_name,prop_value){
var el_id = $(this).attr('id');
return $._spritely.instances[el_id][prop_name];
}
,spStop:function(bool){
$(this).each(function(){
var el_id = $(this).attr('id');
$._spritely.instances[el_id]['_last_fps'] = $(this).spGet('fps');
$._spritely.instances[el_id]['_stopped'] = true;
$._spritely.instances[el_id]['_stopped_f1'] = bool;
if ($._spritely.instances[el_id]['type'] == 'sprite'){
$(this).spSet('fps',0);
}
if (bool){
// set background image position to 0var bp_top = $._spritely.getBgY($(this));
$(this).css('background-position','0 ' + bp_top);
}
}
);
return this;
}
,spStart:function(){
$(this).each(function(){
var el_id = $(this).attr('id');
var fps = $._spritely.instances[el_id]['_last_fps'] || 12;
$._spritely.instances[el_id]['_stopped'] = false;
if ($._spritely.instances[el_id]['type'] == 'sprite'){
$(this).spSet('fps',fps);
}
}
);
return this;
}
,spToggle:function(){
var el_id = $(this).attr('id');
var stopped = $._spritely.instances[el_id]['_stopped'] || false;
var stopped_f1 = $._spritely.instances[el_id]['_stopped_f1'] || false;
if (stopped){
$(this).spStart();
}
else{
$(this).spStop(stopped_f1);
}
return this;
}
,fps:function(fps){
$(this).each(function(){
$(this).spSet('fps',fps);
}
);
return this;
}
,spSpeed:function(speed){
$(this).each(function(){
$(this).spSet('speed',speed);
}
);
return this;
}
,spRelSpeed:function(speed){
$(this).each(function(){
var rel_depth = $(this).spGet('depth') / 100;
$(this).spSet('speed',speed * rel_depth);
}
);
return this;
}
,spChangeDir:function(dir){
$(this).each(function(){
$(this).spSet('dir',dir);
}
);
return this;
}
,spState:function(n){
$(this).each(function(){
// change state of a sprite,where state is the vertical// position of the background image (e.g. frames row)var yPos = ((n - 1) * $(this).height()) + 'px';
var xPos = $._spritely.getBgX($(this));
var bp = xPos + ' -' + yPos;
$(this).css('background-position',bp);
}
);
return this;
}
}
)}
)(jQuery);
// Stop IE6 re-loading background images continuouslytry{
document.execCommand("BackgroundImageCache",false,true);
}
catch(err){
}
CSS代码(main.css):
*{margin:0;padding:0;}
body{background:#FFF;width:auto;height:100%;font-family:'Trebuchet MS',Arial,Sans-Serif;font-size:15px;color:#474747;line-height:1.6em;}
a,a:visited{color:#3a9bc7;text-decoration:none;}
a:hover{text-decoration:underline;}
h1,h2,h3,h4,h5{text-transform:uppercase;font-family:Bliss,'Myriad Pro','Trebuchet MS',Arial,Sans-Serif;color:#a1c117;}
h1 a,h2 a,h3 a,h4 a,h1 a:visited,h2 a:visited,h3 a:visited,h4 a:visited{text-decoration:none;color:#a1c117;}
h1{font-size:1.93em;line-height:1.2em;margin:8px 0 38px 0;}
h2{font-size:1.47em;line-height:1.2em;margin:8px 0 15px 0;}
h3{font-size:1.20em;line-height:1.2em;margin:8px 0 15px 0;}
h4{font-size:1.07em;line-height:1.1em;margin:8px 0 15px 0;}
h5{font-size:1.07em;line-height:1.1em;margin:8px 0 15px 0;}
a img{border:0;}
ul{padding-left:40px;}
pre{background:#efefef;border:1px solid silver;margin:10px 0;padding:10px;font-family:'Courier New',Courier,monospace;}
#logo{background:url(../images/spritely_logo.png) 0 0 no-repeat;width:378px;height:121px;position:absolute;top:7px;left:30px;z-index:1900;text-indent:-9999px;}
#container{min-width:900px;padding-top:359px;position:relative;width:100%;}
#mainContent{padding:40px 30px 30px 30px;}
#mainContent h1{text-align:-9999px;height:0;overflow:hidden;margin:0;}
#content{margin-right:250px;}
#sidebar{position:absolute;top:448px;width:200px;right:30px;}
.panel{margin-bottom:2em;display:none;}