从元素中心过渡宽度

时间:2015-09-02 00:04:05

标签: javascript jquery html css

我试图将div转换为宽度(放大),但我希望它从中心而不是左上角增长

HTML

<div id="attach-wrap">
        <div id="attach">

            ... content ...

        </div>
    </div>

CSS     #attach-wrap {min-height:50px; }

#attach { 
  height: 50px;
  width: 60%;
  margin: 0 auto;
  border-top: 1px solid #000;
  border-bottom: 1px solid #000;
  -webkit-transition: height 0.35s ease, width 0.35s ease;
  -moz-transition: height 0.35s ease, width 0.35s ease;
  -ms-transition: height 0.35s ease, width 0.35s ease;
  -o-transition: height 0.35s ease, width 0.35s ease;
  transition: height 0.35s ease, width 0.35s ease; 
}

#attach.stick { 
  height: 80px; 
  width: 100%;
  border-bottom: 1px solid #000;
  background-color: rgba(250,250,250,1);
  position: fixed;
  z-index: 100;
  top: 0; left: 0;
  margin: auto;
}

&#39; .stick&#39;在满足某些条件时添加,从而激活它。一切都很完美,除非添加.stick类并设置高度和宽度动画,它是从左到右完成的。我希望它来自中心。当删除.stick类时,它会从中心缩小,不知道为什么

1 个答案:

答案 0 :(得分:0)

尝试从position:fixed删除#attach.stick。这应该允许它像你期望的那样扩展。

JSFiddle(点击框切换)

如果您需要position:fixed,请始终为#attach提供明确的保证金,并为保证金设置动画(Fiddle):

#attach {
    height: 50px;
    width: 60%;
    margin: 0 20%;
    border-top: 1px solid #000;
    border-bottom: 1px solid #000;
    -webkit-transition: margin 0.35s ease, height 0.35s ease, width 0.35s ease;
    -moz-transition: margin 0.35s ease, height 0.35s ease, width 0.35s ease;
    -ms-transition: margin 0.35s ease, height 0.35s ease, width 0.35s ease;
    -o-transition: margin 0.35s ease, height 0.35s ease, width 0.35s ease;
    transition: margin 0.35s ease, height 0.35s ease, width 0.35s ease;
}

#attach.stick {
    height: 80px;
    width: 100%;
    border-bottom: 1px solid #000;
    background-color: rgba(250, 250, 250, 1);
    z-index: 100;
    top: 0;
    left:0;
    margin: 0;
    position:fixed;
}