jquery - Scroll input element to top in a div -


i'm struggled move input element on top in scrollable div.

i want move input element top on focusing input element, means after div#up (the initial position of input 1). div should scroll down , move input element top position.

i tried below code, not works properly,

$('input').on('focus', function() {      console.log($(this).offset());      console.log($(this).position());      $('input').removeattr('style');      var ot = parseint($(this).offset().top),          pt = parseint($(this).position().top);      var st = ot - 108; //108 height of div      if (st - pt > 0) {          st = st - pt;      }      if (st < 0) {          st = ot - 108      }      console.log(st, ot, pt);      $('#container').animate({          scrolltop: st      }, 200); }); 

and make online demo same.

you need take account current scroll position.

$('#container').animate({      scrolltop: st+$('#container').scrolltop() },200); 

fixed fiddle


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -