I’ve written some JavaScript to display and subtly hide pieces of information on a staff page. Essentially, there are 3 emblems and 3 sets of description. If you hover over the design emblem, then it will show the design team’s description and images, vice versa. Here is my code to do this:
$('.emblem img:first').hover(
function() {
$('.description:eq(1), .description:eq(2)').css({opacity : 0.2});
$('.devStaff').css({opacity : 0.2});
},
function(){
$('.description:eq(1), .description:eq(2)').css({opacity : 1});
$('.devStaff').css({opacity : 1});
}
);
$('.emblem img:eq(1)').hover(
function() {
$('.description:eq(0), .description:eq(2)').css({opacity : 0.2});
$('.designStaff').css({opacity : 0.2});
},
function(){
$('.description:eq(0), .description:eq(2)').css({opacity : 1});
$('.designStaff').css({opacity : 1});
}
);
$('.emblem img:eq(2)').hover(
function() {
$('.description:eq(0), .description:eq(1)').css({opacity : 0.2});
$('.designStaff').css({opacity : 0.2});
},
function(){
$('.description:eq(0), .description:eq(1)').css({opacity : 1});
$('.designStaff').css({opacity : 1});
}
);
Now looking at this, I feel that there is definitely a better way to do this and I was wondering whether anyone would be able to offer some advice?
Generally, you shouldn’t repeat yourself (http://en.wikipedia.org/wiki/Don't_repeat_yourself),
Try calling a more general function, like this: