Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <main id="main">
  <div id="target">
    <section class="section a four">
      <div class="slide"><div class="image">A1</div></div>
      <div class="slide"><div class="image">A2</div></div>
      <div class="slide"><div class="image">A3</div></div>
      <div class="slide"><div class="image">A4</div></div>
    </section>
    <section class="section b three">
      <div class="slide"><div class="image">B1</div></div>
      <div class="slide"><div class="image">B2</div></div>
      <div class="slide"><div class="image">B3</div></div>
    </section>
    <section class="section c four">
      <div class="slide"><div class="image">C1</div></div>
      <div class="slide"><div class="image">C2</div></div>
      <div class="slide"><div class="image">C3</div></div>
      <div class="slide"><div class="image">C4</div></div>
    </section>
    <section class="section d four">
      <div class="slide"><div class="image">D1</div></div>
      <div class="slide"><div class="image">D2</div></div>
      <div class="slide"><div class="image">D3</div></div>
      <div class="slide"><div class="image">D4</div></div>
    </section>
  </div>
</main>
      
              
            
!

CSS

              
                // reset and set behaviour
html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}
body {
  pointer-events: none;
}
.image, a, .control {
  pointer-events: auto;
}
// main
#main {
  z-index: 20;
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  overflow: visible;
  background: #000;
}
.section {
  position: relative;
  height: 100vh;
  width: 100vw;
  line-height: 100vh;
  text-align: center;
}
.three {
  width: 600vw!important;
}
.four {
  width: 800vw!important;
}
.slide {
  position: relative;
  height: 100vh;
  width: 100vw;
  float: left;
}
// navigation
.control {
	z-index: 10;
	position: absolute;
	&.prev-slide, &.next-slide {
	  top: 50%;
	  transform: translateY(-50%);
    height: 33.33334vw;
  	max-height: 100%;
    max-height: calc(100% - 2.5em);
		width: 50%;
	}
	&.prev-slide {
		left: 0%;
		cursor: w-resize;
	}
	&.next-slide {
		right: 0%;
		cursor: e-resize;
	}
	&.prev-project, &.next-project {
		left: 0;
		height: 50%;
		width: 100%;
	}
	&.prev-project {
		top: 0%;
		cursor: n-resize;
	}
	&.next-project {
		bottom: 0%;
		cursor: s-resize;
	}
}
// media
.image {
  z-index: 90;
  position: absolute;
  display: block;
  top: 50%;
  left: 50%;
  width: 50%;
  height: 33.33334vw;
  max-height: 100%;
  max-height: calc(100% - 2.5em);
  background: #fff;
  transform: translate3d(-50%, -50%, 0);
  transform-origin: top left;
}
@media all and (orientation:portrait) {
  .image {
    width: 100vw;
    max-width: 100vw;
    height:  100vw;
    height:  calc(100vw - 2.5em);
    transform: rotate(-90deg) translate3d(-50%, -50%, 0)!important;
  }
}
              
            
!

JS

              
                // get elements:
var $window;
var $body;
var $target;
var $section;
var windowHeight;
var documentHeight;
var sectionLength;
var setSectionWidth = [];
var maxYScroll;
// set draggable:
var proxyV;
var proxyH = [];
var draggableV;
// monitoring:
var isScroll = false;
var scrollings = [];
var proxyY;
var proxyX = [];
// handle vertical and horizontal dragging
var touch, tolerance = 10, draggingV = false, draggingH = false, startPos = {x: Number.NaN, y: Number.NaN}, delta = {x: Number.NaN, y: Number.NaN};

function getElements() {
	$window = $(window);
	$body = $('body');
	$target = $('#target');
	$section = $('.section');
}

function getSizes() {
	windowHeight = $window.height();
	documentHeight = $target.height();
}

function updateDraggableY() {
	// make sure the y value is always between 0 and documentHeight
	var newY = this.y % documentHeight;
	if(newY < 0){
		newY = newY + documentHeight - 0.01;
	}
	if(newY > 0){
		newY = newY - documentHeight;
	}
	if (newY !== this.y) {
		TweenLite.set($target, {
			y: newY,
			overwrite: false
		});
		this.y = newY;
	}
}

