Skip to content

Instantly share code, notes, and snippets.

@mattkruskamp
Created August 24, 2015 08:28
Show Gist options
  • Save mattkruskamp/86d65686e36ff6271037 to your computer and use it in GitHub Desktop.
Save mattkruskamp/86d65686e36ff6271037 to your computer and use it in GitHub Desktop.
Pinterest-Style Modal Popup Scrolling in Javascript
<html>
<head>
<style type="text/css">
.container {
width: 100%;
color: #ccc;
}
.large-content {
width: 800px;
margin: 0px auto;
}
.container.active {
position: fixed;
}
.popup {
display: none;
}
.popup.active {
display: block;
position: absolute;
}
</style>
<title>Testy McTesterson's Test</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="test.js"></script>
</head>
<body>
<div class="popup">
<p>Lots of content</p>
</div>
<div class="container">
<div class="large-content">
<p>Freaking tons of content <a href="#">Link to show modal</a></p>
</div>
</div>
</body>
</html>
$(document).ready(function () {
var clickableLink = $('a'),
container = $('.container'),
popup = $('.popup'),
body = $('body'),
activePosition = 0,
popupActive = false,
win = $(window);
clickableLink.click(function (e) {
e.preventDefault();
e.stopPropagation();
var position = win.scrollTop();
// set the location of the scrolling
container.css('top', -position + 'px');
activePosition = position;
// set the window to the top
win.scrollTop(0);
// add popup styling
container.addClass('active');
popup.addClass('active');
popupActive = true;
});
// called if the popup is active
body.click(function (e) {
if (!popupActive)
return false;
// remove styling
container.removeClass('active');
popup.removeClass('active');
popupActive = false;
// move the container and the scroller back
container.css('top', '0px');
win.scrollTop(activePosition);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment