CSS - 创建控制单独 DIV 的滚动条

发布于 2024-08-16 04:30:25 字数 300 浏览 3 评论 0原文

我需要创建一个水平滚动条来控制水平 DIV 内的滚动。

我无法使用常规的 OVERFLOW:AUTO CSS,因为滚动条在视觉上与 div 是分开的。滚动条和 DIV 之间有一些(静态)项目。

我想对这些项目使用 POSITION:FIXED ,并将它们保留在滚动 DIV 中。但看起来FIXED位置仅相对于窗口而不是当前容器。

我知道我可以编写一些 jQuery 脚本来处理滚动事件并移动 DIV 中的内容。 有什么方法可以使用 CSS 或现有的 jQuery 插件来实现这一点吗?

I need to create a horizontal scrollbar that will control a the scrolling inside an horizontal DIV.

I can't use the regular OVERFLOW:AUTO CSS since the scrollbar is visually separate from the div. There are some (static) items between the scroll and the DIV.

I thought of using POSITION:FIXED for those items, and keep them inside the scrolling DIV. But it looks like FIXED position is only relative to the window and not the current container.

I know I can probably write some jQuery script to handle the scroll events and move the content in the DIV.
Any way to achieve this with CSS or existing jQuery plugin?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木森分化 2024-08-23 04:30:25

以下是我解决这个问题的方法:

创建一个新的 div 和其中的一个 div,如下所示(这些与实际可见内容分开):

<div id="scrollcontainer"><div id="containercontent"> </div></div>

现在将“scrollcontainer”div 的高度设置为仅与滚动条(17 像素)。

然后设置“scrollcontainer”div 的宽度以匹配可见容器。

然后设置“containercontent”div 的宽度以匹配实际内容的宽度。

然后将可见容器设置为溢出:隐藏

然后处理滚动容器的onscroll事件,如下所示:

var newScroll = scrollcontainer.scrollLeft;
scrolledcontainer.scrollLeft = newScroll;

Here is how I solved this problem:

Create a new div, and a div inside of it, like this (These are separate from your actual visible content):

<div id="scrollcontainer"><div id="containercontent"> </div></div>

Now set the height of the 'scrollcontainer' div to be only as tall as a scrollbar (17px).

Then set the width of the 'scrollcontainer' div to match your the visible container.

Then set the width of the 'containercontent' div to match that of your actual content.

Then set your visible container to overflow: hidden

Then handle the onscroll event of the scrollcontainer, like this:

var newScroll = scrollcontainer.scrollLeft;
scrolledcontainer.scrollLeft = newScroll;
ぶ宁プ宁ぶ 2024-08-23 04:30:25

您可以在固定内容上使用 position:absolute 以获得所需的效果。

You can use position:absolute on the fixed contents for the desired effect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文