简体   繁体   English

始终在特定的div上滚动

[英]Always scroll on particular div

Let's say I have a site with a central div, approximately 50% of the width of the window, with other divs either side of it filling up the remaining space. 假设我有一个中心div的站点,大约是窗口宽度的50%,其他div的任一侧都占满了剩余的空间。 The spanning divs are fixed, and don't move, nor can they scroll. 跨度div是固定的,不会移动,也不会滚动。

At the moment, when my mouse is over one of the spanning divs, I (naturally) can't scroll the central div. 目前,当我的鼠标悬停在一个跨度div上时,我(自然)无法滚动中央div。 My question is this: is there a way to ALWAYS have scroll focus on a particular div, no matter where the mouse is located on the page? 我的问题是这样的:无论鼠标位于页面的何处,是否都可以始终将滚动焦点放在特定的div上?

EDIT: What I actually have is this: 编辑:我实际上是这样的:

<div id='wrapper'>
    <nav id='sidebar'></nav>
    <div id='rhs'></div>
</div>

where wrapper and sidebar both have position fixed, and sidebar and rhs are adjacent in the center of wrapper (ie margin: 0 auto; to sit them in the middle). 包装器和侧边栏的位置都固定,并且侧边栏和rhs在包装器的中心相邻(即margin:0 auto;将它们放置在中间)。 Scrolling with my mouse over either wrapper or sidebar does not scroll rhs, despite the positions being fixed (so Toni Leigh's answer doesn't work for me here). 尽管位置固定,但用鼠标在包装纸或侧边栏上滚动都不会滚动rhs(因此Toni Leigh的答案在这里对我不起作用)。

Yes, you can do this using position: fixed; 是的,您可以使用position: fixed;

The two outer divs are fixed to the screen regardless of scroll position. 无论滚动位置如何,两个外部div都固定在屏幕上。 The the central div scrolls regardless of where the mouse pointer is. 无论鼠标指针在哪里,中央div都会滚动。 You use top and bottom to fix the full height of the screen, then left and right to fix each on either side. 您可以使用topbottom固定在屏幕的整个高度,然后leftright固定在两边各。

You can still interact with content in the fixed outer divs. 您仍然可以与固定外部div中的内容进行交互。

Please see this example 请看这个例子

Something like this? 像这样吗 Demo 演示

You set the two side divs to be have a position: fixed property and by using top: 0 , left: 0 and right: 0 you can move these into position to the top left and top right respectively. 您将两个div设置为具有position: fixed属性,并通过使用top: 0left: 0right: 0将它们移入位置到左上角和右上角。

Then you can have a regular element in the middle. 然后,您可以在中间放置一个常规元素。 The scroll will now always affect the non-fixed element. 现在,滚动将始终影响非固定元素。 (I added a background picture so you can see they don't scroll). (我添加了背景图片,因此您可以看到它们不滚动)。

HTML HTML

<div class="fixed left"></div>
<div class="center"></div>
<div class="fixed right"></div>

CSS CSS

.fixed {
    width: 25%;
    height: 100%;
    background-image: url('http://www.6wind.com/blog/wp-content/uploads/2014/04/Vertical-White-car-Banner.jpg');
    position: fixed;
    top: 0;
}

.left {
    left: 0;
}

.right {
    right: 0;
}

.center {
    position: relative;
    margin: 0 auto;
    width: 50%;
    height: 5000px;
    background: red;
    line-height: 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM