简体   繁体   English

使用窗口滚动条控制溢出的div的滚动-Javascript

[英]Using window scrollbars to control scroll of overflowed div - Javascript

I have a div with an overflow-x:auto which contains an element which is too wide for the div so it scrolls. 我有一个带有overflow-x:auto的div,其中包含一个元素,该元素对于div而言太宽,因此无法滚动。 The problem being the element is too tall and it's rather cumbersome to use the element's scrollbars to view its content. 问题在于元素太高,使用元素的滚动条查看其内容相当麻烦。

What would be nicer is to use the window scrollbars to control the scrolling of the element as their position is fixed on the edge of the window. 更好的方法是使用窗口滚动条控制元素的滚动,因为它们的位置固定在窗口的边缘。

Is that possible at all? 那有可能吗? I can't seem to get my head around a good approach of doing so. 我似乎无法全神贯注地这样做。

Example of the problem below. 下面的问题示例。 Note this wrapper element has to be capable of working within the flow of an existing page layout. 注意,该包装器元素必须能够在现有页面布局的流程内工作。

https://jsfiddle.net/3y1549jk/2/ https://jsfiddle.net/3y1549jk/2/

 .wrapper { position: absolute; width: 75%; left: 12.5%; right: 12.5%; height: 200%; top: 12.5%; bottom: 12.5%; background-color: red; overflow-x: auto; } .content { position: absolute; width: 150%; height: 75%; margin: 7.5%; background-color: blue; color: white; padding: 7.5%; } 
 <!-- I would like the window to control the horizontal scrolling of the .wrapper div --> <div class="wrapper"> <div class="content"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse at libero imperdiet justo finibus vehicula. Integer ac risus quis lectus pretium condimentum in sit amet mauris. Maecenas et sagittis justo. Cras diam urna, placerat aliquet metus non, interdum cursus nisl. Quisque cursus elit feugiat, tempus diam in, faucibus felis. </div> </div> 

Use position fixed instead of absolute .. 使用fixed位置代替absolute ..

 .wrapper { position: absolute; width: 75%; left: 12.5%; right: 12.5%; height: 200%; top: 12.5%; bottom: 12.5%; background-color: red; overflow-x: auto; } .content { position: fixed; width: 150%; height: 75%; margin: 7.5%; background-color: blue; color: white; padding: 7.5%; } 
 <!-- I would like the window to control the horizontal scrolling of the .wrapper div --> <div class="wrapper"> <div class="content"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse at libero imperdiet justo finibus vehicula. Integer ac risus quis lectus pretium condimentum in sit amet mauris. Maecenas et sagittis justo. Cras diam urna, placerat aliquet metus non, interdum cursus nisl. Quisque cursus elit feugiat, tempus diam in, faucibus felis. </div> </div> 

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

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