0

I want to fix postion of "#rightPanelscrl" when I can see the last part of it. I am using below code and it is functioning correctly in FF and not at all working in Chrome.. Can anyone me out please..!

 var sidebarScrollTop = 0;

$(window).load(function () {
    sidebarScrollTop = $("#rightPanelscrl").offset();
    if ($("#rightPanelscrl").height() > 500) {
        alert($("#rightPanelscrl").height());
        $(window).scroll(function () {

            var docScrollTop = $('body,html').scrollTop();
            //  alert(docScrollTop);
            if (docScrollTop > 500) {
                $("#rightPanelscrl").css({ position: 'fixed', top: '-500px' });
            }
            else {
                $("#rightPanelscrl").css({ position: 'static' });
            }
        });
    }
    else if ($("#rightPanelscrl").height() < 500) {
        $("#rightPanelscrl").css({ position: 'fixed', top: '35px' });
    }

});

$(window).resize(function () {
    sidebarScrollTop = $("#rightPanelscrl").offset().top;
});

$(document).resize(function () {
    sidebarScrollTop = $("#rightPanelscrl").offset().top;
});

1 Answers1

0

I prefer to do this by adding a CSS class at a certain point of scrolling rather than by using jQuery to modify the positioning. It makes the code much simpler! Check this out.

Essentially, you have your #rightPanelscrl

<ul id="rightPanelscrl">
    <li><a href="#">one</a></li>
    <li><a href="#">two</a></li>
    <li><a href="#">three</a></li>
    <li><a href="#">four</a></li>
    <li><a href="#">five</a></li>
</ul>​

your CSS:

body {height: 2000px;}    
#rightPanelscrl {
    position:absolute; 
    top:140px; 
    z-index:4; 
    width:100%;  
}    
#rightPanelscrl.fixed {
    position:fixed; 
    top:0px;
}
​

and your jQuery:

$(document).ready(function() {
    var theLoc = $('#rightPanelscrl').position().top;
    $(window).scroll(function() {
        if(theLoc >= $(window).scrollTop()) {
            if($('#rightPanelscrl').hasClass('fixed')) {
                $('#rightPanelscrl').removeClass('fixed');
            }
        } else { 
            if(!$('#rightPanelscrl').hasClass('fixed')) {
                $('#rightPanelscrl').addClass('fixed');
            }
        }
    });
});
crowjonah
  • 2,858
  • 1
  • 23
  • 27