Skip to content

Instantly share code, notes, and snippets.

@klebercode
Last active October 18, 2017 19:16
Show Gist options
  • Select an option

  • Save klebercode/f8e94c5f8443c5d725bd199c907deb4b to your computer and use it in GitHub Desktop.

Select an option

Save klebercode/f8e94c5f8443c5d725bd199c907deb4b to your computer and use it in GitHub Desktop.
css resize element on scroll
#header {
width: 100%;
height: 287px;
background-color: #673ab7;
position: absolute;
z-index: 1;
}
#content {
position: relative;
top: 287px;
height: 1000px;
background: yellow;
}
<div id="header">
My center div...
</div>
<div id="content">
test
</div>
$(function(){
var
Node = $('#header'),
BaseHeight = Node.height();
// To initially run the function:
$(window).resize();
var $scrollingDiv = Node;
$(window).scroll(function() {
var winScrollTop = $(window).scrollTop() + 0,
newSize = BaseHeight - winScrollTop;
Node.css({
height: newSize,
"min-height": "70px",
"marginTop": winScrollTop + "px"
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment