Reputation: 21
here i have 6 divs (.sticker) inside a single div, onClicking one of them i would like to fadeOut the others keeping the clicked one in it position (that's why i do the postop/posleft thing) then i want to move it on the middle of the bigger div while it grows by height and width, showing a hided div (.info). The same for closing! So, this code it's working but it's really laggy, it's not smooth as jQuery should be, am i doing something wrong?
Thanks to all the comunity!
$("body").on('click', '.sticker', function () {
if (!is_open) {
postop = $(this).position().top;
posleft = $(this).position().left;
$('.sticker').not(this).fadeOut(350, function () {
$(".sticker").css("position", "absolute").css("left", posleft + "px").css("top", postop + "px");
$(".sticker").animate({
'top': '0px',
'left': '300px',
'height': '480px',
'width': '750px',
'left': '90px'
}, 350);
$(".sticker").children(".wrap").animate({
'height': '343px',
'width': '750px'
}, 350);
$(".sticker").find(".imgspace").animate({
'height': '343px',
'width': '750px'
}, 350);
$(".sticker").find(".info").animate({
'height': '100px'
}, 350);
$('.arrow-left').animate({
'left': '-20px'
}, 450);
$('.arrow-right').animate({
'left': '880px'
}, 450);
is_open = true;
});
}
if (is_open) {
$(".sticker").children(".wrap").animate({
'height': '193px',
'width': '300px'
}, 350);
$(".sticker").find(".imgspace").animate({
'height': '193px',
'width': '300px'
}, 350);
$(".sticker").find(".info").animate({
'height': '0px'
}, 350);
$(".sticker").animate({
'height': '230px',
'width': '300px',
'top': postop,
'left': posleft
}, 350, function () {
$(".sticker").css("position", "static");
$(".sticker").not(this).fadeIn(300);
is_open = false;
});
}
});
Upvotes: 0
Views: 425
Reputation: 16990
As Yotam has commented, difficult to debug without a jsFiddle. But things that stuck out to me (and in no way is this exhaustive and I am no JavaScript expert):
You could further simplify the code by setting your values to variable objects rather than call the same method twice hard coded with different values.
$("body").on('click', '.sticker', function () {
if (!is_open) {
var $position = $(this).position(),
$sticker = $('.sticker');
$sticker.not(this).fadeOut(350, function () {
$sticker.css({
position: 'absolute',
left: $position.left+'px',
top: $position.top+'px'
})
.animate({
'top': '0px',
'left': '300px',
'height': '480px',
'width': '750px',
'left': '90px'
}, 350);
$sticker.find(".wrap, .imgspace").animate({
'height': '343px',
'width': '750px'
}, 350);
$sticker.find(".info").animate({ 'height': '100px' }, 350);
$('.arrow-left').animate({ 'left': '-20px' }, 450)
.animate({ 'left': '880px' }, 450);
is_open = true;
});
}
if (is_open) {
$sticker.find(".wrap, .imgspace").animate({
'height': '193px',
'width': '300px'
}, 350);
$sticker.find(".info").animate({
'height': '0px'
}, 350);
$sticker.animate({
'height': '230px',
'width': '300px',
'top': $position.top,
'left': $position.left
}, 350, function () {
$sticker.css("position", "static")
.not(this).fadeIn(300);
is_open = false;
});
}
});
Upvotes: 0