Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(js): better indentation, editorconfig added #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
172 changes: 86 additions & 86 deletions src/delighters.js
Original file line number Diff line number Diff line change
@@ -1,98 +1,98 @@
/*
Delighters - Add CSS animations to delight users as they scroll down.
(c) 2018 - Q42
Written by Martin Kool
https://github.com/Q42/delighters
Delighters - Add CSS animations to delight users as they scroll down.
(c) 2018 - Q42
Written by Martin Kool
https://github.com/Q42/delighters
*/
var Delighters = new (function() {
var self = this,
dels = this.dels = [],
var self = this,
dels = this.dels = [],

// default options
options = {
attribute: 'data-delighter',
classNames: ['delighter', 'started', 'ended'],
start: 0.75, // default start threshold
end: 0.75, // default end threshold
autoInit: true // initialize when DOMContentLoaded
};
// default options
options = {
attribute: 'data-delighter',
classNames: ['delighter', 'started', 'ended'],
start: 0.75, // default start threshold
end: 0.75, // default end threshold
autoInit: true // initialize when DOMContentLoaded
};

document.addEventListener("DOMContentLoaded", function() {
if (options.autoInit) init();
});
document.addEventListener("DOMContentLoaded", function() {
if (options.autoInit) init();
});

function config(opts) {
for (var name in opts) options[name] = opts[name];
}
function init() {
document.addEventListener('scroll', scroll)
var els = document.querySelectorAll('[' + options.attribute + ']');
function config(opts) {
for (var name in opts) options[name] = opts[name];
}
function init() {
document.addEventListener('scroll', scroll)
var els = document.querySelectorAll('[' + options.attribute + ']');

for (var i=0; i<els.length; i++) {
var el = els[i],
def = el.getAttribute(options.attribute, 2),
pairs = def.split(';'),
del = {};
del.start = options.start;
del.end = options.end;
for (var j=0; j<pairs.length; j++) {
var pair = pairs[j].split(':'),
name = pair[0],
val = isNaN(pair[1] * 1)? pair[1] : pair[1] * 1;
if (name) del[name] = (val === undefined)? true : val;
}
for (var i=0; i<els.length; i++) {
var el = els[i],
def = el.getAttribute(options.attribute, 2),
pairs = def.split(';'),
del = {};
del.start = options.start;
del.end = options.end;
for (var j=0; j<pairs.length; j++) {
var pair = pairs[j].split(':'),
name = pair[0],
val = isNaN(pair[1] * 1)? pair[1] : pair[1] * 1;
if (name) del[name] = (val === undefined)? true : val;
}

del.el = el;
del.id = dels.length;
dels.push(del);
el.classList.add(options.classNames[0])
if (del.debug) el.style.outline = 'solid red 4px';
}
scroll();
}
del.el = el;
del.id = dels.length;
dels.push(del);
el.classList.add(options.classNames[0])
if (del.debug) el.style.outline = 'solid red 4px';
}
scroll();
}

function scroll() {
var viewportHeight = window.innerHeight;
for (var i=0; i<dels.length; i++) {
var del = dels[i],
box = del.el.getBoundingClientRect(),
factorStart = box.top / viewportHeight,
factorEnd = box.bottom / viewportHeight;
function scroll() {
var viewportHeight = window.innerHeight;
for (var i=0; i<dels.length; i++) {
var del = dels[i],
box = del.el.getBoundingClientRect(),
factorStart = box.top / viewportHeight,
factorEnd = box.bottom / viewportHeight;

if (del.debug) {
if (factorStart >= 0 && factorStart <= 1) {
if (!del.startLine) {
del.startLine = document.createElement('div')
document.body.appendChild(del.startLine);
del.startLine.style = 'position:fixed;height:0;width:100%;border-bottom:dotted red 2px;top:' + (del.start * 100) + 'vh';
}
}
if (((factorEnd < del.end) || (factorStart > 1)) && del.startLine) {
del.startLine.parentNode.removeChild(del.startLine);
delete del.startLine;
}
}
if (factorStart < del.start && !del.started) {
del.started = true;
del.el.classList.add(options.classNames[1])
}
else if (factorStart > del.start && del.started) {
del.started = false;
del.el.classList.remove(options.classNames[1])
}
if (factorEnd < del.end && !del.ended) {
del.ended = true;
del.el.classList.add(options.classNames[2])
}
else if (factorEnd > del.end && del.ended) {
del.ended = false;
del.el.classList.remove(options.classNames[2])
}
}
}
if (del.debug) {
if (factorStart >= 0 && factorStart <= 1) {
if (!del.startLine) {
del.startLine = document.createElement('div')
document.body.appendChild(del.startLine);
del.startLine.style = 'position:fixed;height:0;width:100%;border-bottom:dotted red 2px;top:' + (del.start * 100) + 'vh';
}
}
if (((factorEnd < del.end) || (factorStart > 1)) && del.startLine) {
del.startLine.parentNode.removeChild(del.startLine);
delete del.startLine;
}
}
if (factorStart < del.start && !del.started) {
del.started = true;
del.el.classList.add(options.classNames[1])
}
else if (factorStart > del.start && del.started) {
del.started = false;
del.el.classList.remove(options.classNames[1])
}
if (factorEnd < del.end && !del.ended) {
del.ended = true;
del.el.classList.add(options.classNames[2])
}
else if (factorEnd > del.end && del.ended) {
del.ended = false;
del.el.classList.remove(options.classNames[2])
}
}
}

self.init = init;
self.config = config;
self.init = init;
self.config = config;
})();