9

我的页面上有一系列 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/

4

2 回答 2

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>

较少的

#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
}
于 2012-12-13T21:51:20.793 回答
3

给你,我的好先生。

有一个错误,如果您在动画运行时继续单击,它将再次运行。但我认为这是一个很好的方法。

使用动画:

      $(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);
        });

http://jsfiddle.net/a7Cks/11/

添加height/2以使其居中于对象中间而不是对象顶部。

另一点是使用 position:absolute 来定位元素。css 中的 position:static 是多余的。

于 2012-12-07T17:06:02.320 回答