gpt4 book ai didi

css - 从右侧滑入div时如何将div居中放置在剩余空间中

转载 作者:行者123 更新时间:2023-11-28 03:02:04 25 4
gpt4 key购买 nike

我有一个将显示图像的页面(目前只是一个绿色的 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 水平滚动?

最佳答案

你可以使用 flex 并重新思考结构:

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%;
}
<input type="checkbox" id="full" />
<div id='Picture_container'>
<p>picture container <label for="full">Toggle me full or 700px width</label></p>
<div id='thePicture'>
<input id="mw100" type="checkbox" /><label for="mw100">toggle image overflow</label>
<img src="http://dummyimage.com/1800x1000/00ff00&text=the picture goes here" />
</div>

<div id="commentsBox">
<input type="checkbox" id="test" />
<div id="comments">
<label for="test">To show comments- click me!</label>
</div>

<div id="clickme">
Shown comments here.
</div>
</div>

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

  • 容器的全宽或最大宽度

  • 容器容纳图像溢出或图像宽度最大

labels 和 imputs 仅用于演示,滑动框使用 js。

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

关于css - 从右侧滑入div时如何将div居中放置在剩余空间中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46143477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com