Skip to content

Commit

Permalink
Add dist
Browse files Browse the repository at this point in the history
  • Loading branch information
scriptex committed Apr 2, 2018
1 parent 5b6ac31 commit 5a88050
Show file tree
Hide file tree
Showing 7 changed files with 1,932 additions and 194 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/env", "@babel/stage-2"]
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ typings/

# dotenv environment variables file
.env

# Misc
.DS_Store
195 changes: 5 additions & 190 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">

<title>IntroScroll Demo</title>

<style type="text/css" media="screen">
Expand Down Expand Up @@ -36,202 +36,17 @@

<div class="container" id="container">
<section class="section" style="background-color: gray">Gray Section</section><!-- /.section -->

<section class="section" style="background-color: lime">Lime Section</section><!-- /.section -->

<section class="section" style="background-color: lavender">Lavender Section</section><!-- /.section -->
</div>
</div>

<script>
class IntroScroll {
constructor(settings = {}) {
this.settings = Object.assign(
{},
{
element: '#intro',
wrapper: '#wrapper',
container: '#container',
trigger: '#intro__link',
scrollClass: 'is-scrolled',
duration: 1500,
afterScroll: null
},
settings
);
this.win = window;
this.doc = document;
this.element = this.getElement(this.settings.element);
this.wrapper = this.getElement(this.settings.wrapper);
this.container = this.getElement(this.settings.container);
this.trigger = this.getElement(this.settings.trigger);
this.scrollClass = this.settings.scrollClass;
this.duration = this.settings.duration;
this.afterScroll = this.settings.afterScroll;
this.isScrolling = false;

this.init();
}

init() {
this.bind();
}

getElement(selector) {
return this.doc.querySelector(selector);
}

swipe(element) {
let touchstartX = 0;
let touchstartY = 0;
let touchendX = 0;
let touchendY = 0;

const onSwipeUp = new Event('onswipeup');
const onSwipeRight = new Event('onswiperight');
const onSwipeDown = new Event('onswipedown');
const onSwipeLeft = new Event('onswipeleft');
const onTap = new Event('tap');

element.addEventListener(
'touchstart',
event => {
touchstartX = event.changedTouches[0].screenX;
touchstartY = event.changedTouches[0].screenY;
},
false
);

element.addEventListener(
'touchend',
event => {
touchendX = event.changedTouches[0].screenX;
touchendY = event.changedTouches[0].screenY;

handleTouch();
},
false
);

const handleTouch = () => {
if (touchendX < touchstartX) {
element.dispatchEvent(onSwipeLeft);
}

if (touchendX > touchstartX) {
element.dispatchEvent(onSwipeRight);
}

if (touchendY < touchstartY) {
element.dispatchEvent(onSwipeUp);
}

if (touchendY > touchstartY) {
element.dispatchEvent(onSwipeDown);
}

if (touchendY === touchstartY) {
element.dispatchEvent(onTap);
}
};
}

bind() {
const win = this.win;
const trigger = this.trigger;
const element = this.element;
const wrapper = this.wrapper;
const container = this.container;

this.swipe(element);
this.swipe(wrapper);

element.addEventListener(
'mousewheel',
event => {
if (event.deltaY > 0) {
this.enableScroll();
}
},
false
);

element.addEventListener(
'onswipeup',
event => {
this.enableScroll();
},
false
);

wrapper.addEventListener(
'onswipedown',
event => {
if (this.win.pageYOffset <= 0) {
this.disableScroll();
}
},
false
);

container.addEventListener(
'mousewheel',
event => {
if (event.deltaY < 0 && this.win.pageYOffset <= 0) {
this.disableScroll();
}
},
false
);

win.addEventListener(
'mousewheel',
event => {
if (win.pageYOffset <= 0 && wrapper.classList.contains(this.scrollClass) && event.deltaY < 0) {
this.disableScroll();
}
},
false
);

trigger.addEventListener(
'click',
event => {
event.preventDefault();

if (!wrapper.classList.contains(this.scrollClass) || win.pageYOffset > 1) {
this.enableScroll();
}
},
false
);
}

enableScroll() {
this.wrapper.classList.add(this.scrollClass);
this.isScrolling = true;

setTimeout(() => {
this.doc.body.style.overflow = 'visible';
this.isScrolling = false;

typeof this.afterScroll === 'function' && this.afterScroll(this);
}, this.duration);
}

disableScroll() {
if (this.isScrolling) {
return;
}

this.wrapper.classList.remove(this.scrollClass);
this.doc.body.style.overflow = 'hidden';
}
}
</script>
<script src="../dist/introscroll.js"></script>

