最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Fixed position menu when scrolling until it hits the bottom of a relative container - Stack Overflow

programmeradmin2浏览0评论

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 0
Add a ment  | 

2 Answers 2

Reset to default 8

You 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/

发布评论

评论列表(0)

  1. 暂无评论