元素的页面布局,其中包含页面上的重要内容。设计的重要部分是:始终滚动 div 元素而不是页面本身
我有一个带有内部
#content {
height: 300px;
width: 500px;
overflow: scroll;
}
现在当包含的文本大于 300px 时,我需要能够滚动它。即使鼠标没有悬停在元素上(箭头键也应该起作用),是否可以滚动
?请注意,我不想禁用“全局”滚动:页面上应该有两个滚动条,全局滚动条和
滚动条。唯一改变的是内部
应始终滚动,除非它无法再移动(在这种情况下页面应开始滚动)。这有可能以某种方式实现吗?
编辑
我认为这个问题有点令人困惑,所以我将附加一系列我希望它如何工作的序列。 (Khez 已经提供了概念验证。)
第一个图像是页面打开时的外观。
现在,鼠标位于指示的位置并滚动,应该发生的是,
- 首先内部 div 滚动其内容(图 2)
- 内部 div 已完成滚动(图 3)
- body 元素滚动,以便 div 本身得到感动了。 (图4)
希望现在更清楚了。
(图片感谢 gomockingbird.com)
I have a page layout with an inner <div id="content">
element which contains the important stuff on the page. The important part about the design is:
#content {
height: 300px;
width: 500px;
overflow: scroll;
}
Now when the containing text is larger than 300px, I need to be able to scroll it. Is it possible to scroll the <div>
, even when the mouse is not hovering the element (arrow keys should also work)?
Note that I don’t want to disable the ‘global’ scrolling: There should be two scrollbars on the page, the global scrollbar and the scrollbar for the <div>
.
The only thing that changes is that the inner <div>
should always scroll unless it can’t be moved anymore (in which case the page should start scrolling).
Is this possible to achieve somehow?
Edit
I think the problem was a bit confusing, so I’ll append a sequence of how I would like it to work. (Khez already supplied a proof-of-concept.)
The first image is how the page looks when opened.
Now, the mouse sits in the indicated position and scrolls and what should happen is that
- First the inner div scrolls its content (Fig. 2)
- The inner div has finished scrolling (Fig. 3)
- The body element scrolls so that the div itself gets moved. (Fig. 4)
Hope it is a bit clearer now.
(Image thanks to gomockingbird.com)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为如果不编写脚本就不可能实现这一目标,考虑到滚动元素的众多事件(单击、滚轮、向下箭头、空格键),这可能会很混乱。
I don't think that is possible to achieve without scripting it, which could be messy, considering the numerous events which scroll an element (click, scrollwheel, down arrow, space bar).
一个选项是使用 jQuery 滚动插件。我知道它可以在 div 上创建滚动条。您唯一需要自己添加的是按下键盘按钮时捕获事件的逻辑。只需检查箭头键的键码并使 div 向下滚动即可。
该插件可以在此处找到。
你可以这样使用它;
An option could be using the jQuery scroll plugin. I know it has the availability to create scrollbars on an div. The only thing you need to add yourself is the logic to catch the events when keyboard buttons are pressed. Just check out the keycodes for the arrow keys and make the div scroll down.
The plugin can be found here.
You can use it like this;
这是一个可能有效的解决方案:(小提琴:http://jsfiddle.net/maniator/9sb2a/)
虽然有点问题,但这是一个开始:-)
here is a solution that might work: (fiddle: http://jsfiddle.net/maniator/9sb2a/)
It is a bit buggy but it is a start :-)
我相信实现这一目标的唯一方法是使用框架。
框架 - W3Schools 参考
The only way I believe you can achieve this is through the use of frames.
Frames - W3Schools Reference
如果您只想有一个固定定位的“div”并仅滚动它,也许您可以使用如下技巧:
http:// /jsfiddle.net/3cpvT/
使用鼠标滚轮和各种按键滚动可以按预期工作。唯一的问题是滚动条仅位于文档正文上。
If you just want to have a fixed positioned "div" and scroll only it, maybe you could use a trick like:
http://jsfiddle.net/3cpvT/
Scrolling with mouse wheel and all kinds of keys works as expected. Only thing is that the scrollbar is on the document body only.
我找到了一个解决方案...并不完美... http://jsfiddle.net/fGjUD/6/。
CSS:
JS(jQuery):
将整个内容放在固定宽度的div中可以解决文本换行问题。 IE 浏览器还有另一个错误。如果页面具有居中对齐的背景,它将在 #scrolldiv 上的 mouseenter 上左右移动
I found a solution... Not perfect... http://jsfiddle.net/fGjUD/6/.
CSS:
JS (jQuery):
The text wrapping problem can be solved putting the whole content in fixed-width div. There is another bug for IE browser. If the page has center-aligned backgrond, it will move left-right on mouseenter on #scrolldiv