javascript - Scrolling effect as per in Bootswatch template -


i'm trying element fixed top when user scrolls beyond element in height per "#subnav" div in page http://bootswatch.com/simplex/

looking around google, managed form following function , code.

function isscrolledintoview(elem) {     var docviewtop = $(window).scrolltop();     var docviewbottom = docviewtop + $(window).height();      var elemtop = $(elem).offset().top;     var elembottom = elemtop + $(elem).height();      if(docviewtop >= elembottom){     return true;     }else{     return false;     } }  $(window).scroll(function(){     if(!$("#search_menu").hasclass("subnav-fixed")){         if(!isscrolledintoview($("#search_menu"))){             $("#search_menu").addclass("subnav-fixed");         }            }else{          var docviewtop = $(window).scrolltop();         var docviewbottom = docviewtop + $(window).height();          $("#window_top").html(docviewtop);         $("#window_bottom").html(docviewbottom);         $("#height").html(element_frm_top);          if(docviewtop < element_frm_top < docviewbottom){             $("#search_menu").removeclass("subnav-fixed");         }     } }); 

using this. able add class subnav-fixed , div position fixed top. i'm not able return original position after scrolling up. wonder where's gone wrong or if else have better solution this.

i think should be

$(window).scroll(function(){     if(!$("#search_menu").hasclass("subnav-fixed")){         if(!isscrolledintoview($("#search_menu"))){             $("#search_menu").addclass("subnav-fixed");         }            }else{         if(isscrolledintoview($("#search_menu"))){             $("#search_menu").removeclass("subnav-fixed");         }     } }); 

did not tested.

also consider changing if(docviewtop >= elembottom) if(docviewtop >= elemtop)

edit

it depends place #search_menu. assuming top: 40px should be:

http://jsfiddle.net/mattydsw/ydpez/

function isscrolledintoview(elem) {     var docviewtop = $(window).scrolltop();     var docviewbottom = docviewtop + $(window).height();      var elemtop = $(elem).offset().top - 40;     var elembottom = elemtop + $(elem).height();      if (docviewtop >= elemtop) {         return true;     } else {         return false;     } }  $(window).scroll(function () {     var $body = $('body'),         $menu = $("#search_menu");     if (!$menu.hasclass("subnav-fixed")) {         if (isscrolledintoview($menu)) {             $menu.addclass("subnav-fixed");         }     } else {         if ($(window).scrolltop() <= 40) {             $menu.removeclass("subnav-fixed");         }     } }); 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -