-1

Ok so im making a slot machine look alike - for this i need to make a sprite of 5 picture lets say 500 height then and 100 width (each image is 100x100). I need it so that i have a button on top of the image and one in the button og the image - so i can go to next picture with the buttom button (new position on the sprite) or previous with the top button.

I hope someone can help me since i have been searching for something i can use all day. Im sure its very simple for some of you guys.

thx

user1562679
  • 145
  • 1
  • 3
  • 10
  • i try this above - i cant upload the sprite to fiddle can i? So if you think you can fix it i can sent you the files ? – user1562679 Jul 30 '12 at 14:58
  • This is working, i just need to have the top button added and also instead of text for buttons i need images... Or i need a complete new script since im not good enough to make my own java scripts... – user1562679 Jul 30 '12 at 15:00

1 Answers1

3

I believe you have all the pieces, you just need to put them together, so lets break this apart.

If you keep track of what image is currently being shown, and you know the height of each image, it is quite easy to calculate the proper background position. Let's say we call your tracking-variable i. Notice that i have to be zero-based for this to work. We assume the height of each image is 100px. We also multiply by -1 to get the negative value that you need.

var position = "0 " + (i * 100 * -1) + "px"; // Will be something like "0 -100px"

You can change the background-position using jQuery's .css() something like this:

$("#your-image-container-id").css("backgroundPosition", position);

You can add a click event-listener for your buttons with jQuery's .click().

$("#your-button").click(function () {
    /* 
    Do your rotation magic here
    For the next button increase i by one and apply the new position
    For the prev button you decrease i by one instead...
    */
});

With those pieces, I believe you should be able to assemble the code you need. If you get stuck at some place, feel free to ask.

Update:

I have assembled some of the pieces for you:

$(function () {    
    var i = 0,
        numberOfImages = 5;

    // Handle click on next button
    $(".next-btn").click(function () {
        // Increase by one, and restart when we reach the last image
        i = ((i + 1) < numberOfImages) ? i + 1 : 0;
        var position = calculateBackgroundPosition(i);
        $(".image-container").css("backgroundPosition", position);
    });
});

function calculateBackgroundPosition(index)
{
    return "0 " + (index * 100 * -1) + "px";
}

It is also available in this fiddle. What remain to be done is to implement the previous-button, but that will be your task. Take a look a how the next-button is implemented and then give it a go!

Update 2:

It seems to be a bit troublesome to animate background-position in Firefox. I found this SO answer that describe an extension to make it work properly in Firefox. Unfortunately Firefox doesn't support background-position-y, and the mentioned extension does not support jQuery's backgroundPosition: "+=50px" syntax. So I had to do a workaround.

It is not as smooth, but if you include the above mentioned extension. You can get it to work with the following code:

$(function () {    
    var i = 0,
        numberOfImages = 5;

    $(".op").click(function () {
        // Decrease by one, and restart when we reach the first image
        i = ((i - 1) >= 0) ? i - 1 : numberOfImages - 1;
        animate(this, i);
    });

    $(".ned").click(function () {
        // Increase by one, and restart when we reach the last image
        i = ((i + 1) < numberOfImages) ? i + 1 : 0;
        animate(this, i);
    });
});

function calculateBackgroundPosition(index)
{
    return "0 " + (index * 50 * -1) + "px";
}

function animate (that, i)
{
    var position = calculateBackgroundPosition(i);
    $(that).parent().find(".hjul").animate({"backgroundPosition": position});    
}

Here is a working example as well.

It doesn't behave perfectly when it reaches the last image and should start over, or vice versa, but it is the best I have at the moment.

Update 3:

To make it work with multiple wheels, you need to have a separate counter variable i for each wheel. Otherwise they will affect each other. I updated your code so that the ID hjulwrapper now is a class hjulwrapper instead. An ID have to be unique to a single element. So make sure to update your CSS accordingly. Apart from that, you have to update some of your code to with this:

$(function () {

    $(".hjulwrapper").each(function () {

        var i = 0,
            numberOfImages = 5;

        $(".op", this).click(function () {
            // Decrease by one, and restart when we reach the first image
            i = ((i - 1) >= 0) ? i - 1 : numberOfImages - 1;
            animate(this, i);
        });

        $(".ned", this).click(function () {
           // Increase by one, and restart when we reach the last image
            i = ((i + 1) < numberOfImages) ? i + 1 : 0;
           animate(this, i);
        });
    });
});

Notice that I loop over each hjulwrapper and create an individual spinner for each wheel.

Here is a working fiddle: http://jsfiddle.net/yEhpF/65/