function setDraggable() {
	draggableV = Draggable.create(proxyV, {
		type: 'y',
		trigger: '.image',
		lockAxis: true,
		onDrag: updateDraggableY,
		onThrowUpdate: updateDraggableY,
		throwProps: true,
		dragResistance: 1,
		snap: function(endValue) {
			var step = maxYScroll / (sectionLength);
			return Math.round( endValue / step) * step;
		},
		onDragStart: function() {
			isScroll = true;
		},
		onDragEnd: function() {
			isScroll = false;
			proxyY = this.endY;
		}
	});
	Draggable.create($section, {
		type: 'x',
		lockAxis: true,
		throwProps: true,
		dragResistance: 1,
		snap: function(endValue) {
			var width = $(this.target).width() / 2;
			var length = $(this.target).children().length;
			var step = width / length;
			return Math.round( endValue / step) * step;
		},
		onDragStart: function() {
			isScroll = true;
		},
		onDragEnd: function() {
			isScroll = false;
		},
		onDrag: function() {
			var section = this.target;
			var sectionWidth = $(section).width() * 0.5;
			var sectionX = this.x;
			var newX = sectionX % sectionWidth * 2;
			if(newX < 0){
				newX = newX + sectionWidth - 0.01;
			}
			if(newX > 0){
				newX = newX - sectionWidth;
			}
			if (newX !== sectionX) {
				TweenLite.set(this, {
					x: newX,
					overwrite: false
				});
				this.x = newX;
			}
		},
		onThrowUpdate: function() {
			var section = this.target;
			var sectionWidth = $(section).width() * 0.5;
			var sectionX = this.x;
			var newX = sectionX % sectionWidth * 2;
			if(newX > 0){
				newX = newX - sectionWidth;
			}
			if (newX !== sectionX) {
				TweenLite.set(this, {
					x: newX,
					overwrite: false
				});
				this.x = newX;
			}
		}
	});
}
function setDom() {
	$target.children().clone(true, true).prependTo($target);
	$section.each(function() {
		$(this).children().clone(true, true).prependTo($(this));
	})
	.promise()
	.done( function() {
		$section = $('.section');
		sectionLength = $target.children().length;
		$section.each(function(){
			setSectionWidth.push($(this).width());
		});
		maxYScroll = windowHeight * sectionLength;

		proxyV = document.createElement('div');
		for (var i = 0; i < sectionLength; i++) { // ??????
			var currentProxyH = document.createElement('div');
			proxyH[i] = currentProxyH;
		}

		setDraggable();
	});
	$body.append('<div class="control prev-project"></div><div class="control next-project"></div><div class="control prev-slide"></div><div class="control next-slide">');
}

/**
* Gets the average of the last `number` elements of the given array.
*/
function getAverage(elements, number){
	var sum = 0;

	//taking `number` elements from the end to make the average, if there are not enought, 1
	var lastElements = elements.slice(Math.max(elements.length - number, 1));

	for(var i = 0; i < lastElements.length; i++){
		sum = sum + lastElements[i];
	}

	return Math.ceil(sum / number);
}

function updateTweenVertical() {
	// draggable.y; // = this.target._gsTransform.y;
	var newY = draggableV[0].y % documentHeight;

	if(newY < 0){
		newY = newY + documentHeight - 0.01;
	}
	if(newY > 0){
		newY = newY - documentHeight;
	}
	if (newY !== draggableV[0].y) {
		TweenLite.set($target, {
			y: newY,
			overwrite: false
		});
		draggableV[0].y = newY;
	}
	draggableV[0].update();
}

function getPos(e) {
	touch = e.type === 'touchstart' ? e.originalEvent.touches[0] : e.originalEvent;
	delta.x = touch.pageX - startPos.x;
	delta.y = touch.pageY - startPos.y;
	draggingV = Math.abs(delta.y) > tolerance;
	draggingH = Math.abs(delta.x) > tolerance;
	if (draggingV) {
		draggableV[0].dragResistance = 0;
		$(window).off('mousemove touchmove', getPos);
	} else if (draggingH) {
		for (var i = 0; i < sectionLength; i++) {
			var d = $section[i];
			Draggable.get(d).dragResistance = 0;
		}
		$(window).off('mousemove touchmove', getPos);
	}
}

$(window).on('mousedown touchstart', function (e) {

	touch = e.type === 'touchstart' ? e.originalEvent.touches[0] : e.originalEvent;

	startPos.x = touch.pageX;
	startPos.y = touch.pageY;

	$(window).on('mousemove touchmove', getPos);

});

$(window).on('mouseup touchend', function () {
	$(window).off('mousemove touchmove');
	for (var i = 0; i < sectionLength; i++) {
		var d = $section[i];
		Draggable.get(d).dragResistance = 1;
	}
	draggableV[0].dragResistance = 1;
});

