简体   繁体   English

如何在div的顶部和底部获得水平滚动条?

[英]How can I get horizontal scrollbars at top and bottom of a div?

Since I'm pretty sure there is no native html or CSS way of doing this, I'm open to doing this with JavaScript. 由于我很确定没有原生的html或CSS方式来做这件事,我很乐意用JavaScript做这件事。 Obviously, I can get a scrollbar at the bottom of my div using overflow: auto in CSS. 显然,我可以在我的div底部使用overflow: auto获取一个滚动条。 Is there some JavaScript that could "clone" the scrollbar from the bottom and place it at the top of the div? 是否有一些JavaScript可以从底部“克隆”滚动条并将其放在div的顶部? Maybe there's another way? 也许有另一种方式?

You could create a new dummy element above the real one, with the same amount of content width to get an extra scrollbar, then tie the scrollbars together with onscroll events. 您可以在真实元素上方创建一个新的虚拟元素,使用相同数量的内容宽度来获得额外的滚动条,然后将滚动条与onscroll事件绑定在一起。

 function DoubleScroll(element) { var scrollbar = document.createElement('div'); scrollbar.appendChild(document.createElement('div')); scrollbar.style.overflow = 'auto'; scrollbar.style.overflowY = 'hidden'; scrollbar.firstChild.style.width = element.scrollWidth+'px'; scrollbar.firstChild.style.paddingTop = '1px'; scrollbar.firstChild.appendChild(document.createTextNode('\\xA0')); scrollbar.onscroll = function() { element.scrollLeft = scrollbar.scrollLeft; }; element.onscroll = function() { scrollbar.scrollLeft = element.scrollLeft; }; element.parentNode.insertBefore(scrollbar, element); } DoubleScroll(document.getElementById('doublescroll')); 
 #doublescroll { overflow: auto; overflow-y: hidden; } #doublescroll p { margin: 0; padding: 1em; white-space: nowrap; } 
 <div id="doublescroll"> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p> </div> 

This is a proof of concept that could be improved eg. 这是可以改进的概念证明,例如。 by polling or listening for events that might change the scrollWidth of element , for example window resizes when % lengths are in use, font size changes, or content changes driven by other scripts. 通过轮询或侦听可能更改elementscrollWidth的事件,例如,当使用%长度,字体大小更改或由其他脚本驱动的内容更改时,窗口会调整大小。 There are also (as usual) issues with IE choosing to render horizontal scrollbars inside the element, and IE7's page zooming. IE中也存在(通常)问题,选择在元素内部渲染水平滚动条,以及IE7的页面缩放。 But this is a start. 但这是一个开始。

You could use jQuery's UI Slider control . 您可以使用jQuery的UI Slider控件 You can read a tutorial for acheiving this effect online at NetTuts: http://net.tutsplus.com/tutorials/javascript-ajax/making-a-content-slider-with-jquery-ui/ 您可以在NetTuts上阅读在线获得此效果的教程: http ://net.tutsplus.com/tutorials/javascript-ajax/making-a-content-slider-with-jquery-ui/

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

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