Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to display a sprite animation on a web page via jquery

Posted on 2010-11-16
8
Medium Priority
?
607 Views
Last Modified: 2012-05-10
how does one display (see attached image) as an animation on a web page using jquery please
bot-explode.png
0
Comment
Question by:Johnny
  • 4
  • 4
8 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 34150477
You may play with CSS and backgroundPosition-x

Test page :




<!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>Untitled Document</title>
<style type="text/css">
img.boom
{
	width:23px;
	height:28px;
	background:url(bot-explode.png) 0 0;
}
</style>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script language="javascript">
	setInterval("badaboum()",100);
	var nbSprites = 7;
	var widthSprite = 23;
	function badaboum() {
		var x = parseInt($(".boom").css("backgroundPosition-x").replace("px","")) + 23;
		if( x >= nbSprites*widthSprite ) {
			$(".boom").css("backgroundPosition-x", "0px");
		}
		else {
			$(".boom").css("backgroundPosition-x", x+"px");
		}
	}
</script>
</head>
<body bgcolor="#BBBBBB">
<img class="boom" />
</body>
</html>

Open in new window

0
 

Author Comment

by:Johnny
ID: 34150964
@leakim971

how does one place it anyplace on the screen? please

seams to work well thank you
0
 

Author Closing Comment

by:Johnny
ID: 34150969
thank you
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 82

Expert Comment

by:leakim971
ID: 34151029
>how does one place it anyplace on the screen?

css :
      position:absolute;
        left:100px;
        right:128px;

You can set multiple css attribute : $(".boom").css({"left":posX+"px","top":posY+"px","position":"absolute","backgroundPosition-x":x+"px"});
where posX, posY and x are variable

thanks for the points!
0
 

Author Comment

by:Johnny
ID: 34151299
did i miss something as this does not work

<script language="javascript">
        setInterval("badaboum()",100);
        var nbSprites = 7;
        var widthSprite = 23;
        var posX = 120;
        var posY = 120;
        function badaboum() {
                var x = parseInt($(".boom").css("backgroundPosition-x").replace("px","")) + 23;
                if( x >= nbSprites*widthSprite ) {
                        $(".boom").css({"left":posX+"px","top":posY+"px","position":"absolute","backgroundPosition-x", "0px");
                }
                else {
                        $(".boom").css({"left":posX+"px","top":posY+"px","position":"absolute","backgroundPosition-x", x+"px");
                }
        }
</script>

Open in new window


thank you for the help
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34151314

                if( x >= nbSprites*widthSprite ) {
                        $(".boom").css({"left":posX+"px","top":posY+"px","position":"absolute","backgroundPosition-x":"0px"});
                }
                else {
                        $(".boom").css({"left":posX+"px","top":posY+"px","position":"absolute","backgroundPosition-x":x+"px"});
                }
   
0
 

Author Comment

by:Johnny
ID: 34151319
perfect thank you
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34151325
you're welcome!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question