Community
  • 1
  • 1
Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
  • im Sorry but im not good enough to make this work... Can you look here and give me some more pointers? Really all help is appreciated, i need this done by tomorrow :-/ http://jsfiddle.net/3BZEH/3/ – user1562679 Jul 30 '12 at 16:45
  • If anyone knows how to get further, please post. http://jsfiddle.net/3BZEH/21/ link to fiddle – user1562679 Jul 30 '12 at 17:32
  • @user1562679 I have implemented most of it for you [here](http://jsfiddle.net/3BZEH/22/), now it is up to you to implement the previous button. Let me know if you have any questions, but give it a shot! – Christofer Eliasson Jul 30 '12 at 17:51
  • Ok i made this, but for some reason it wont work in firefox ??? `code ` – user1562679 Jul 30 '12 at 20:41
  • `code ` – user1562679 Jul 30 '12 at 20:47
  • body {background-color:#789;} #hjulwrapper .hjul { height: 50px; width: 50px; background-color: #000; } .ned {background:url(next-btn.png);} – user1562679 Jul 30 '12 at 20:48
  • Dont know how to use those code tags, they made it kinda weird here ?? Hope you can still understand it... Any idea why it wont work in Firefox and what i should do to make it work? This stupid scripts is killing me....:-) – user1562679 Jul 30 '12 at 20:49
  • @user1562679 Could you show it in a fiddle instead? It would be a lot easier to play around with it there. – Christofer Eliasson Jul 30 '12 at 22:32
  • yes but i cant seem to upload my sprite in fiddle, then u cant see the animation anyway?? Or how do i upload pictures there? – user1562679 Jul 31 '12 at 06:17
  • @user1562679 I guess you could use a placeholder image, like [this one](http://placehold.it/100x500), and reference it instead of your image. – Christofer Eliasson Jul 31 '12 at 09:55
  • Finally i got it working with the image uploader! Here is the jsfiddle: http://jsfiddle.net/iBertel/yEhpF/15/ But it wont work in the fiddle, only works in safari >< Please help me solve this – user1562679 Jul 31 '12 at 11:12
  • @user1562679 You didn't load jQuery for the fiddle in the link you sent me. Added it for you [here](http://jsfiddle.net/yEhpF/20/). I also adjusted the animation to increase/decrease with 50px instead of 183px, so that it steps one number at a time. – Christofer Eliasson Jul 31 '12 at 11:32
  • Yea i uploaded the wrong number in fiddle :P Sorry - but still, you can see it dont work right? I cant get pass picture 1 unless i look in safari! In FF i wont work??? What can this be? – user1562679 Jul 31 '12 at 11:50
  • I didn't try it in firefox, so I missed that. Firefox doesn't support `background-position-y`. You will have to use regular background-position to make it work in all browsers. I don't have time to fix it for you right now, but give it a try and if you can't solve it, I can help you in a couple of hours. – Christofer Eliasson Jul 31 '12 at 12:32
  • I have been trying some different approaches now, but cant seem to get anything to work - if you can i would appreciate if you could see a fix. This is what i tried so far: http://jsfiddle.net/yEhpF/51/ – user1562679 Jul 31 '12 at 13:14
  • @user1562679 See my updated answer. I believe that is the best I have to offer. – Christofer Eliasson Jul 31 '12 at 16:52
  • Imagine that something so "simple" should be such a hassle!!! Thank you very much for all this help, its very much appreciated. I can live with the backspin everytime it needs a loop - once again, THANKS! – user1562679 Aug 01 '12 at 07:17
  • Im so sorry to pull this up, but i just saw a bug - its nothing you could know, but if you know a quick fix to it, i would appreciate it! look at this fiddle: http://jsfiddle.net/yEhpF/61/ Its because i will need 5 of these wheels in my script, and i notice that its like it stacks up - so if i push the first wheel it take 1 step, after i push secound whell i take 2 steps etc - all whells should only take 1 step - is this to much work to fix? – user1562679 Aug 01 '12 at 07:34
  • Hey again - sorry to revive this, but suddently im having issues!! Look here: http://floatleft.dk/wheel/#op i get this error: TypeError: $.curCSS is not a function its weird, becuase it has been working flawlessly, and i havent updated my browsers or anything - also the fidlle still works, but if i copy the code into my docs, this happens. SOS [Break ved denne fejl] var start = $.curCSS(fx.elem,'backgroundPosition'); – user1562679 Aug 10 '12 at 07:11
  • I solved it by using and older jquery version: 1.7.2 jquery - must be because the latest version is not working properly? – user1562679 Aug 10 '12 at 07:38
  • @user1562679 Glad you solved it! Not sure why that happend. I haven't had time to look into the changes that were made in jQuery 1.8 yet. Maybe they broke something with it, not sure, a bit hard to believe though... – Christofer Eliasson Aug 10 '12 at 08:40
  • Is it possible to make it start in middle of sprite instead of top? Like if the sprite is 800 px high i want it to start at 400 px. I have been changing some pixels values in the code, but i cant seem to make it have a new starting point. thx – user1562679 Aug 10 '12 at 13:19
  • @user1562679 Sure, just update your calculateBackgroundPosition function to something like this: `function calculateBackgroundPosition(index) { return "0 " + (index * 50 * -1 - 400) + "px"; }` – Christofer Eliasson Aug 10 '12 at 13:39
  • That doesnt seem to change the starting point on the strip but rather how many steps it takes on each click?? – user1562679 Aug 10 '12 at 13:54
  • @user1562679 I believe that should work, given that you provide a proper starting point in your CSS, so that when the page is loaded, the background-position is set to `0 -400px`. – Christofer Eliasson Aug 10 '12 at 14:03