Skip to content

Instantly share code, notes, and snippets.

@DamianMullins
Last active February 24, 2017 13:43
Show Gist options
  • Select an option

  • Save DamianMullins/6bf935796b9fcbe43a01ea90d5a3c037 to your computer and use it in GitHub Desktop.

Select an option

Save DamianMullins/6bf935796b9fcbe43a01ea90d5a3c037 to your computer and use it in GitHub Desktop.
Slide Interface
import Swipe from 'swipejs';
import { lory } from 'lory.js';
/*
* Helpers
*/
const $ = (selector) => document.querySelector(selector);
const $$ = (selector) => Array.prototype.slice.apply(document.querySelectorAll(selector));
/*
* Swipe
*/
$$('.swipe').forEach(swipeEl => {
const options = {
prevClass: 'swipePrev',
nextClass: 'swipeNext'
};
const swipeInstance = new Swipe(swipeEl, {
draggable: true,
disableScroll: true,
continuous: false
});
const slideInterface = {
element: swipeEl,
slideInstance: swipeInstance,
prev: swipeInstance.prev,
next: swipeInstance.next
};
setupSlide(slideInterface, options);
});
/*
* Lory
*/
$$('.slider').forEach(swipeEl => {
const options = {
prevClass: 'swipePrev',
nextClass: 'swipeNext'
};
const loryInstance = lory(swipeEl, {
infinite: false,
enableMouseEvents: true,
classNameFrame: 'lory-frame',
classNameSlideContainer: 'lory-slides'
});
const slideInterface = {
element: swipeEl,
slideInstance: loryInstance,
prev: loryInstance.prev,
next: loryInstance.next
};
setupSlide(slideInterface, options);
});
/*
* Create the slide
*/
function setupSlide (slideInterface, opts = {}) {
const defaults = {
prevClass: 'prev',
nextClass: 'next'
};
const options = { ...defaults, ...opts };
console.log(slideInterface);
function setup () {
createNavElements();
}
function createNavElements () {
const navElFragment = document.createDocumentFragment();
const prev = document.createElement("a");
prev.textContent = 'Previous';
prev.classList.add('slider-nav', options.prevClass);
prev.addEventListener('click', e => {
e.preventDefault();
slideInterface.prev();
});
navElFragment.appendChild(prev);
const next = document.createElement("a");
next.textContent = 'Next';
next.classList.add('slider-nav', options.nextClass);
next.addEventListener('click', e => {
e.preventDefault();
slideInterface.next();
});
navElFragment.appendChild(next);
slideInterface.element.appendChild(navElFragment);
}
setup();
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Sliders</title>
<style>
p {
padding: 20px;
text-align: center;
}
.slider-nav {
color: white;
display: inline-block;
background-color: green;
padding: 10px;
user-select: none;
}
.slider-nav:hover {
background-color: darkgreen;
cursor: pointer;
}
</style>
<style>
.swipe {
overflow: hidden;
visibility: hidden;
position: relative;
width: 400px;
}
.swipe-wrap {
overflow: hidden;
position: relative;
}
.swipe-slide {
float: left;
width: 100%;
position: relative;
background-color: orange;
}
</style>
<style>
.lory {
}
.lory-frame {
width: 400px;
position: relative;
line-height: 0;
overflow: hidden;
white-space: nowrap;
}
.lory-slides {
display: inline-block;
margin: 0;
}
.lory-slide {
position: relative;
display: inline-block;
width: 400px;
background-color: orange;
}
</style>
</head>
<body>
<h1>Slides</h1>
<h2>Swipe</h2>
<div class="swipe">
<div class="swipe-wrap">
<div class="swipe-slide">
<p>1</p>
</div>
<div class="swipe-slide">
<p>2</p>
</div>
<div class="swipe-slide">
<p>3</p>
</div>
</div>
</div>
<h2>Lory</h2>
<div class="slider js_slider">
<div class="lory-frame">
<ul class="lory-slides">
<li class="lory-slide">
<p>1</p>
</li>
<li class="lory-slide">
<p>2</p>
</li>
<li class="lory-slide">
<p>3</p>
</li>
</ul>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment