简体   繁体   English

下溢滚动div

[英]Overflow scrolling div underneath

I'm working with a div sitting above another div. 我正在与另一个div上方的div一起工作。 When I scroll the div that sits above the other and reach the bottom, it scrolls the entire body. 当我滚动位于另一个上方并到达底部的div时,它将滚动整个主体。 This is hard to explain so I made a fiddle to demonstrate the effect: http://jsfiddle.net/2Ydr4/ 这很难解释,所以我做了一个小提琴来演示效果: http : //jsfiddle.net/2Ydr4/

HTML: HTML:

<div class="body">
    <div class="above">
       <!-- Content in here that is longer than the height -->
    </div>
</div>

CSS: CSS:

.above {
    width: 300px;
    height: 200px;
    overflow-y: scroll;
    background: red;
    z-index: 10;
    position: fixed;
}

My question is: how do I prevent the body scrolling when the floating div is scrolled to the bottom or top? 我的问题是:当浮动div滚动到底部或顶部时,如何防止主体滚动? I'm sure it's something really obvious that I'm missing. 我敢肯定,我确实很想念我。 Thanks! 谢谢!

EDIT: I should probably have mentioned that the target device for this was an iPad. 编辑:我可能应该提到目标设备是iPad。 I've tried adding body {overflow: hidden} conditionally as suggested below, and while this solves the problem on a desktop browser, it still seems to persist on a touch-based browser. 我尝试按照下面的建议有条件地添加body {overflow: hidden} ,尽管这在桌面浏览器上解决了问题,但在触摸式浏览器上似乎仍然可以解决。

Desktop 桌面

That's how scrolling works. 滚动就是这样。 What you will need to do is remove the body's scroll property temporarily or by the users action. 您需要做的是暂时或通过用户操作删除正文的scroll属性。

For example you could disable the body's scroll when the user hovers over the floating div by using... 例如,当用户将鼠标悬停在浮动div上时,可以使用以下命令禁用主体的滚动:

body{overflow:hidden}

and then re-enable it when you hover off the floating div by using.. 然后在使用悬停浮动div时重新启用它。

body{overflow:auto}

Mobile 移动

On mobile devices you will need to use touch events. 在移动设备上,您将需要使用触摸事件。 I haven't tried this but in theory this should work. 我没有尝试过,但理论上应该可以。

var $body = document.querySelector('.body'),
    $above = document.querySelector('.above');

$above.addEventListener('ontouchstart', onAboveStart, false);
$body.addEventListener('ontouchstart', onBodyStart, false);


function onAboveStart()
{
        $body.addEventListener('ontouchmove', function(e) {e.preventDefault()}, false);
}

function onBodyStart()
{
        $body.removeEventListener('ontouchmove', function(e) {e.preventDefault()});
}

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

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