Skip to content

Instantly share code, notes, and snippets.

@reflexxion
Forked from anonymous/index.html
Created June 28, 2016 17:42
Show Gist options
  • Save reflexxion/6bc99b8c220547696fc464447c5ce92b to your computer and use it in GitHub Desktop.
Save reflexxion/6bc99b8c220547696fc464447c5ce92b to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/kotali
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style id="jsbin-css">
body, html {
margin:0;
padding:0;
overflow:hidden;
font-family: sans-serif;
font-size: 150px;
color: white;
font-weight: bold;
text-shadow: 5px 5px 30px #000;
}
section {
display: block;
height: 100%;
}
section > div {
display: table;
width: 100%;
height: 100%;
}
section > div > div {
display: table-cell;
vertical-align: middle;
text-align: center;
}
</style>
</head>
<body>
<div class="main">
<section style="background: red"><div>
<div>1</div>
</div></section>
<section style="background: blue"><div>
<div>2</div>
</div></section>
<section style="background: yellow"><div>
<div>3</div>
</div></section>
<section style="background: green"><div>
<div>4</div>
</div></section>
</div>
<script id="jsbin-javascript">
(function() { $(function() {
var sectionContainer = $('.main'),
loop = true,
throttleTimer, throttleTimeout = 500,
scrollSpeed = 400,
sections = sectionContainer.children('section'),
scrollToSection = function(section) {
activeSection.removeClass('active');
activeSection = section.addClass('active');
$('body, html').animate({
scrollTop: activeSection.offset().top
}, scrollSpeed);
},
onScroll = function (e) {
e.preventDefault();
var delta = e.originalEvent.detail < 0 || e.originalEvent.wheelDelta > 0 ? 1 : -1,
targetSection;
if (delta < 0) { // DOWN
targetSection = activeSection.next('section');
if (targetSection.length === 0 && loop) {
targetSection = sections.first();
}
} else { // UP
targetSection = activeSection.prev('section');
if (targetSection.length === 0 && loop) {
targetSection = sections.last();
}
}
if (targetSection.length > 0) {
if (throttleTimer) {
window.clearTimeout(throttleTimer);
}
throttleTimer = window.setTimeout(function() {
scrollToSection(targetSection);
}, throttleTimeout);
}
},
init = function () {
sections.height($(window).height());
$(document).on('mousewheel DOMMouseScroll', onScroll);
},
activeSection = sectionContainer.children('section').first().addClass('active');
init();
}); }());
</script>
<script id="jsbin-source-css" type="text/css">body, html {
margin:0;
padding:0;
overflow:hidden;
font-family: sans-serif;
font-size: 150px;
color: white;
font-weight: bold;
text-shadow: 5px 5px 30px #000;
}
section {
display: block;
height: 100%;
}
section > div {
display: table;
width: 100%;
height: 100%;
}
section > div > div {
display: table-cell;
vertical-align: middle;
text-align: center;
}</script>
<script id="jsbin-source-javascript" type="text/javascript">(function() { $(function() {
var sectionContainer = $('.main'),
loop = true,
throttleTimer, throttleTimeout = 500,
scrollSpeed = 400,
sections = sectionContainer.children('section'),
scrollToSection = function(section) {
activeSection.removeClass('active');
activeSection = section.addClass('active');
$('body, html').animate({
scrollTop: activeSection.offset().top
}, scrollSpeed);
},
onScroll = function (e) {
e.preventDefault();
var delta = e.originalEvent.detail < 0 || e.originalEvent.wheelDelta > 0 ? 1 : -1,
targetSection;
if (delta < 0) { // DOWN
targetSection = activeSection.next('section');
if (targetSection.length === 0 && loop) {
targetSection = sections.first();
}
} else { // UP
targetSection = activeSection.prev('section');
if (targetSection.length === 0 && loop) {
targetSection = sections.last();
}
}
if (targetSection.length > 0) {
if (throttleTimer) {
window.clearTimeout(throttleTimer);
}
throttleTimer = window.setTimeout(function() {
scrollToSection(targetSection);
}, throttleTimeout);
}
},
init = function () {
sections.height($(window).height());
$(document).on('mousewheel DOMMouseScroll', onScroll);
},
activeSection = sectionContainer.children('section').first().addClass('active');
init();
}); }());
</script></body>
</html>
body, html {
margin:0;
padding:0;
overflow:hidden;
font-family: sans-serif;
font-size: 150px;
color: white;
font-weight: bold;
text-shadow: 5px 5px 30px #000;
}
section {
display: block;
height: 100%;
}
section > div {
display: table;
width: 100%;
height: 100%;
}
section > div > div {
display: table-cell;
vertical-align: middle;
text-align: center;
}
(function() { $(function() {
var sectionContainer = $('.main'),
loop = true,
throttleTimer, throttleTimeout = 500,
scrollSpeed = 400,
sections = sectionContainer.children('section'),
scrollToSection = function(section) {
activeSection.removeClass('active');
activeSection = section.addClass('active');
$('body, html').animate({
scrollTop: activeSection.offset().top
}, scrollSpeed);
},
onScroll = function (e) {
e.preventDefault();
var delta = e.originalEvent.detail < 0 || e.originalEvent.wheelDelta > 0 ? 1 : -1,
targetSection;
if (delta < 0) { // DOWN
targetSection = activeSection.next('section');
if (targetSection.length === 0 && loop) {
targetSection = sections.first();
}
} else { // UP
targetSection = activeSection.prev('section');
if (targetSection.length === 0 && loop) {
targetSection = sections.last();
}
}
if (targetSection.length > 0) {
if (throttleTimer) {
window.clearTimeout(throttleTimer);
}
throttleTimer = window.setTimeout(function() {
scrollToSection(targetSection);
}, throttleTimeout);
}
},
init = function () {
sections.height($(window).height());
$(document).on('mousewheel DOMMouseScroll', onScroll);
},
activeSection = sectionContainer.children('section').first().addClass('active');
init();
}); }());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment