admin管理员组文章数量:1399155
I'm trying to simulate what Yelp does with their Mo Map.
I know how to get an flip an element to fixed position once it reaches a certain screen scroll position, but how do you turn off fixed position once it hits the bottom of a relative container?
The css sticky position solves this, but since its fairly new, it doesnt have great coverage.
I'm trying to simulate what Yelp does with their Mo Map.
I know how to get an flip an element to fixed position once it reaches a certain screen scroll position, but how do you turn off fixed position once it hits the bottom of a relative container?
The css sticky position solves this, but since its fairly new, it doesnt have great coverage.
Share Improve this question edited Sep 13, 2012 at 15:39 Dan asked Sep 13, 2012 at 15:21 DanDan 3,3585 gold badges38 silver badges58 bronze badges 02 Answers
Reset to default 8You can try doing something like this: little link.
Here's a mented version of the JavaScript: (note: this uses jQuery, but it isn't necessary. If you need a plain JavaScript version I'd be glad to provide some hints)
var oritop = -100;
$(window).scroll(function() { //on scroll,
var scrollt = window.scrollY; //get the amount of scrolling
var elm = $(".box"); //get the box we want to make sticky
if(oritop < 0) {
oritop= elm.offset().top; //cache the original top offset
}
if(scrollt >= oritop) { //if you scrolled past it,
elm.css({"position": "fixed", "top": 0, "left": 0}); //make it sticky
}
else { //otherwise
elm.css("position", "static"); //reset it to default positioning
}
});
You can do this by marking the selected item.
Function that places the menu with absolute position when scrolling and marks the selected item:
jQuery(window).scroll(function () {
console.log(jQuery(window).scrollTop());
// x = jQuery("html").scrollTop();
x = jQuery(window).scrollTop(); // corrigindo bug do chome
/* Item marcado de acordo a rolagem */
switch (true) {
case (x >= 600 && x < 2500): // ajuste aqui a area
jQuery('.coluna-222-right a').removeClass('ativo');
jQuery('.coluna-222-right a.programacao').addClass('ativo');
break;
case (x >= 2500 && x < 5000):
jQuery('.coluna-222-right a').removeClass('ativo'); // ajuste aqui a area
jQuery('.coluna-222-right a.palestrantes').addClass('ativo');
break;
case (x >= 5000 && x < 5765):
jQuery('.coluna-222-right a').removeClass('ativo'); // ajuste aqui a area
jQuery('.coluna-222-right a.local').addClass('ativo');
break;
}
if (x>40) {
jQuery(".coluna-222-right ul").css("position","absolute");
jQuery(".coluna-222-right ul").css("top",x+20); // ajuste aqui a posição do menu, pode usar - ao invés de +
}
else {
jQuery(".coluna-222-right ul").css("position","static");
jQuery(".coluna-222-right ul").css("top","0");
}
});
Check the item clicked:
jQuery("a").click(function () {
jQuery("a").removeClass("ativo");
jQuery(this).addClass("ativo");
});
http://jsfiddle/67fwh/
本文标签:
版权声明:本文标题:javascript - Fixed position menu when scrolling until it hits the bottom of a relative container - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744138537a2592512.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论