我正在使用“粘性页脚”技术在页面上的一个区域,其中也有一个导航栏利用词缀。问题是,数据偏移量属性需要根据粘性页脚的位置进行更新-因此它不能被硬编码。
如何获取粘性页脚所在位置的像素值,并将该值传递给data-offset属性,以便它知道何时附加自身?
任何帮助都将不胜感激!
发布于 2012-10-26 17:49:49
我还修复了导航栏的位置,不能硬编码到css。这不是因为一个粘性的页脚,而是导航栏上方的空格(当没有固定时)是动态的,但我认为解决方案是相似的。
我使用JavaScript通过动态地设置/取消设置适当的类来修复/取消修复,这取决于某些DOM元素是否在视区中可见。该计划的内容如下:
position: absolute样式,包含导航栏的空间具有导航栏的静态高度,因此固定时不会更改下方内容的位置,如果上面的元素不可见,Opa framework中的代码(转换为JS+jQuery应该很简单,因为Opa的DOM库只是简单地绑定到jQuery):
// id of the element above the navbar, and the navbar
logobar_id = "logo-bar";
navbar_id = "main-menu";
// hardcoded height of the navbar
navbar_height_px = 30;
client function distance() {
dom = #{logobar_id};
// hardcoded height of the navbar
win = Dom.select_window();
// position of the top of the viewport
scroll_visible = Dom.get_scroll_top(win);
// return the distance between of bottom of element above the navbar and the top of
dom_bottom = Dom.get_offset(dom).y_px + Dom.get_height(dom);
dom_bottom - scroll_visible;
}
dom = #{navbar_id};
private client function fixation() {
if (distance() <= 0) {
// TODO: remember if subnav is fixed, dont fix if fixed
Dom.add_class(dom, "navbar-fixed-top");
Dom.remove_class(dom, "container");
} else {
// TODO: remember if subnav is fixed, dont unfix if unfixed
Dom.remove_class(dom, "navbar-fixed-top");
Dom.add_class(dom, "container");
void;
}
}
// (un)fix when scroll
private client function onscroll(_) {
fixation();
}
// bind the `onscroll` handler for subnav when it is loaded
private client function onready(_) {
_ = Dom.bind(Dom.select_window(), {scroll}, onscroll);
fixation();
}导航栏和导航栏本身上面的DOM元素:
<div class="container" id=#{logobar_id}>
My logo with dynamic content
</div>
<div class="container" style="height: {navbar_height_px}px; position: relative; top: 0px">
<div style="position: absolute; top: 0px">
<div class="navbar container" id=#{navbar_id} onready={onready}>
...
</div>
</div>
</div>https://stackoverflow.com/questions/13079529
复制相似问题