OPEN FOR DISCUSSION
I want to optimize this code, perhaps make an object. But more importantly, I'm new to features. I would like to learn how to transfer, retrieve, return variables. If anyone has good links to this tutorial or would like to discuss, please start here. :)
As we know, images are rectangles. I have several transparent PNGs inside a div on separate z-index layers. I ran into a problem when I tried to click on one of the images, but it was blocked by a div on top of it with a higher z index. Can I click PNG and their parent div to achieve this effect? Please see the attached image as a reference, this illustrates my idea better.
Thank!

In short, for some reason, I thought I could use PNG alpha as a hit. Lol @ this idea. So I created an overlaying div in a new separate container, and they acted like hits.

As you can see, this is an endless carousel. Z-indexes of hits, as well as z-indexes of images change by click. It was an interesting project, to say the least. I want to optimize it and learn how to be more efficient with functions (for example, passing, extracting vars, returning vars, etc.).
I will write javascript when I get home to tempt an interesting conversation, I hope. If you have ideas for optimization, please share! :)
(function (w, d) {
$(w).load(function () {
preload();
$('#info div a:not([rel=ad])').find('img').hide();
});
$(d).ready(function () {
onHover();
onClick();
});
var isFirst = ["1"],
pxPos = ["1px", "399px", "577px", "782px", "969px", "1162px", "1330px"],
name = ["bill", "sophia", "brian", "jenn", "mom"],
thisZ = 0;
$('.left, .right').hide();
function onHover () {
$('.hit').hover(function () {
var _this = this,
thisName = $(_this).parent().attr('id'),
img = '_img/' + thisName + '.png';
$(_this).parent().next().find('img').attr('src', img);
}, function() {
var _this = this,
thisName = $(_this).parent().attr('id'),
img = '_img/' + thisName + '-d.png';
if (!($(this).parent().next().hasClass('current'))) {
$(_this).parent().next().find('img').attr('src', img);
}
})
}
function onClick () {
$('a.left').bind('click', function (e) {
e.preventDefault();
if (!$('.p').is(":animated")) {
pos = 'p5';
var o = [name[1],name[2],name[3],name[4],name[0]];
name = o;
var thisName = name[3];
updateArr(thisName, pos);
};
})
$('a.right').bind('click', function (e) {
e.preventDefault();
if (!$('.p').is(":animated")) {
pos = 'p3';
var o = [name[4],name[0],name[1],name[2],name[3]];
name = o;
var thisName = name[3];
updateArr(thisName, pos);
}
})
$('.hit').bind('click', function () {
if (!$('.p').is(":animated")) {
var _this = this;
var thisName = $(_this).parent().attr('id');
if(isFirst[0] === "1") {
$('h1').hide();
$('.left, .right').fadeIn().show();
$('#bg2').fadeIn().show();
var pos = checkPosition(_this);
updateArr(thisName, pos);
isFirst[0] = "2";
}else if(isFirst[0] === "2") {
var pos = checkPosition(_this);
updateArr(thisName, pos);
}
}
})
}
function checkPosition (_this, thisName) {
var pos;
if($(_this).parent().next().hasClass('p1')) {
pos = 'p1';
}else if($(_this).parent().next().hasClass('p2')) {
pos = 'p2';
}else if($(_this).parent().next().hasClass('p3')) {
pos = 'p3';
}else if($(_this).parent().next().hasClass('p4')) {
pos = 'p4';
}else if($(_this).parent().next().hasClass('p5')) {
pos = 'p5';
}
return pos;
}
function updateArr (thisName, pos) {
for(l=0; l <= name.length; l++) {
if(thisName == name[l]) {
var thisIndex = l;
}
}
if(thisName === name[thisIndex]) {
var o = [];
if(thisIndex === 0) {
o = [name[2],name[3],name[4],name[0],name[1]];
}else if(thisIndex === 1) {
o = [name[3],name[4],name[0],name[1],name[2]];
}else if(thisIndex === 2) {
o = [name[4],name[0],name[1],name[2],name[3]];
}else if(thisIndex === 3) {
o = [name[0],name[1],name[2],name[3],name[4]];
}else if(thisIndex === 4) {
o = [name[1],name[2],name[3],name[4],name[0]];
}
name = o;
updateFlags();
}
$.each($('.p'), function () {
if(($(this).attr('class').length > 5)) {
var oldImg = $(this).find('img').attr('src');
img = '_img/' + $(this).prev().attr('id') + '-d.png';
$(this).find('img').attr('src', img);
$(this).removeClass('current');
}
});
$('#' + thisName).next().addClass('current');
img = '_img/' + thisName + '.png';
$('#' + thisName).next().find('img').attr('src', img);
updateZIndex();
animate(pos);
var current = thisName;
return disorderText(current);
}
function updateFlags() {
$('.p').each (function() {
$(this).removeClass('p1 p2 p3 p4 p5');
});
for(i=0; i < name.length; i++) {
$('#' + name[i]).next().addClass('p' + (i + 1));
}
}
function updateZIndex (thisZ) {
$("#" + name[3]).children().each(function(thisZ) {
thisZ++;
$(this).css({'z-index': thisZ + 800});
});
$("#" + name[4]).children().each(function(thisZ) {
thisZ++;
thisZ = thisZ + 1;
$(this).css({'z-index': thisZ + 700});
});
$("#" + name[2]).children().each(function(thisZ) {
thisZ++;
thisZ = thisZ + 1;
$(this).css({'z-index': thisZ + 600});
});
$("#" + name[1]).children().each(function(thisZ) {
thisZ++;
thisZ = thisZ + 1;
$(this).css({'z-index': thisZ + 500});
});
$("#" + name[0]).children().each(function(thisZ) {
thisZ++;
thisZ = thisZ + 1;
$(this).css({'z-index': thisZ + 400});
});
$('.p1').css({'z-index': 40});
$('.p2').css({'z-index': 50});
$('.p3').css({'z-index': 60});
$('.p4').css({'z-index': 70});
$('.p5').css({'z-index': 30});
}
function animate (pos) {
var selector1 = '#' + name[0] + ', #' + name[0] + 'i',
selector2 = '#' + name[1] + ', #' + name[1] + 'i',
selector3 = '#' + name[2] + ', #' + name[2] + 'i',
selector4 = '#' + name[3] + ', #' + name[3] + 'i',
selector5 = '#' + name[4] + ', #' + name[4] + 'i',
easeType = 'easeOutCubic',
easeOutType = 'easeOutCubic';
if (pos === 'p1') {
$(selector1).animate({'left': pxPos[6]}, 400, easeOutType).delay(0).animate({'opacity': 0}, 0).animate({'left':pxPos[0]}, 0).animate({'opacity': 100}, 0).animate({'left': pxPos[1]}, 600, easeType);
$(selector2).animate({'left': pxPos[6]}, 400, easeOutType).delay(0).animate({'opacity': 0}, 0).animate({'left':pxPos[0]}, 0).animate({'opacity': 100}, 0).animate({'left': pxPos[2]}, 600, easeType);
$(selector3).animate({'left': pxPos[6]}, 400, easeOutType).delay(0).animate({'opacity': 0}, 0).animate({'left':pxPos[0]}, 0).animate({'opacity': 100}, 0).animate({'left': pxPos[3]}, 600, easeType);
$(selector4).animate({'left': pxPos[4]}, 1350, easeType);
$(selector5).animate({'left': pxPos[5]}, 1350, easeType);
}else if (pos === 'p2') {
$(selector1).animate({'left': pxPos[6]}, 400, easeOutType).delay(0).animate({'opacity': 0}, 0).animate({'left':pxPos[0]}, 0).animate({'opacity': 100}, 0).animate({'left': pxPos[1]}, 700, easeType);
$(selector2).animate({'left': pxPos[6]}, 400, easeOutType).delay(0).animate({'opacity': 0}, 0).animate({'left':pxPos[0]}, 0).animate({'opacity': 100}, 0).animate({'left': pxPos[2]}, 700, easeType);
$(selector3).animate({'left': pxPos[6]}, 400, easeOutType).delay(0).animate({'opacity': 0}, 0).animate({'left':pxPos[0]}, 0).animate({'opacity': 100}, 0).animate({'left': pxPos[3]}, 700, easeType);
$(selector4).animate({'left': pxPos[4]}, 1550, easeType);
$(selector5).animate({'left': pxPos[5]}, 1550, easeType);
}else if (pos === 'p3') {
$(selector1).animate({'left': pxPos[6]}, 200, easeOutType).delay(0).animate({'opacity': 0}, 0).animate({'left':pxPos[0]}, 0).animate({'opacity': 100}, 0).animate({'left': pxPos[1]}, 500, easeType);
$(selector2).animate({'left': pxPos[2]}, 700, easeType);
$(selector3).animate({'left': pxPos[3]}, 700, easeType);
$(selector4).animate({'left': pxPos[4]}, 700, easeType);
$(selector5).animate({'left': pxPos[5]}, 700, easeType);
}else if (pos === 'p4') {
$(selector1).animate({'left': pxPos[1]}, 500, easeType);
$(selector2).animate({'left': pxPos[2]}, 700, easeType);
$(selector3).animate({'left': pxPos[3]}, 700, easeType);
$(selector4).animate({'left': pxPos[4]}, 700, easeType);
$(selector5).animate({'left': pxPos[5]}, 700, easeType);
}else if (pos === 'p5') {
$(selector1).animate({'left': pxPos[1]}, 700, easeType);
$(selector2).animate({'left': pxPos[2]}, 700, easeType);
$(selector3).animate({'left': pxPos[3]}, 700, easeType);
$(selector4).animate({'left': pxPos[4]}, 700, easeType);
$(selector5).animate({'left': pxPos[0]}, 200, easeOutType).delay(0).animate({'opacity': 0}, 0).animate({'left':pxPos[6]}, 0).animate({'opacity': 100}, 0).animate({'left': pxPos[5]}, 500, easeType);
}
}
function disorderText (current) {
var _this = ['swd', 'osa', 'nar'],
swd = "Of the 15 million Americans who work evening or night shirts, 25% may have shift work disorder.",
osa = "18 million Americans suffer from OSA.",
nar = "Narcolepsy affects 1 in every 2,000 Americans.",
link = "<a href=''><img src='_img/learn-more.png' width='125' height='31' class='learn_more' /></a>",
brian_quote = '"' + "I get enough sleep during the day, but I'm still exhausted at work." + '"',
sophia_quote = '"' + "Since I started working nights, I struggle to stay awake." + '"',
jenn_quote = '"' + "I'm so tired on my shift that it hard to do my job" + '"',
bill_quote = '"' + "I struggle to stay awake even outside of work - I'm almost dozing off at my son Little League games." + '"',
mom_quote = '"' + "Quote to come." + '"',
i = 0,
p_name = "",
quote = "",
info = "",
disorderArr = ["_this", "swd", "osa", "nar", "link", "brian_quote", "sophia_quote", "jenn_quote", "bill_quote", "mom_quote", "i", "pname"];
$('#info').children().each (function () {
$(this).removeClass('open');
$(this).find('.content').html('<p>');
});
switch(current) {
case 'brian' : i = 0;
p_name = '<h2><b>Alex,</b> Bartender</h2>';
info = swd;
quote = brian_quote;
break;
case 'sophia' : i = 0;
p_name = '<h2><b>Sophia,</b> EMT</h2>';
info = swd;
quote = sophia_quote;
break;
case 'jenn' : i = 0;
p_name = '<h2><b>Susan,</b> Nurse</h2>';
info = swd;
quote = jenn_quote;
break;
case 'bill' : i = 1;
p_name = '<h2><b>Martin,</b> Real Estate</h2>';
info = osa;
quote = bill_quote;
break;
case 'mom' : i = 2;
p_name = '<h2><b>Susan,</b> Mom</h2>';
info = nar;
quote = mom_quote;
break;
}
$('#' + _this[i]).addClass('open');
$('#info').children().each (function () {
if($(this).hasClass('open')) {
$(this).find('.header span').addClass('down');
$(this).children().find('a img').show();
$(this).find('.content').addClass('disorder');
}else if(!$(this).hasClass('open')) {
$(this).find('.header span').removeClass('down');
$(this).find('.content').removeClass('disorder');
}
});
return $('#' + _this[i]).find('.content').html(p_name + '<p class="gen_quote"><i>' + quote + '</i></p>' + '<p class="gen_info"><b>' + info + '</b></p>' + '<p class="learn-more">' + ' ' + link);
}
function preload(imgArray) {
$(imgArray).each(function(){
$('<img/>')[0].src = this;
});
}
preload([ '_img/ad.png', '_img/arrow_sm_d.png', '_img/arrow_sm.png', '_img/arrow-left.png', '_img/arrow-right.png', '_img/bill-d.png', '_img/bill.png', '_img/border.png', '_img/brian-d.png', '_img/brian.png', '_img/corner.png', '_img/hit.gif', '_img/jenn-d.png', '_img/jenn.png', '_img/mom-d.png', '_img/mom.png', '_img/sidebar-bg.png', '_img/sophia-d.png', '_img/sophia.png', '_img/stone-bg-d.jpg', '_img/stone-bg.jpg' ]);
} (window, document));