简体   繁体   English

试图有一个左右*响应*侧边栏,中间有可滚动的内容,滚动条在右侧

[英]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.让我向您展示我在过去两天中发现的一些不完全存在的东西。

  1. 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,周围的侧边栏将不会响应调整大小的浏览器。

  2. 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: fixedsticky因为侧边栏必须像 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.

相关问题 制作一个响应式导航栏,其内容在左,中和右 - Making a Responsive Navigation Bar with content on the left, middle and right 左侧带有粘性侧边栏,右侧带有可滚动内容的布局 - Layout with sticky sidebar on the left and scrollable content on the right 左侧的可滚动内容 - 右侧的粘性图像 - 滚动上的活动项目 - Scrollable content on left - sticky image on right - with active item on scroll boostrap左侧边栏,内容右侧 - boostrap left sidebar with content right 左,右和中间内容位置 - left, right, and middle content position 主要内容后如何有左右侧边栏? - How to have a left and right sidebar after main content? 如何在中间元素滚动时保持左右 flex 元素(侧边栏)固定,滚动条在右侧? - How to keep the left and right flex elements (sidebars) fixed while the middle element scrolls, with the scroll bar on the right? 如何修复左右侧边栏并保持中心主要内容可滚动向上? - How to fix left and right sidebar and remains centre main content scrollable upside? 带有Bootstrap的右侧内容栏的左侧边栏 - Left sidebar with the content div on right with Bootstrap 如何使y可滚动DIV的滚动条显示在右侧而不是左侧 - how to make a scroll-bar of a y-scrollable DIV appears on the right instead of left
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM