从中心扩大div

时间:2015-08-30 11:03:30

标签: jquery html css

我有一个带有背景图像的div,它会放大/缩小。它是在导航中根据滚动改变高度。我正在使用效果很好的css过渡,但是随着行高,填充等的改变我希望它从图像的中心缩放。

CSS

header {
  position: fixed;
  overflow: hidden;
  z-index: 100;
  height: 125px;
  background-color: rgba(250,250,250,1);
  width: 100%;
  top: 0; left: 0;
  padding: 0 25px;
  -webkit-transition: height 0.35s ease;
  -moz-transition: height 0.35s ease;
  -ms-transition: height 0.35s ease;
  -o-transition: height 0.35s ease;
  transition: height 0.35s ease;
}
.brand {
  display: inline-block;
  height: 100px;
  width: 100px;
  margin: 10px 0;
  background: url('img/logo.png') no-repeat center center;
  background-size: contain;
  -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;
}
导航较小时的

CSS

header.shrink {
  height: 80px;
  line-height: 80px;
}

header.shrink .brand {
  height: 40px;
  width: 40px;
  margin: 20px 0;
}

1 个答案:

答案 0 :(得分:0)

Updated Fiddle

说明

CSS边距和过渡可用于实现从中心开始的动画缩小(相对于先前大小的x方向和相对于父元素,标题的y方向)。此处发布的解决方案对宽度,高度和边距使用已知值,以确保在大小切换时图像保持居中。

动画似乎来自中心,因为边距,宽度和高度同时动画 相同的速率Image Demonstration

CSS:

header, .brand {
    /* Animate for 0.35s */
    -webkit-transition: all 0.35s ease;
    -moz-transition: all 0.35s ease;
    -ms-transition: all 0.35s ease;
    -o-transition: all 0.35s ease;
    transition: all 0.35s ease;
}

/* Enlarged styles */
header {
    width: 100%;
    height: 125px;
    position: fixed;
    overflow: hidden;
    z-index: 100;
    background-color: black;
    top: 0; left: 0;
    padding: 0 25px;
}
.brand {
    width: 100px;
    height: 100px;
    margin: 10px 0;
    display: inline-block;
    background: url('http://img1.wikia.nocookie.net/__cb20120104232844/steamplane/images/b/b0/Happy_Face_100x100.gif') no-repeat center center;
    background-size: contain;
}

/* Small styles */
header.shrink {
    height: 80px;
    line-height: 80px;
}
header.shrink .brand {
    width: 40px;
    height: 40px;

    /* Margin shorthand translates to:
     * - Top, bottom margins: 20px each
     * - Left, right margins: 30px each
     */
    margin: 20px 30px;
}

/* Button */
#button{
    margin-top: 130px;
    cursor: pointer;
}