0

I have a variable number of promotional items (panels) that are in a sliding belt, that should be set to the width of a panel (300px) multiplied with the amount of panels.

It alerts the correct beltsize. With fixed numbers the slider works too. I suspect the error to be in the if/else if part. I am not even sure this is valid Javascript Syntax.

Any hint is appreciated.

   $(window).ready(function(){  
        var whichpanel = 1;
        var panels = $(".panel").length;
        var beltsize = panels*300;
        $('.belt').css({'width':beltsize});
    });

    $(window).ready(function promoslider(){
        if (panels>whichpanel){
            $('.belt').delay(7000).animate({'left':'-=300'}, 500);
            whichpanel += 1;    
        }
        else if (panels=whichpanel){
            $('.belt').delay(7000).animate({'left':'0'}, 500);
            whichpanel = 1;
        }   
        setTimeout(promoslider, 0);

    });
    promoslider;

UPDATE! Here is the code that works for me now (http://jsfiddle.net/zr5Nd/10/):

$(window).ready(function () {
var whichpanel = 1;
var panels = $(".panel").length;
var beltsize = panels * 300;
$('.belt').css({
    'width': beltsize
});

function movingdiv() {
    if (panels > whichpanel) {
        //alert('Panels:' + panels + '/whichpanel:' + whichpanel);
        $('.belt').delay(1000).animate({
            'margin-left': '-=300px'
        }, 500);
        whichpanel += 1;

    } else if (panels == whichpanel) {
        //alert('Panels:' + panels + '/whichpanel:' + whichpanel);
        $('.belt').delay(1000).animate({
            'margin-left': '0'
        }, 500*panels);
        whichpanel = 1;

    } else {
        alert('3');
    }
    setTimeout(movingdiv, 0);
}

setTimeout(movingdiv, 0);

});

2
  • I changed promoslider; to promoslider(); put it inside the ready function too, also changed the operator, but it still does not move. There must be one or more additional errors ... Commented Apr 8, 2013 at 12:21
  • Here is a fiddle for it: jsfiddle.net/Coffeehouse/zr5Nd/3 since it still does not work Commented Apr 8, 2013 at 13:22

3 Answers 3

1

You need to use the equality/identity operators (==/===) instead of the assignment operator = in your else if statement, e.g.

else if (panels == whichpanel){
   $('.belt').delay(7000).animate({'left':'0'}, 500);
   whichpanel = 1;
}  

Also, I believe promoslider; is supposed to be promoslider();.

Sign up to request clarification or add additional context in comments.

6 Comments

Ok, I changed both (though it worked before with the brackets for promoslider). But there must be an additional error, because it still does not move.
@user2164882 Can you make a little jsFiddle with your HTML/jQuery? I need your HTML. I can fix it quickly then.
It works now: jsfiddle.net/zr5Nd/10 The reason seemed to be that they needed to be in the same window ready function, also the 'left' did not work, so I had to use 'margin-left'. I assume that is because of some missing or wrong position attributes and the floating divs, because it worked with just 'left' in my live system.
@user2164882 Yep, I tried putting both inside the same window ready yesterday, but didn't do the 'left' part, so it didn't work :p Can I update the full code as the answer? Just for future readers who may not read our comments.
Sure you can. I am new to stackoverflow.
|
0

You should put the call of promoslider into $(window).ready as well, as the function does not yet exist otherwise. Plus you have to do the call this way promoslider(), promoslider alone will not result in a call. And of course you have to use the equals-operator == instead of the assign-operator = in your if else-statement.

Comments

0

use panels === whichpanel instead of panels=whichpanel

variables whichpanel, panels have lost their scope in the if/else if part.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.