function moveSectionDown() {
	TweenLite.to(proxyV, 1.2, {
		y: proxyY - windowHeight,
		ease: Power2.easeOut,
		onUpdate: updateTweenVertical,
		onStart: function() {
			isScroll = true;
		},
		onComplete: function() {
			isScroll = false;
			proxyY = draggableV[0].y;
			draggableV[0].update();
		}
	});
}

function moveSectionUp() {
	TweenLite.to(proxyV, 1.2, {
		y: proxyY + windowHeight,
		ease: Power2.easeOut,
		onUpdate: updateTweenVertical,
		onStart: function() {
			isScroll = true;
		},
		onComplete: function() {
			isScroll = false;
			proxyY = draggableV[0].y;
			draggableV[0].update();
		}
	});
}

// mouseWheelHandler by https://github.com/alvarotrigo/fullPage.js

var prevTime = new Date().getTime();

function MouseWheelHandler(e) {
	var curTime = new Date().getTime();
	// cross-browser wheel delta
	e = e || window.event;
	var valueY = e.wheelDelta || -e.deltaY || -e.detail;
//	var valueX = -e.deltaX;
	var deltaY = Math.max(-1, Math.min(1, valueY));
//	var deltaX = Math.max(-1, Math.min(1, valueX));

	// add isScrollingHorizontally and integrate with slide navigation
	var horizontalDetection = typeof e.wheelDeltaX !== 'undefined' || typeof e.deltaX !== 'undefined';
	var isScrollingVertically = (Math.abs(e.wheelDeltaX) < Math.abs(e.wheelDelta)) || (Math.abs(e.deltaX ) < Math.abs(e.deltaY) || !horizontalDetection);

	//Limiting the array to 150 (lets not waste memory!)
	if(scrollings.length > 149){
		scrollings.shift();
	}
	//keeping record of the previous scrollings
	scrollings.push(Math.abs(valueY));

	//time difference between the last scroll and the current one
	var timeDiff = curTime - prevTime;
	prevTime = curTime;

	// haven't they scrolled in a while?
	// (enough to be consider a different scrolling action to scroll another section)
	if(timeDiff > 200){
		//emptying the array, we dont care about old scrollings for our averages
		scrollings = [];
	}
	if(!isScroll) {
	//	isScroll = true;
		var averageEnd = getAverage(scrollings, 10);
		var averageMiddle = getAverage(scrollings, 70);
		var isAccelerating = averageEnd >= averageMiddle;
		//to avoid double swipes...
		if(isAccelerating && isScrollingVertically) {
			//scrolling down?
			if (deltaY < 0) {
				moveSectionDown();
			//scrolling up?
			} else if (deltaY > 0) {
				moveSectionUp();
			}
		}
	}
}

/**
* Adds the auto scrolling action for the mouse wheel and trackpad.
* After this function is called, the mousewheel and trackpad movements will scroll through sections
* https://developer.mozilla.org/en-US/docs/Web/Events/wheel
*/
function addMouseWheelHandler() {
	var prefix = '';
	var addEventListener;

	if (window.addEventListener) {
		addEventListener = 'addEventListener';
	}else{
		addEventListener = 'attachEvent';
		prefix = 'on';
	}
	// detect available wheel event
	var support = 'onwheel' in document.createElement('div') ? 'wheel' : // Modern browsers support 'wheel'
		document.onmousewheel !== undefined ? 'mousewheel' : // Webkit and IE support at least 'mousewheel'
		'DOMMouseScroll'; // let's assume that remaining browsers are older Firefox
	if(support === 'DOMMouseScroll') {
		document[ addEventListener ](prefix + 'MozMousePixelScroll', MouseWheelHandler, false);
	}
	// handle MozMousePixelScroll in older Firefox
	else{
		document[ addEventListener ](prefix + support, MouseWheelHandler, false);
	}
}
function resizeHandler() {
	getSizes();
	draggableV[0].update();
	for (var i = 0; i < sectionLength; i++) {
		var d = $section[i];
		Draggable.get(d).update();
	}
}
function bindEvents() {
	proxyY = draggableV[0].y;
	for (var i = 0; i < sectionLength; i++) {
		var d = $section[i];
		proxyX[i] = Draggable.get(d).x;
	}
	addMouseWheelHandler();
	$window.resize(resizeHandler);
	$('.prev-project').on('click', function(){
		moveSectionUp();
	});
	$('.next-project').on('click', function(){
		moveSectionDown();
	});
}
$(document).ready(function() {
	getElements();
	getSizes();
	setDom();
	bindEvents();
});

              
            
!
999px

Console