Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rachelslurs/db53a256976ed1183bbf to your computer and use it in GitHub Desktop.
Save rachelslurs/db53a256976ed1183bbf to your computer and use it in GitHub Desktop.
Vertical and horizontal center, change width on scroll
<div class="container">
<div id="lightbox">
<img src="http://placehold.it/140/8BC34A/ffffff">
<img src="http://placehold.it/140/3F51B5/ffffff">
<img src="http://placehold.it/140/E91E63/ffffff">
<img src="http://placehold.it/140/00BCD4/ffffff">
<img src="http://placehold.it/140/FF5722/ffffff">
<img src="http://placehold.it/140/9C27B0/ffffff">
</div>
</div>
$(function() {
var lightbox = $('#lightbox');
var initialWidth = 800;
var finalWidth = initialWidth / 3;
var diffWidth = initialWidth - finalWidth;
$(window).scroll(function() {
var scrollFromTop = $(window).scrollTop();
var newSize = 800 - (diffWidth * (scrollFromTop / 2000));
console.log(lightbox.width());
lightbox.css({
width: newSize,
"marginTop": scrollFromTop + "px"
});
});
});
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
.container {
height: 2000px;
overflow:hidden;
}
#lightbox {
position:relative;
margin: 0 auto;
height:100vh;
display: flex;
align-items: center;
justify-content: center;
width: 800px;
background-color:#e4e4e4;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment