如何让固定位置的 div 随内容水平滚动?使用 jQuery
我有一个带有以下 CSS 的 div.scroll_fixed
.scroll_fixed {
position:absolute
top:210px
}
.scroll_fixed.fixed {
position:fixed;
top:0;
}
当 div 到达页面顶部时,我使用以下 jQuery 代码来设置 .fixed 类。
var top = $('.scroll_fixed').offset().top - parseFloat($('.scroll_fixed').css('margin-top').replace(/auto/, 0));
$(window).scroll(function (event) {
// what the y position of the scroll is
var y = $(this).scrollTop();
// whether that's below the form
if (y >= top) {
// if so, ad the fixed class
$('.scroll_fixed').addClass('fixed');
} else {
// otherwise remove it
$('.scroll_fixed').removeClass('fixed');
}
});
这对于垂直滚动固定非常有用。但对于较小的浏览器窗口,水平滚动会导致与该固定 div 右侧的内容发生冲突。
我希望 div 能够水平滚动内容。
谁能指出我正确的方向。我仍在接触 JS/JQuery。
我基本上希望它像这个 示例中的第二个框一样工作< /a>.
I have a div.scroll_fixed with the following CSS
.scroll_fixed {
position:absolute
top:210px
}
.scroll_fixed.fixed {
position:fixed;
top:0;
}
I'm using the following jQuery code to set the .fixed class when the div reaches the top of the page.
var top = $('.scroll_fixed').offset().top - parseFloat($('.scroll_fixed').css('margin-top').replace(/auto/, 0));
$(window).scroll(function (event) {
// what the y position of the scroll is
var y = $(this).scrollTop();
// whether that's below the form
if (y >= top) {
// if so, ad the fixed class
$('.scroll_fixed').addClass('fixed');
} else {
// otherwise remove it
$('.scroll_fixed').removeClass('fixed');
}
});
This works great for the vertical scroll fixing. But with a small browser window, horizontal scrolling causes a clash with content to the right of this fixed div.
I would like the div to scroll with the content horizontally.
Could anyone point me in the right direction. Still getting my feet wet with JS/JQuery.
I basically want it to work like the second box in this example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该演示保持元素的
position:fixed
并操作元素的left
属性:使用
leftInit
会考虑可能的左边距。在这里尝试一下:http://jsfiddle.net/F7Bme/The demo is keeping the element's
position:fixed
and manipulating theleft
property of the element:Using
leftInit
takes a possible left margin into account. Try it out here: http://jsfiddle.net/F7Bme/您现在可能已经继续前进,但对于其他正在寻找水平可滚动固定元素解决方案的人来说,这是一个答案。这个 jquery 插件就是为了解决这个问题而创建的。
该演示使用购物车摘要,当固定在页面顶部时,该摘要仍会水平滚动;而且,我还将它用于表格数据上方的标题:
演示:http://jsfiddle.net/y3qV5/434/(更新)
插件和来源:https://github.com/bigspotteddog/ScrollToFixed
用法:
You have probably moved on by now, but here is an answer for anyone else looking for a horizontally scrollable fixed element solution. This jquery plugin was created to solve this exact problem.
This demo uses a shopping cart summary that will still scroll horizontally when fixed to the top of the page; and, I have also used it for a header above tabular data:
Demo: http://jsfiddle.net/y3qV5/434/ (updated)
Plugin and source: https://github.com/bigspotteddog/ScrollToFixed
Usage:
使用 css 属性
position:sticky
来获取它。这是文章解释和现场演示。
http://updates.html5rocks.com /2012/08/Stick-your-landings-position-sticky-lands-in-WebKit
唯一的缺点是浏览器兼容性。
use css property
position:sticky
to get it.Here is the article explained and live demo.
http://updates.html5rocks.com/2012/08/Stick-your-landings-position-sticky-lands-in-WebKit
the only drawback is browser compatibility.