Created
April 3, 2015 18:23
-
-
Save thatjeannie/bd7ba7b6e11838e763d5 to your computer and use it in GitHub Desktop.
Fix element while scrolling JQUERY HTML CSS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
$(window).scroll(function(){ | |
var eOT = $('#sH').offset().top; //distance sidebar is from the top of the page | |
if($(window).width() >= 992){ | |
var yPT = $(window).scrollTop(); //distance the page has scrolled | |
//if the distance the page has scrolled is larger than the distance the sidebar is from the top, run | |
if(yPT > eOT){ | |
var yPosW = $(window).scrollTop(); //distance the page has scrolled | |
var cH = $("#cH").outerHeight(); //height of the content -page content- | |
var lH = $("#lH").outerHeight(); // height of the content -listing content- | |
var sH = $("#sH").outerHeight(); //height of the sidebar | |
var sL = (cH + lH) - sH; //height of contents minus height of sidebar. The amount left over is the distance the sidebar should be fixed for | |
var sLM = yPosW - eOT; | |
if(sL + eOT > yPosW){ | |
$("#sH").css("position","relative"); | |
$("#sH").css("top", sLM + "px"); | |
} | |
}else{ | |
$("#sH").css("position","relative"); | |
$("#sH").css("top","0px"); | |
} | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment