View Full Version : Resolved Sprites and backgroundPosition

07-09-2011, 09:39 PM
I am having trouble figuring out what is wrong in this script:

<script type="text/javascript">
var increm = 0;
var handleEvent = function(e) {
switch((e || window.event).keyCode) {
case 90:
case 122:
case 37:
case 38:
case 39:
case 40:

function cycle() {
var order = [0, 1, 0, 2];
if(increm >= order.length) {
increm = 0;
var x = order[increm]*16;
document.getElementById("sprite").style.backgroundPosition = "-16px -16px;";

document.onkeydown = function(e) {
<style type="text/css">
#sprite {
background: url(sprite.png);
background-position: 0px -16px;
position: absolute;
top: 100px;
left: 100px;
height: 16px;
width: 16px;
display: block;
sprite testing<br>
<div id="sprite"></div>

It is very redundant, I know. But my only problem is the highlighted line where the CSS style backgroundPosition is manipulated. It doesn't seem to be working correctly. I changed it to a constant -16px -16px to ensure that variables weren't causing the problem. The original position is 0px -16px and it will not change to -16px -16px when an arrow key is pressed. The sprite image is in the attachments. Please explain to me what is going on here. (I'm using the latest version of FireFox) Thanks!

07-10-2011, 12:39 AM
Lose the quoted ; (red) - it's not part of a valid value for backgroundPosition:

document.getElementById("sprite").style.backgroundPosition = "-16px -16px;";

Even in css it's only required if another property/value pair follows. It's a delimiter in css and has no place in the javascript syntax used here.