我的页面上有一系列div。每个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/
答案 0 :(得分:6)
只是为了好玩,因为我们讨论的是css3,有纯粹的css解决方案,使用target:
选择器http://codepen.io/dmi3y/full/keAds,它从初始数据中被清理干净
HTML
<div id="container">
<a href="#pack1" id="pack1" class="screenItem cardPack"></a>
<a href="#pack2" id="pack2" class="screenItem cardPack"></a>
<a href="#pack3" id="pack3" class="screenItem cardPack"></a>
<a href="#pack4" id="pack4" class="screenItem cardPack"></a>
</div>
LESS
#pack1{
margin-left: 20px;
margin-top: 20px;
float: left;
clear: both;
&:target {
top: 105px; left: 75px;
margin-left: 0;
margin-top: 0;
}
}
#pack2{
margin-right: 20px;
margin-top: 20px;
float: right;
&:target {
top: 105px; left: -75px;
margin-right: 0;
margin-top: 0;
}
}
#pack3{
margin-left: 20px;
margin-top: 20px;
float: left;
clear: both;
&:target {
top: -105px; left: 75px;
margin-left: 0;
margin-top: 0;
}
}
#pack4{
margin-right: 20px;
margin-top: 20px;
float: right;
&:target {
top: -105px; left: -75px;
margin-right: 0;
margin-top: 0;
}
}
#container {
position: relative;
margin: auto;
width: 310px;
height: 568px;
}
.screenItem {
background: green;
position: relative;
height: 193px;
width: 127px;
-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;
}
:target {
background: red;
z-index: 100;
padding: 10px; // + 20 px dimensions
}
答案 1 :(得分:2)
在这里,我是个好先生。
有一个错误,如果你在动画运行时继续点击它会再次出现。 但我认为这是一个很好的方法。
使用animate:
$(cardPack).animate({
width: '50%',
height: '50%',
}, 700, function() {
$(cardPack).animate({
left: (contwidth - width * 1.5) / 2,
top: (contheight - (height+height/2) * 1.5) / 2
}, 300);
});
添加height/2
使其成为对象中间而不是对象顶部的中心。
另一点是使用position:absolute来定位元素。位置:css中的静态是多余的。