Skip to content

Instantly share code, notes, and snippets.

@davidanton1d
Created October 30, 2018 10:38
Show Gist options
  • Save davidanton1d/df2142b1b58e2e62f132e2ea48f852bd to your computer and use it in GitHub Desktop.
Save davidanton1d/df2142b1b58e2e62f132e2ea48f852bd to your computer and use it in GitHub Desktop.
Move a div to "above the fold" if not visible at load. Div follows scroll until resting in original position.
var originalPosition = false;
(function(){
var $ = jQuery;
function updateSliderSizeAndOrigPos(){
setTimeout(function(){
$("#interest-form__wrap").css('top', 0 + 'px')
originalPosition = $("#interest-form__wrap").offset().top
updateSliderSize()
}, 500)
}
function updateSliderSize(){
if(originalPosition === false){
originalPosition = $("#interest-form__wrap").offset().top
}
//console.log('offset', offset, '=', $(window).height(), '+', $(document).scrollTop(), ' - ', (originalPosition + $("#interest-form__wrap").height()))
var offset = ($(window).height() + $(document).scrollTop()) - (originalPosition + $("#interest-form__wrap").height())
if($(window).width() > 1000){
$("#interest-form__wrap").css('top', Math.min(0,offset) + 'px')
}else{
$("#interest-form__wrap").css('top', 0 + 'px')
}
}
$(window).on('resize',updateSliderSizeAndOrigPos)
$(window).on('scroll',updateSliderSize)
$(document).ready(function(){
$("#interest-form__wrap").css({position:'relative', transition:'all .5s'})
updateSliderSize()
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment