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

Add onDrag function which returns percent completion; Close #2 #4

Open
wants to merge 6 commits 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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ Executed before the menu begins to close
- `onClose` (`callback`):
Executed as soon as the menu has closed

- `onDrag` (`callback`):
Executed as the menu is being dragged; returns percentage completion

# Methods

- `OSREC.superslide.open()`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "superslide.js",
"version": "1.0.0",
"version": "1.0.1",
"description": "A lightweight, slick sliding menu for your next PWA by OSREC Technologies (https://osrec.co.uk)",
"main": "superslide-std.js",
"scripts": {
Expand Down
39 changes: 32 additions & 7 deletions superslide-std.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ OSREC.superslide = function (p) {
me.p.onOpen = typeof me.p.onOpen == 'function' ? me.p.onOpen : function () {};
me.p.beforeClose = typeof me.p.beforeClose == 'function' ? me.p.beforeClose : function () {};
me.p.onClose = typeof me.p.onClose == 'function' ? me.p.onClose : function () {};
me.p.onDrag = typeof me.p.onDrag == 'function' ? me.p.onDrag : function () {};

me.body = document;
me.p.isOpen = false;
Expand Down Expand Up @@ -98,22 +99,34 @@ OSREC.superslide = function (p) {
me.p.content.style.transition = 'none';
me.p.slider.style.transition = 'none';

var sliderStartPosition = me.p.isOpen ? me.p.slider.offsetWidth : 0;
var sliderOffsetWidth = me.p.slider.offsetWidth;
var sliderStartPosition = me.p.isOpen ? sliderOffsetWidth : 0;
var translation = 0;
var delta = 0;
var completion = 0;

var touchMoveFunction = function (e) {

delta = e.changedTouches[0].clientX - startX;
translation = sliderStartPosition + delta;

if (translation > me.p.slider.offsetWidth) {
translation = me.p.slider.offsetWidth;
if (translation > sliderOffsetWidth) {
translation = sliderOffsetWidth;
}
if (translation < 0) {
translation = 0;
}

if (delta >= sliderOffsetWidth) {
completion = 1;
} else if (delta <= 0) {
completion = 0;
} else {
completion = delta / sliderOffsetWidth;
}

me.p.onDrag(completion);

me.p.slider.style.transform = `translate3d(${translation}px, 0, 0)`;
if (me.p.slideContent) {
me.p.content.style.transform = me.p.slider.style.transform;
Expand Down Expand Up @@ -164,22 +177,34 @@ OSREC.superslide = function (p) {
me.p.content.style.transition = 'none';
me.p.slider.style.transition = 'none';

var sliderStartPosition = me.p.isOpen ? -me.p.slider.offsetWidth : 0;
var sliderOffsetWidth = me.p.slider.offsetWidth;
var sliderStartPosition = me.p.isOpen ? -sliderOffsetWidth : 0;
var translation = 0;
var delta = 0;
var completion = 0;

var touchMoveFunction = function (e) {

delta = e.changedTouches[0].clientX - startX;
translation = sliderStartPosition + delta;

if (translation < -me.p.slider.offsetWidth) {
translation = -me.p.slider.offsetWidth;
if (translation < -sliderOffsetWidth) {
translation = -sliderOffsetWidth;
}
if (translation > 0) {
translation = 0;
}

if ((delta * -1) >= sliderOffsetWidth) {
completion = 1;
} else if ((delta * -1) <= 0) {
completion = 0;
} else {
completion = (delta * -1) / sliderOffsetWidth;
}

me.p.onDrag(completion);

me.p.slider.style.transform = `translate3d(${translation}px, 0, 0)`;
if (me.p.slideContent) {
me.p.content.style.transform = me.p.slider.style.transform;
Expand Down Expand Up @@ -401,4 +426,4 @@ if (hasDefine) {
// Assign to the global object
// This makes sure that the object really is assigned to the global scope
root.OSREC = OSREC;
}
}
2 changes: 1 addition & 1 deletion superslide-std.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.