Solved

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

Posted on 2010-11-16
8
596 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 500 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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)

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now