有5个瓷砖,如果我点击其中一个,它会打开(宽度为100%),当我点击右上角的X时,它会关闭,这很好。但是当它被打开并且我再次点击蓝色框时,它将覆盖全局变量,并且onclick Close不再知道旧位置。
这里是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;
}
/*//////////////////////////////////////
Tile Width And Location eddid here
///////////////////////////////////////*/
#tp1{
width: 100px;
height: 50px;
}
/*//////////////////////////////////////
IMG SIZE
///////////////////////////////////////*/
img {
width: 100%;
height: 100%;
}
/*//////////////////////////////////////
Close Button
///////////////////////////////////////*/
.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'); //<-- This was my try to get it to work
}
});
$(".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');//<-- This was my try to get it to work
});