将元素设置为固定位置的屏幕大小

时间:2016-06-07 18:45:34

标签: javascript jquery html css jquery-animate

如何在点击时为<div>设置动画以展开以适应屏幕。在保持固定位置的同时,揭示<div>

的内容

图像:

sample

2 个答案:

答案 0 :(得分:5)

  1. 为您的元素设置CSS3 100vw
  2. 创建,使您的元素100vh$("#box").on("click", function() { $(this).toggleClass("fullScreen"); });(视口宽度高度单位)
  3. 点击
  4. 添加该课程

    html, body{height:100%;margin:0;}
    
    /* YOUR BOX */
    #box{
      position: fixed;
      overflow: hidden; /* in order to contain content */
      
      /* The initial styles: */
      border-radius: 25px;
      background: red;
      left:50px; bottom:50px;
      width: 50px;
      height: 50px;
      
    
      /* TRANSITION TO THE RESCUE */
              transition: 0.7s;
      -webkit-transition: 0.7s;
    }
    
    /* ADD THIS CLASS WITH JS */
    #box.fullScreen{
      /* Your override styles: */
      border-radius: 0;
      background: gold;
      left:0; bottom:0;
      width: 100vw;
      height:100vh;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="box">CLICK</div>
    {{1}}

答案 1 :(得分:2)

您可以使用transform: scale( )按比例向下和向上缩放整个屏幕框。缩放也会影响框的内容。然后,盒子的小版本具有与全屏盒子相同的宽高比。

实施例

$("#box").on("click", function() {
  $(this).toggleClass("fullScreen");
});
html, body{height:100%;margin:0;}

/* YOUR BOX */
#box{
  position: fixed;
  cursor: pointer;
  background-color: red;
  
  /* make the box full screen */
  width: 100vw; /* IE9+ */
  height:100vh; /* IE9+ */
  top: 0;
  left: 0;

  /* scale it down to 0.1 (10%) initially,
     make an offset from bottom left */
  -ms-transform: translate(50px, -50px) scale(0.1); /* IE9 */
  -ms-transform-origin: left bottom; /* IE9 */
  transform: translate(50px, -50px) scale(0.1);
  transform-origin: left bottom;

  /* smooth transition (IE10+) */
  -webkit-transition: all 0.7s ease;
          transition: all 0.7s ease;
}

/* ADD THIS CLASS WITH JS */
#box.fullScreen {
  /* Your override style:
     remove offset, scale it to 1 (100%) */
  -ms-transform: translate(0px, 0px) scale(1); /* IE9 */
  transform: translate(0px, 0px) scale(1);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>click the box</div>
<div id="box">your content</div>