There are 5 tiles, if I click on one of them, it will open them (100% width), and when I click on X in the upper right corner, it will close it, which works great. but when it is open and I click the blue frame again, it will redefine the global vars, and onclick Close dose will no longer know the old position.
Fiddle here fiddle
HTML
<div class="tile" id="tp1">
<img class="bus" src="http://s14.directupload.net/images/131130/4gzq9oaz.png" />
<div class="close">x</div>
</div>
CSS
.tile, .tile_open {
position: absolute;
background-color:#0090db;
}
#tp1{
width: 100px;
height: 50px;
}
img {
width: 100%;
height: 100%;
}
.close {
display: none;
background-color:#C85051;
position: absolute;
top: 0;
right: 5px;
width: 50px;
height: 25px;
text-align: center;
}
.open .close {
display: block;
}
JQuery
var posL , posT;
$(".tile").on("click", function (e) {
var pos= $(this).position();
posL = $(this).css('left'),
posT = $(this).css('top');
e.stopPropagation();
if(!$(this).hasClass('open') ) {
$(this).animate({
"top": pos.top - pos.top,
"left":pos.left - pos.left,
"width": "100%",
"height": "100%"
}, 1000).addClass('open')
$(this).removeClass('tile').addClass('tile_open');
}
});
$(".close").on("click", function (e) {
e.stopPropagation();
$(this).parent().stop().animate({
"left" : posL,
"top" : posT,
"width": "100px",
"height": "50px"
}, 1000).removeClass('open')
$(this).removeClass('tile_open').addClass('tile');
});
source
share