I need to keep an element fixed when scrolling on a page without setting specific height values. Do you know of any way to achieve this?
Below is the code for reference:
// Keep the orange box at the top after scrolling to a certain extent
$(window).scroll(function(e){
var scrollTop = $(window).scrollTop();
// Limit based on current content length
if ($(this).scrollTop() >= ($('.mainContent2ColLayout250').height() - 400))
scrollTop = ($('.mainContent2ColLayout250').height() - 400);
if ($(this).scrollTop() >= 340)
$('.fixedElement').css({'marginTop': scrollTop - 325, 'marginBottom': -1 * (scrollTop - 325)});
if ($(this).scrollTop() < 340)
$('.fixedElement').css({'marginTop': 0, 'marginBottom': 0});
});
Any assistance would be greatly appreciated.
Thank you!