1

I have this problem with this code. I'm a novice javascript,jQuery programmer and trying to make a code to make one picture stay focused and the other ones animate to be smaller.

So here's the code:

var picHeight = '75';
var picWidth = '100';
var picAmount = 12;
var prev = 0;
var next = 0;
var picNr = '0';

function animatePictures(picNr) {

$('#pic'+picNr).animate({
    height: picHeight+'px' ,
    width: picWidth+'px'
},500);

}

function animateSidePics(prev,next) {

if ( next>=picAmount+1 ) { }
else {
    var h=picHeight;
    var w=picWidth;
    for ( var i=next; i<=picAmount; i++ )
    {
        h=h-(h*0.3);
        w=w-(w*0.3);
        $('#pic'+i).animate({
            height: h+'px' ,
            width: w+'px'
        },500);

    }
}

if ( prev==0 ) {  }
else {
    var hh=picHeight;
    var ww=picWidth;
    for ( var i=prev; i>=1; i-- )
    {
        hh=hh-(hh*0.3);
        ww=ww-(ww*0.3);
        $('#pic'+i).animate({
            height: hh+'px' ,
            width: ww+'px'
        },500);

    }
}

}

for ( var y=1; y<=picAmount; y++ ) {

$('#pic'+y).click(function() {
    animatePictures(y)
    animateSidePics(y+1,y-1)
});

}

Can anyone answer why this code doesn't work? It only makes the last picture focus. Even if you click on the first picture. There must be something with the last for loop I guess.

3
  • 2
    I don't understand this this type of programming if (smth){}else{...}. What's the point? Just do if (!smth){...}
    – elclanrs
    Commented Jun 14, 2013 at 5:13
  • How 'bout considering a different approach. Assign the image that should remain unchanged with the class "active", then use $('img:not(active)").animate(....); This will animate all the images except the active one.
    – Carl
    Commented Jun 14, 2013 at 5:17
  • Would you explain what you are trying to do so that I can help you further ?
    – CME64
    Commented Jun 14, 2013 at 6:28

1 Answer 1

0

if you are trying to animate a specific picture you can use it's id, but if you want to animate it dynamically you use it's index which jquery generates for each, example :

var index = 1;
$('img:eq('+index+')').animate({'height':'100px','width':'100px'});

if you want to depend on the current width and height of the image use operations inside, ex:

.animate({'height':'-=100px','width':'+=100px'});

and don't use loops, jquery does that for you, you just need to figure out your selection

jsfiddle

updated : this is what I think you are trying to do :

$(document).ready(function(){
    $('img').animate({'height':'50px','width':'50px'},500);
    $('img').click(function(){
        $(this).animate({'height':'100px','width':'100px'},500);
        $(this).siblings('img').animate({'height':'50px','width':'50px'},500);
    });
});

jsfiddle2

2
  • Yes thanx tha'ts exactly what I'm trying to do :) Commented Jun 14, 2013 at 14:29
  • good to know that, could you mark it as your question's answer so that other people can know the solution ?
    – CME64
    Commented Jun 14, 2013 at 19:46

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.