<script>
new IntroScroll();
</script>
</body>
</html>
</html>
169 changes: 169 additions & 0 deletions dist/introscroll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

var IntroScroll =
/*#__PURE__*/
function () {
function IntroScroll() {
var settings = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

_classCallCheck(this, IntroScroll);

this.settings = Object.assign({}, {
element: '#intro',
wrapper: '#wrapper',
container: '#container',
trigger: '#intro__link',
scrollClass: 'is-scrolled',
duration: 1500,
afterScroll: null
}, settings);
this.win = window;
this.doc = document;
this.element = this.getElement(this.settings.element);
this.wrapper = this.getElement(this.settings.wrapper);
this.container = this.getElement(this.settings.container);
this.trigger = this.getElement(this.settings.trigger);
this.scrollClass = this.settings.scrollClass;
this.duration = this.settings.duration;
this.afterScroll = this.settings.afterScroll;
this.isScrolling = false;
this.init();
}

_createClass(IntroScroll, [{
key: "init",
value: function init() {
this.bind();
}
}, {
key: "getElement",
value: function getElement(selector) {
return this.doc.querySelector(selector);
}
}, {
key: "swipe",
value: function swipe(element) {
var touchstartX = 0;
var touchstartY = 0;
var touchendX = 0;
var touchendY = 0;
var onSwipeUp = new Event('onswipeup');
var onSwipeRight = new Event('onswiperight');
var onSwipeDown = new Event('onswipedown');
var onSwipeLeft = new Event('onswipeleft');
var onTap = new Event('tap');
element.addEventListener('touchstart', function (event) {
touchstartX = event.changedTouches[0].screenX;
touchstartY = event.changedTouches[0].screenY;
}, false);
element.addEventListener('touchend', function (event) {
touchendX = event.changedTouches[0].screenX;
touchendY = event.changedTouches[0].screenY;
handleTouch();
}, false);

var handleTouch = function handleTouch() {
if (touchendX < touchstartX) {
element.dispatchEvent(onSwipeLeft);
}

if (touchendX > touchstartX) {
element.dispatchEvent(onSwipeRight);
}

if (touchendY < touchstartY) {
element.dispatchEvent(onSwipeUp);
}

if (touchendY > touchstartY) {
element.dispatchEvent(onSwipeDown);
}

if (touchendY === touchstartY) {
element.dispatchEvent(onTap);
}
};
}
}, {
key: "bind",
value: function bind() {
var _this = this;

var win = this.win;
var trigger = this.trigger;
var element = this.element;
var wrapper = this.wrapper;
var container = this.container;
this.swipe(element);
this.swipe(wrapper);
element.addEventListener('mousewheel', function (event) {
if (event.deltaY > 0) {
_this.enableScroll();
}
}, false);
element.addEventListener('onswipeup', function (event) {
_this.enableScroll();
}, false);
wrapper.addEventListener('onswipedown', function (event) {
if (_this.win.pageYOffset <= 0) {
_this.disableScroll();
}
}, false);
container.addEventListener('mousewheel', function (event) {
if (event.deltaY < 0 && _this.win.pageYOffset <= 0) {
_this.disableScroll();
}
}, false);
win.addEventListener('mousewheel', function (event) {
if (win.pageYOffset <= 0 && wrapper.classList.contains(_this.scrollClass) && event.deltaY < 0) {
_this.disableScroll();
}
}, false);
trigger.addEventListener('click', function (event) {
event.preventDefault();

if (!wrapper.classList.contains(_this.scrollClass) || win.pageYOffset > 1) {
_this.enableScroll();
}
}, false);
}
}, {
key: "enableScroll",
value: function enableScroll() {
var _this2 = this;

this.wrapper.classList.add(this.scrollClass);
this.isScrolling = true;
setTimeout(function () {
_this2.doc.body.style.overflow = 'visible';
_this2.isScrolling = false;
typeof _this2.afterScroll === 'function' && _this2.afterScroll(_this2);
}, this.duration);
}
}, {
key: "disableScroll",
value: function disableScroll() {
if (this.isScrolling) {
return;
}

this.wrapper.classList.remove(this.scrollClass);
this.doc.body.style.overflow = 'hidden';
}
}]);

return IntroScroll;
}();

exports.default = IntroScroll;
Loading

0 comments on commit 5a88050

Please sign in to comment.