当从右侧滑动div时,如何将div放在剩余空间中

时间:2017-09-10 17:21:06

标签: css

我有一个显示图像的页面(现在只是一个绿色div占位符)。我想从右侧滑动一个div,这将是一个注释框,并将图像保持在剩余空间的中心位置。如果图像对于剩余空间来说太大,我想让用户水平滚动图像。

我有这个:

var showingComments = false;

$("#clickme").click(function() {
  if (showingComments == false) {
    showingComments = true;
    $(this).parent().animate({
      right: '0px'
    }, {
      queue: false,
      duration: 500
    });
  } else {
    showingComments = false;
    $(this).parent().animate({
      right: '-150px'
    }, {
      queue: false,
      duration: 500
    });
  }
});
#Picture_container {
  width: auto;
  background-color: yellow;
  overflow-x: auto;
}

#thePicture {
  height: 300px;
  width: 400px;
  /* this will change when the page loads */
  background-color: green;
  left: 0px;
  right: 0px;
  margin-left: auto;
  margin-right: auto;
}

#commentsBox {
  background: #00FFFF;
  position: absolute;
  width: 150px;
  height: 400px;
  right: -150px;
  top: 10px;
}

#clickme {
  position: absolute;
  top: 0;
  left: 0;
  height: 20px;
  width: 400px;
  background: #5F9EA0;
  text-align: center;
  transform: rotate(90deg);
  transform-origin: left top 0;
}

#comments {
  float: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='Picture_container'>
  picture container
  <div id='thePicture'>
    the picture goes here
  </div>
</div>

<div id="commentsBox">
  <div id="comments">
    I am a sticky-out comments box
  </div>
  <div id="clickme">
    click me!
  </div>
</div>

示例:https://jsfiddle.net/fwwfe2q6/

正如您所见,注释框弹出,但它与图像持有者div重叠,而不是向左移动。您是否可以建议如何在注释框的展开和折叠状态中居中绿色div(表示图像),并且当绿色div太大而无法适应窗口以及展开的注释框时,是否可以将绿色div水平滚动?

1 个答案:

答案 0 :(得分:0)

您可以使用flex和rethink结构:

&#13;
&#13;
"SHA1PRNG"
&#13;
body {margin:0;}
#Picture_container {
  display: flex;
  flex-wrap: wrap;
  height: 100vh;
  max-width: 700px;
  transition: 0.5s;
  margin: auto;
  background: yellow;
  text-align: center;
}

#thePicture {
  display: flex;
  flex-wrap: wrap;
  flex: 1;
  overflow: auto;
}

#thePicture img {
  flex-shrink: 0;
  margin: auto;
  max-height: 75vh;
}

p, [for="mw100"] {
  width: 100%;
  }
  p {
  background: turquoise;
  padding: 1em;
  margin: 0;
}

#commentsBox {
  display: flex;
  background: tomato;
  overflow: hidden;
}

#test,
#full,
#mw100 {
  position: absolute;
  right: 100%;
}

#commentsBox label {
  display: block;
  background: tomato;
  width: 2em;
  margin: 1em 0 0 0em;
  white-space: nowrap;
  transform: rotate(90deg);
  cursor: pointer;
}

#commentsBox label:before {
  content: '';
  position: absolute;
  width: 2em;
  height: 100vh;
  display: block;
}

#clickme {
  background: purple;
  width: 200px;
  margin-right: -200px;
  transition: 0.1s;
}

label {
  font-size: 1.2em;
  font-weight: bold;
  color: white;
  text-shadow: 0 0 2px black;
}

#test:checked~#clickme {
  margin-right: 0;
}

#full:checked~#Picture_container {
  max-width: 100%;
}

#mw100:checked~img {
  max-width: 95%;
}
&#13;
&#13;
&#13;

Snippet根据预期结果提出一些选项。

  • 容器上的全宽或最大宽度

  • 容器图像溢出或图像为max-width

标签和投影仅用于演示,使用js作为滑动框。

关于flex的教程/提醒我觉得有用:https://css-tricks.com/snippets/css/a-guide-to-flexbox/