Flooper
Uses flex order attribute to loop a list of items instead of duplication or expensive multiItem position calculations. See it in action at project page
Usage & Options
Install
Old school include (exposes flooper
global):
<script src="https://unpkg.com/flooper"></script>
Savage developers:
npm install --save flooper
Then import it:
import 'flooper' from 'flooper';
const flooper = require('flooper');
HTML & CSS
Don't be a bad boy and put js-hooks classes in your CSS files please.
This is the required and minimal HTML and CSS for the plugin to work.
Note: your classNames and jsHooks can be different
<div class="c-my-flooper js-flooper">
...
<div class="c-my-flooper__el js-flooper-item"></div>
...
</div>
.c-my-flooper{
position: relative;
display: flex;
flex-wrap: nowrap;
white-space: nowrap;
}
.c-my-flooper__el{
}
Javascript
let myFlooper = new flooper('.js-flooper');
myFlooper.init();
Options and defaults
let instance = new Flooper('.js-flooper', {
autoPlay: true,
flooperItemSelector: '.js-flooper-item',
bufferSize: 10,
name: `flooperInstance`,
speed: 1,
onFloop: () => {},
onStart: () => {},
onPlay: () => {},
onPause: () => {},
onSlowmotion: () => {},
});
Usage via data-attributes
You can specifiy options via data-flooper-options
attribute and using valid JSON notation
<div class="c-my-flooper js-flooper-with-data-options" data-flooper-options=`{
"autoplay": false,
"speed": 2,
}`>
...
<div class="c-my-flooper__el js-flooper-item"></div>
...
</div>
const myFlooper = new flooper('js-flooper-with-data-options');
myFlooper.init();
Methods & Callbacks
After instanciate and init, these are the current methods available.
const myFlooper = new flooper();
myFlooper.init();
Method | Description |
---|
myFlooper.pause() | Pauses the flooper looping animation |
myFlooper.play() | Resumes animation if paused |
myFlooper.slowMotion() | Toggles slowmo mode. Reduces speed to half. Currently default behaviour on mouseHover, but soon to be optional |
myFlooper.setCallbacks(obj) | Used this to pass an object with desired callbacks after instanciation. Ex: myFlooper.setCallbacks({onFloop: function(el, i){console.log(el)}}) |
callback | Description |
---|
myFlooper.onFloop(el, order) | Each time an element order is changed, returns flooped el and its current order |
myFlooper.onStart() | when initialised |
myFlooper.onPlay() | Self describing |
myFlooper.onPause() | Self describing |
myFlooper.onSlowmotion() | Self describing |
Multiple floopers on the same page
<div data-flooper data-flooper-options=`{
"flooperItemSelector": "[data-flooper-item]",
"name": "first"
}`>
<div data-flooper-item>1</div>
...
</div>
<div data-flooper data-flooper-options=`{
"flooperItemSelector": "[data-flooper-item]",
"name": "second"
}`>
<div data-flooper-item>1</div>
...
</div>
<div data-flooper data-flooper-options=`{
"flooperItemSelector": "[data-flooper-item]",
"name": "third"
}`>
<div data-flooper-item>1</div>
...
</div>
(function(){
var $floopers = document.querySelectorAll('[data-flooper]');
$floopers.forEach(function(floop, i){
flooperInstances[i] = new flooper(floop);
});
})();
If you need access to a specific istance
(function initAllThaFloopers(){
window.flooperInstances = [];
var $floopers = document.querySelectorAll('[data-flooper]');
$floopers.forEach(function(floop, i){
flooperInstances[i] = new flooper(floop);
});
})();
...
var secondFlooper = flooperInstances.find(inst => inst.uid.startsWith('second') === true);
var secondFlooper = flooperInstances.find(inst => inst.options.name('second') === true);
secondFlooper.pause();
Developing
- Clone the repo
npm install
npm start
or gulp
Production env
To start a production like environment pass --env production
flag.
Ex:npm start --env production
Deploy site to gh-pages
npm run deploy:site
This will build a production ready optimized site and deploy it to gh-pages
branch.
Contributing
See our guidelines
Authors & Credits
Tomás Marques tomasmcm@whitesmith.co (http://tomasmcm.design/)
Renato de Leão renatodeleao@whitesmith.co (http://renatodeleao.com/)
Original Concept
Demo Images source:
Demo Animations powered by the awesome libs
License
The MIT License