Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll position lost when hiding div

Tags:

jquery

http://jsfiddle.net/cbp4N/16/

If you show the div. Change the scroll position and then hide and show it agian the scroll position is lost.

am I doing anything wrong or is this a bug. Is there a way round it with som plugins.

/Anders

Thanks for the answers and solutions. But what if the div that I hide is a outer div and the scrolling div is deep inside the div I hide. Is there a smart way to also fix this. Becuse now I cant set/save the scroll position in the callback of the hide/show

like image 954
user1199595 Avatar asked Jun 11 '12 14:06

user1199595


2 Answers

Jquery's .scrollTop() works well if you maintain the position as data.

$('#cbxShowHide').click(function(){
    if(this.checked) {
        $('#block').show('fast',function() {
            $(this).scrollTop($(this).data('scroll'));
        });
    }
    else {
       $('#block').data('scroll',$('#block').scrollTop());
        $('#block').hide('fast');
    }
});

example

like image 68
AbstractChaos Avatar answered Nov 19 '22 00:11

AbstractChaos


This is normal behaviour because the element is set to the least possible variables to memory when you hide it. If you want to remember scroll position you'll have to store those yourself and then apply the scroll position on showing it.

Scroll Position of div with "overflow: auto"

like image 1
Tschallacka Avatar answered Nov 18 '22 22:11

Tschallacka