[英]Trying to have a left and right *responsive* sidebar with scrollable content in the middle, where the scroll bar is on the right
I am having a tough time accomplishing something I see done on both Twitter and Instagram's desktop versions.我很难完成我在 Twitter 和 Instagram 桌面版本上看到的事情。
What I want is a responsive sidebar on both the left and right sides of the page.我想要的是页面左右两侧的响应式侧边栏。 In the middle is a 600 px width div with the page's content.
中间是一个 600 px 宽度的 div,里面有页面的内容。 The scroll bar must be on the right hand side of the page, not the right hand side of the content div.
滚动条必须位于页面的右侧,而不是内容 div 的右侧。
Let me show you some things that aren't quite there which I found over the past two days.让我向您展示我在过去两天中发现的一些不完全存在的东西。
A Flexbox layout with fixed sidebars .带有固定侧边栏的 Flexbox 布局。 There are three problems here: (1) the scroll bar is on the right side of the content div.
这里存在三个问题:(1)滚动条在内容div的右侧。 I want it on the right hand side of the browser .
我希望它在浏览器的右侧。 (2) The content in the middle isn't set to 600px width.
(2) 中间的内容没有设置为 600px 宽度。 (3) I suspect that if its width was 600px, the surrounding sidebars would not responsively adjust to the browser being resized.
(3) 我怀疑如果它的宽度是 600px,周围的侧边栏将不会响应调整大小的浏览器。
This jsFiddle which is very close .这个 jsFiddle 非常接近。 The problem here is that the code isn't responsive.
这里的问题是代码没有响应。 Because the sidebars are removed from the document flow by their
fixed
position, their width cannot auto-adjust to fill the space around the 600px
width content div when I write it that way.因为侧边栏是通过它们的
fixed
位置从文档流中移除的,所以当我这样写时,它们的宽度无法自动调整以填充600px
宽度的内容 div 周围的空间。 Instead, I get white space on either side.相反,我在两边都有空白。
This would be what I want if the sidebar divs followed you down the page when you scroll.如果侧边栏 div 在您滚动时跟随您向下滚动页面,这将是我想要的。
Summary of requirements:要求总结:
Two responsive sidebars, fixed to the left and right sides of the page, which expand and contract to fill the space around the 600px center content div.两个响应式侧边栏,固定在页面的左右两侧,可扩展和收缩以填充 600 像素中心内容 div 周围的空间。
A div in the center containing content which is scrollable;中心的 div 包含可滚动的内容; however the scroll bar is not on the side of the div (I can do that, it's easy).
但是滚动条不在 div 的一侧(我可以做到,这很容易)。 Rather it is on the side of the page like Twitter and Instagram's.
相反,它像 Twitter 和 Instagram 一样位于页面的一侧。
I've been at this for about 16 hours across two days.我已经在这两天里工作了大约 16 个小时。 It seems apparent that I cannot use
position: fixed
or sticky
as the sidebars must be responsive like Twitter's.很明显我不能使用
position: fixed
或sticky
因为侧边栏必须像 Twitter 一样响应。 Said differently, the content div in the middle takes 600px of the width, and the sidebars responsively adjust to fill the remaining width.换句话说,中间的内容 div 占用 600px 的宽度,侧边栏响应调整以填充剩余的宽度。 But without
position: fixed
there is no way to make the sidebars stay where they are...?但是没有
position: fixed
就没有办法让侧边栏停留在它们原来的位置......?
My best guess would be for you to use position:absolute;
我最好的猜测是你使用
position:absolute;
for your side bars, and use left:0; top:0;
对于你的侧边栏,使用
left:0; top:0;
left:0; top:0;
for the left bar and right:0; top:0;
对于左栏和
right:0; top:0;
right:0; top:0;
for the right bar.对于正确的酒吧。
As for the responsiveness, maybe try using relative units for the width such as vw, or vh.至于响应性,可以尝试使用相对单位的宽度,例如 vw 或 vh。 (For eg,
width:20vw;
(which would be 20% of screen width) ). (例如,
width:20vw;
(这将是屏幕宽度的 20%))。 I believe this will help your content wrap and change according to the size of the screen.我相信这将有助于您的内容根据屏幕大小进行换行和更改。
Also, try putting overflow:hidden;
另外,尝试放置
overflow:hidden;
for the middle div CSS, and overflow-y:auto;
对于中间的 div CSS,以及
overflow-y:auto;
for the body CSS.用于身体 CSS。
:-) :-)
I got it!我知道了!
The solution came to me when I read this question with an answer that mentions width: calc()
.当我阅读这个问题的答案提到
width: calc()
时,我想到了解决方案。
Here is my code, roughly:这是我的代码,大致如下:
<article>
<aside className="general-sidebar aside1 w-calc">foo
</aside>
<div className="center-page">bar</div>
<aside className="general-sidebar aside2 w-calc">baz
</aside>
</article>
.aside1 {
left: 0;
}
.aside2 {
right: 0;
}
.general-sidebar {
position: fixed;
top: 0;
bottom: 0;
}
.w-calc {
width: calc((100% - 600px) / 2) !important;
// border: 2px solid white;
}
So basically the sidebars are in fixed position on the left and right side, removed from document flow... to remedy what this does to the responsive width I was achieving using Flexbox, I inserted the width: calc()
line.所以基本上侧边栏位于左侧和右侧的固定位置,从文档流中删除......为了弥补这对我使用 Flexbox 实现的响应宽度的影响,我插入了
width: calc()
行。 Both sidebars receive this width property.两个侧边栏都接收此宽度属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.