<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Webmaster-Source &#187; sprites</title>
	<atom:link href="https://www.webmaster-source.com/tag/sprites/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.webmaster-source.com</link>
	<description>Useful Resources For Webmasters</description>
	<lastBuildDate>Thu, 24 Aug 2017 02:01:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Animating a CSS Sprite With JavaScript</title>
		<link>https://www.webmaster-source.com/2012/06/04/animating-a-css-sprite-with-javascript/</link>
		<comments>https://www.webmaster-source.com/2012/06/04/animating-a-css-sprite-with-javascript/#comments</comments>
		<pubDate>Mon, 04 Jun 2012 11:58:52 +0000</pubDate>
		<dc:creator><![CDATA[Matt]]></dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[sprites]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=4700</guid>
		<description><![CDATA[Take a look at this demo, and guess how the animation is achieved. It&#8217;s not an animated GIF. It&#8217;s a PNG sprite that is brought to life with a little bit of JavaScript. The sprite image is a long strip containing each frame of the animation. The stylesheet sets it as the background of a [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Take a look at <a href="http://www.webmaster-source.com/static/demos/jsspriteanim/">this demo</a>, and guess how the animation is achieved. It&#8217;s not an animated GIF. It&#8217;s a PNG sprite that is brought to life with a little bit of JavaScript.</p>
<p>The sprite image is a long strip containing each frame of the animation. The stylesheet sets it as the background of a DIV, and the dimensions are set so you can only see one frame at a time. Then a script uses a timer loop to continuously shift down the line, resetting once it reaches the end. (A more complex version of this effect can be seen in this <a href="http://www.google.com/doodles/martha-grahams-117th-birthday">Google Doodle</a>.)</p>
<p><a href="http://www.webmaster-source.com/static/demos/jsspriteanim/"><img style=' display: block; margin-right: auto; margin-left: auto;'  class="aligncenter size-full wp-image-4701 imgborder" title="The Sprite" src="//www.webmaster-source.com/wp-content/uploads/2012/06/animsprite-puzzlesprite.png" alt="" width="600" height="63" /></a></p>
<p>It works just like the sprites traditionally used in video games. It&#8217;s more efficient to load one image from a slow data source, whether it&#8217;s a hard disk or an internet connection, and only show a piece of it at a time, than to load dozens of images and alternate between them. (It saves some overhead in other ways in a performance-minded environment like a game, as well.)<span id="more-4700"></span></p>
<h3>Why not just use an animated GIF?</h3>
<p>Aside from the simple fact that it&#8217;s more fun, the Graphics Interchange Format has some limitations that make it unsuitable for certain applications. It doesn&#8217;t work very well at all for photographic images, and it only has very basic transparency support. JPEG is far better for photographs, and PNG for images with flatter colors.</p>
<p>There <em>is</em> another image format that supports animation, the animated PNG format (APNG), but support for it is sorely lacking. Most popular image editors don&#8217;t support it natively, and only a couple of browsers can display them without third-party extensions. Firefox/Gecko and Opera are about the extent of browser support for APNG. Internet Explorer can&#8217;t display them at all, and Chrome requires a third-party extension.</p>
<p>By using a sprite, you have an image format that fits your needs, and still have animation. Oh, and you can start and stop the animation at will, through JavaScript.</p>
<h3>Implementation</h3>
<p>For starters, you need an element to hold the sprite. I ended up using a DIV with an ID of &#8220;sprite.&#8221;</p>
<pre class="brush: xml; title: ; notranslate">&lt;div id=&quot;sprite&quot;&gt;&lt;/div&gt;</pre>
<p>Then you need to style the element. (If it&#8217;s not already a block-level element like a DIV, you may need to add <code>display:block</code>.) It&#8217;s height should be the height of your sprite image, and the width should be the width of a single frame. My image is 200 pixels high, and a frame is 300 pixels wide.</p>
<pre class="brush: css; title: ; notranslate">
#sprite {
width: 300px;
height: 200px;
margin: 30px auto;
background-image: url(sprite.png);
}
</pre>
<p>The most important part, of course, is the script. I&#8217;m using some jQuery, simply because it&#8217;s a convenient way to handle my button events. You could easily adapt it to work without, though.</p>
<pre class="brush: jscript; title: ; notranslate">
$(document).ready(function(){

var frameWidth = 300;
var frameHeight = 200;
var spriteWidth = 5400;
var spriteHeight = 200;
var spriteElement = document.getElementById(&quot;sprite&quot;);

var curPx = 0;
var ti;

function animateSprite() {

spriteElement.style.backgroundPosition = curPx + 'px 0px';
curPx = curPx + frameWidth;

if (curPx &gt;= spriteWidth) {
curPx = 0;
}

ti = setTimeout(animateSprite, 80);

}

animateSprite();

});
</pre>
<p>The block of variables at the top set the frame dimensions, the dimensions of the sprite image, and the element containing the sprite. Then a timer is set, firing once per 80 milliseconds, which runs the <code>animateSprite()</code> function. Every time the function runs, it sets the background offset to <code>curPx</code>, and adds the frame width to <code>curPx</code>, so the next frame will be displayed in the following iteration. If <code>curPx</code> reaches the last frame, it&#8217;s reset to zero, so the animation will wrap around.</p>
<p>As an added bonus, you can start and stop the animation at will through the script. I just added some click events to a couple of links for illustrative purposes.</p>
<pre class="brush: jscript; title: ; notranslate">
$(&quot;#stop&quot;).click(function() {
clearTimeout(ti);
});

$(&quot;#start&quot;).click(function() {
ti = setTimeout(animateSprite, 80);
});
</pre>
<p>You can see <a href="http://www.webmaster-source.com/static/demos/jsspriteanim/">the finished example in action here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.webmaster-source.com/2012/06/04/animating-a-css-sprite-with-javascript/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Easy CSS Sprites with Sprite Cow</title>
		<link>https://www.webmaster-source.com/2011/07/13/easy-css-sprites-with-sprite-cow/</link>
		<comments>https://www.webmaster-source.com/2011/07/13/easy-css-sprites-with-sprite-cow/#comments</comments>
		<pubDate>Wed, 13 Jul 2011 12:33:33 +0000</pubDate>
		<dc:creator><![CDATA[Matt]]></dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[(x)html]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[sprites]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=4152</guid>
		<description><![CDATA[CSS sprites are a commonly used technique to decrease page load times. One of the biggest hassles when setting them up, though, is figuring out the coordinates for the images in your sprite. (The other is rebuilding the sprite when you want to add new graphics&#8230;) There&#8217;s not much to be done for the latter, [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.webmaster-source.com/2009/11/06/learning-css-sprites/">CSS sprites</a> are a commonly used technique to decrease page load times. One of the biggest hassles when setting them up, though, is figuring out the coordinates for the images in your sprite. (The other is rebuilding the sprite when you want to add new graphics&#8230;)</p>
<p>There&#8217;s not much to be done for the latter, but there is a handy tool that makes finding the coordinates painless. <a href="http://www.spritecow.com/">Sprite Cow</a> intelligently draws a box around an image you select in your sprite sheet (after you load the sprite, of course) and writes the CSS for it. You can&#8217;t get any simpler than that.</p>
<p style="text-align: center;"><a href="http://www.spritecow.com/"><img style=' display: block; margin-right: auto; margin-left: auto;'  class="aligncenter size-full wp-image-4153 imgborder" title="Sprite Cow" src="//www.webmaster-source.com/wp-content/uploads/spritecow.jpg" alt="" width="600" height="286" /></a></p>
<p>I bet the developers could make some good money by making a Mac version and putting it up for sale in the App Store.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.webmaster-source.com/2011/07/13/easy-css-sprites-with-sprite-cow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning CSS Sprites</title>
		<link>https://www.webmaster-source.com/2009/11/06/learning-css-sprites/</link>
		<comments>https://www.webmaster-source.com/2009/11/06/learning-css-sprites/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 11:41:47 +0000</pubDate>
		<dc:creator><![CDATA[Matt]]></dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[sprites]]></category>

		<guid isPermaLink="false">http://www.webmaster-source.com/?p=2705</guid>
		<description><![CDATA[What exactly is a sprite? It&#8217;s a CSS technique that can make your website load faster. Instead of having a dozen images that are included on every page of the site (one for your logo, one for an RSS button, a few fore social media links, etc.) you combine them all into one big image. [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>What exactly is a sprite? It&#8217;s a CSS technique that can make your website load faster. Instead of having a dozen images that are included on every page of the site (one for your logo, one for an RSS button, a few fore social media links, etc.) you combine them all into one big image. You can then use CSS to display just the portion you need in any given spot.</p>
<p>How does this improve loading times? The major bottleneck in the loading of a web page today isn&#8217;t download speeds so much as <em>concurrent downloads</em>. A web browser will only download two files at once from a web server. After the HTML is downloaded, the browser has to grab CSS, JavaScripts, and images from your server. You might have a lot of those secondary files, and only two will be loaded at once. If you combine your template&#8217;s images into one big file, they all get downloaded at once, instead of two at a time. Also, you may shave off a few kilobytes of file size in some cases, but that&#8217;s not as important.</p>
<p>Now how does this trickery work? Let&#8217;s use <a href="http://www.apple.com/">Apple.com</a> as an example. They keep their header navigation in a sprite that looks like this:</p>
<p><img style=' display: block; margin-right: auto; margin-left: auto;'  class="aligncenter size-full wp-image-2706" title="Apple navigation header sprite" src="//www.webmaster-source.com/wp-content/uploads/sprites-apple-header.jpg" alt="Apple navigation header sprite" width="580" height="90" /></p>
<p>As you can see, the buttons are laid out in a row, with their alternate states underneath them. A sprite starts out as something just like that: an image full of little images. The real trick is making it spritely.<span id="more-2705"></span></p>
<p>Let&#8217;s start with the HTML. Here is a simple block of HTML links wrapped in a parent DIV. Each item has an id assigned so we can address it in the stylesheet.</p>
<pre class="brush: xml; title: ; notranslate">
&lt;div id=&quot;sprite&quot;&gt;
&lt;a id=&quot;button-1&quot;&gt;Apple&lt;/a&gt;
&lt;a id=&quot;button-2&quot;&gt;Apple&lt;/a&gt;
&lt;a id=&quot;button-3&quot;&gt;Store&lt;/a&gt;
&lt;a id=&quot;button-4&quot;&gt;Mac&lt;/a&gt;
&lt;/div&gt;
</pre>
<p>Now the first thing that needs to be done in the stylesheet is to collectively select <em>all</em> of the <em>a</em> elements with a CSS rule. I made them all block-level with the first attribute so they&#8217;re each on one line, which will make the output look tidier to better illustrate things.</p>
<pre class="brush: css; title: ; notranslate">
#sprite a {
display: block;
width: 117px;
overflow:hidden;
height:0px;
padding-top: 38px;
background-image: url('path/to/sprite.png');
margin: 5px;
}
</pre>
<p>The <em>width </em>and <em>overflow</em> lines in the above CSS snippet define some of the dimensions of the elements. The <em>width</em> is self-explanatory, and the latter ensures that the element doesn&#8217;t expand to a different size if the textual contents of the link are too big. Then the <em>height</em> and <em>padding-top</em> lines are used to set the height of the element. Instead of directly setting the height to 38px, the padding trick is used to make the plain text inside the links invisible. Otherwise it would appear over the image background.</p>
<p>The background image is then included. Now things get interesting&#8230;</p>
<pre class="brush: css; title: ; notranslate">
#button-1 {
background-position: 0px 0px;
}
#button-2 {
background-position: 0px -38px;
}
#button-3 {
background-position: -117px 0px
}
#button-4 {
background-position: -234px 0px
}
</pre>
<p>These CSS rules are simple but powerful. The <em>background-position</em> attributes set which part of the image is displayed. Since we&#8217;ve already defined a width and height for the elements, only a small slice of the image can be visible at a time. The <em>background-position</em> lines simply define which slice that is.</p>
<p>The first selector, <em>#button-1</em>, sets the top-left corner of the element&#8217;s background image to be at position 0,0 (the top left corner) of the image. For <em>#button-2</em> we shift it down 38px, <em>#button-3</em> right by 117px, and so on. It may seem a little odd at first, but you&#8217;ll get the hang of it eventually.</p>
<p>You can even do &#8220;rollovers&#8221; with sprites. Just use the <em>:hover</em> pseudoclass or a JavaScript <em>mouseover</em> trigger and shift the sprite to the desired spot by changing the CSS property on the fly.</p>
<p style="text-align: center;"><img style=' display: block; margin-right: auto; margin-left: auto;'  class="aligncenter size-full wp-image-2707 imgborder" title="CSS sprite example" src="//www.webmaster-source.com/wp-content/uploads/sprites-example-apple.jpg" alt="Sprites example" width="510" height="378" /></p>
]]></content:encoded>
			<wfw:commentRss>https://www.webmaster-source.com/2009/11/06/learning-css-sprites/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/


Served from: www.webmaster-source.com @ 2026-06-09 00:55:21 by W3 Total Cache
-->