i have pieced together some code that works for my project. however it is pretty long, but similar. i am a jquery beginner and i tried to shorten the code by using variables, but it didn't work so far... i guess i should use some for()-statement... could you guys help me to simplify this code?
//apply the class "active" to the menu items whose div is in the viewport and animate
$('#home').bind('inview', function (event, visible) {
if (visible == true) {
$('#menu li:nth-child(1)').stop().animate(
{backgroundPosition:"(0 0)"},
{duration:500});
$('#menu li:nth-child(1) a').css( {'color': 'white'} );
$('#nav li:nth-child(1)').stop().animate({opacity: 1}, 500);
} else {
$('#menu li:nth-child(1)').stop().animate(
{backgroundPosition:"(-200px 0)"},
{duration:500});
$('#menu li:nth-child(1) a').css( {'color': 'black'} );
$('#nav li:nth-child(1)').stop().animate({opacity: 0.2}, 500);
}
});
$('#referenzen').bind('inview', function (event, visible) {
if (visible == true) {
$('#menu li:nth-child(2)').stop().animate(
{backgroundPosition:"(0 0)"},
{duration:500});
$('#menu li:nth-child(2) a').css( {'color': 'white'} );
$('#nav li:nth-child(2)').stop().animate({opacity: 1}, 500);
} else {
$('#menu li:nth-child(2)').stop().animate(
{backgroundPosition:"(-200px 0)"},
{duration:500});
$('#menu li:nth-child(2) a').css( {'color': 'black'} );
$('#nav li:nth-child(2)').stop().animate({opacity: 0.2}, 500);
}
});
$('#unternehmen').bind('inview', function (event, visible) {
if (visible == true) {
$('#menu li:nth-child(3)').stop().animate(
{backgroundPosition:"(0 0)"},
{duration:500});
$('#menu li:nth-child(3) a').css( {'color': 'white'} );
$('#nav li:nth-child(3)').stop().animate({opacity: 1}, 500);
} else {
$('#menu li:nth-child(3)').stop().animate(
{backgroundPosition:"(-200px 0)"},
{duration:500});
$('#menu li:nth-child(3) a').css( {'color': 'black'} );
$('#nav li:nth-child(3)').stop().animate({opacity: 0.2}, 500);
}
});
$('#taetigkeit').bind('inview', function (event, visible) {
if (visible == true) {
$('#menu li:nth-child(4)').stop().animate(
{backgroundPosition:"(0 0)"},
{duration:500});
$('#menu li:nth-child(4) a').css( {'color': 'white'} );
$('#nav li:nth-child(4)').stop().animate({opacity: 1}, 500);
} else {
$('#menu li:nth-child(4)').stop().animate(
{backgroundPosition:"(-200px 0)"},
{duration:500});
$('#menu li:nth-child(4) a').css( {'color': 'black'} );
$('#nav li:nth-child(4)').stop().animate({opacity: 0.2}, 500);
}
});
$('#kontakt').bind('inview', function (event, visible) {
if (visible == true) {
$('#menu li:nth-child(5)').stop().animate(
{backgroundPosition:"(0 0)"},
{duration:500});
$('#menu li:nth-child(5) a').css( {'color': 'white'} );
$('#nav li:nth-child(5)').stop().animate({opacity: 1}, 500);
} else {
$('#menu li:nth-child(5)').stop().animate(
{backgroundPosition:"(-200px 0)"},
{duration:500});
$('#menu li:nth-child(5) a').css( {'color': 'black'} );
$('#nav li:nth-child(5)').stop().animate({opacity: 0.2}, 500);
}
});
$('#anfahrt').bind('inview', function (event, visible) {
if (visible == true) {
$('#menu li:nth-child(6)').stop().animate(
{backgroundPosition:"(0 0)"},
{duration:500});
$('#menu li:nth-child(6) a').css( {'color': 'white'} );
$('#nav li:nth-child(6)').stop().animate({opacity: 1}, 500);
} else {
$('#menu li:nth-child(6)').stop().animate(
{backgroundPosition:"(-200px 0)"},
{duration:500});
$('#menu li:nth-child(6) a').css( {'color': 'black'} );
$('#nav li:nth-child(6)').stop().animate({opacity: 0.2}, 500);
}
});
#anfahrt, etc) the same nodes referenced by thenth-child()? – Michael Aug 4 '12 at 12:27