我的页面上有一系列 div。每个 div 都有一个背景图像,并以网格形式排列。我的页面上有任意数量的 div。页面被限制为使用<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
我希望能够点击一个 div,让它缩放到特定的比例,然后居中。
我的标记:
<body id="body">
<div id="container" style="position: relative">
<div id="pack1" class="screenItem cardPack"></div>
<div id="pack2" class="screenItem cardPack"></div>
<div id="pack3" class="screenItem cardPack"></div>
<div id="pack4" class="screenItem cardPack"></div>
</div>
</body>
我的CSS:
#pack1{
margin-left: 20px;
margin-top: 20px;
height: 193px;
width: 127px;
background-image: url(../images/image1.png);
background-size: 100% 100%;
float: left;
clear: both;
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
-ms-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}
#pack2{
margin-right: 20px;
margin-top: 20px;
height: 193px;
width: 127px;
background-image: url(../images/image2.png);
background-size: 100% 100%;
float: right;
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
-ms-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}
#pack3{
margin-left: 20px;
margin-top: 20px;
height: 193px;
width: 127px;
background-image: url(../images/image3.png);
background-size: 100% 100%;
float: left;
clear: both;
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
-ms-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}
#pack4{
margin-right: 20px;
margin-top: 20px;
height: 193px;
width: 127px;
background-image: url(../images/comingSoon.png);
background-size: 100% 100%;
float: right;
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
-ms-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}
#body,
.ui-page {
background-image: url(../images/bg.png);
background-size: auto 100%;
background-repeat: repeat-x;
}
#container {
margin: auto;
width: 310px;
height: 568px;
}
我有一个几乎可以工作的软糖:
$(cardPack).click(function() {
var left = $(cardPack).position().left;
var top = $(cardPack).position().top;
$(cardPack).css("-webkit-transform", "scale(2.5,2.5)");
$(cardPack).css("-webkit-transform-origin", (3*(left/4)) + " " + (3*(top/4)));
});
但我认为这更多的是巧合和运气。我的大脑没有工作到可以锻炼设置位置的程度,transform-origin
以便图像最终位于屏幕中心,无论其起点如何。
我很高兴考虑替代方案来transform-origin
实现这一目标。
编辑:“与本地的行为不太一样”jsfiddle:http: //jsfiddle.net/a7Cks/9/