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 some feture #5

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
92 changes: 49 additions & 43 deletions zepto.onepagescroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
*
* Credit: Eike Send for the awesome swipe event
* https://github.com/peachananr/zepto-onepage-scroll
*
*
* Zepto Modules Required:
*
* Zepto Modules Required:
* - touch.js
* - fx.js
*
Expand All @@ -23,20 +23,21 @@
;(function($){
$.extend($.fn, {
onePageScroll: function(options){

var defaults = {
sectionContainer: "section",
easing: "ease",
animationTime: 1000,
pagination: true,
threshold: 100,
pagination: true,
updateURL: false,
keyboard: true,
beforeMove: null,
afterMove: null,
loop: false,
responsiveFallback: false
};

var settings = $.extend({}, defaults, options),
el = $(this),
sections = $(settings.sectionContainer),
Expand All @@ -48,21 +49,21 @@
quietPeriod = 300,
paginationList = "",
body = $("body");


/*------------------------------------------------*/
/* Credit: Eike Send for the awesome swipe event */
/* Credit: Eike Send for the awesome swipe event */
/*------------------------------------------------*/

$.fn.swipeEvents = function() {
return this.each(function() {

var startX,
startY,
$this = $(this);

$this.on('touchstart', touchstart);

function touchstart(event) {
var touches = event.touches;
if (touches && touches.length) {
Expand All @@ -71,14 +72,14 @@
$this.on('touchmove', touchmove);
}
}

function touchmove(event) {
var touches = event.touches;
if (touches && touches.length) {
event.preventDefault();
var deltaX = startX - touches[0].pageX;
var deltaY = startY - touches[0].pageY;

if (deltaX >= 50) {
$this.trigger("swipeLeft");
}
Expand All @@ -96,45 +97,45 @@
}
}
}

});
};

/*---------------------------------*/
/* Function to transform the page */
/*---------------------------------*/

$.fn.transformPage = function(settings, pos, index, next_el) {
var el2 = $(this);

if (typeof settings.beforeMove == 'function') settings.beforeMove(index, next_el);

el2.animate({
translate3d: "0, " + pos + "%, 0"
}, settings.animationTime, settings.easing, function() {
if (typeof settings.afterMove == 'function') settings.afterMove(index);
});
}

/*---------------------------------*/
/* Function to move down section */
/*---------------------------------*/

$.fn.moveDown = function() {
var timeNow = new Date().getTime();

// Cancel scroll if currently animating or within quiet period
if(timeNow - lastAnimation < quietPeriod + settings.animationTime) {
event.preventDefault();
return;
}

var index = $(settings.sectionContainer +".active").data("index"),
current = $(settings.sectionContainer + "[data-index='" + index + "']"),
next = $(settings.sectionContainer + "[data-index='" + (parseInt(index) + 1) + "']"),
el3 = $(this);


if(next.length < 1) {
if (settings.loop == true) {
pos = 0;
Expand Down Expand Up @@ -165,7 +166,7 @@
el3.transformPage(settings, pos, next_index, next);
lastAnimation = timeNow;
}

/*---------------------------------*/
/* Function to move up section */
/*---------------------------------*/
Expand All @@ -178,7 +179,7 @@
event.preventDefault();
return;
}

var index = $(settings.sectionContainer +".active").data("index"),
current = $(settings.sectionContainer + "[data-index='" + index + "']"),
next = $(settings.sectionContainer + "[data-index='" + (parseInt(index) - 1) + "']"),
Expand Down Expand Up @@ -212,14 +213,14 @@
el4.transformPage(settings, pos, next_index, next);
lastAnimation = timeNow;
}

/*-------------------------------------------*/
/* Function to move to specified section */
/*-------------------------------------------*/

$.fn.moveTo = function(page_index) {
var timeNow = new Date().getTime();

// Cancel scroll if currently animating or within quiet period
if(timeNow - lastAnimation < quietPeriod + settings.animationTime) {
event.preventDefault();
Expand Down Expand Up @@ -249,7 +250,7 @@
lastAnimation = timeNow;
}
}

/*-------------------------------------------*/
/* Responsive Fallback trigger */
/*-------------------------------------------*/
Expand All @@ -268,11 +269,11 @@
bodyremoveClass("disabled-onepage-scroll");
$("html, body, .wrapper").animate({ scrollTop: 0 }, "fast");
}
$(document).swipeEvents().on("swipeDown", function(event){

$(document).swipeEvents().on("swipeDown", function(event){
if (!$("body").hasClass("disabled-onepage-scroll")) event.preventDefault();
el.moveUp();
}).on("swipeUp", function(event){
}).on("swipeUp", function(event){
if (!$("body").hasClass("disabled-onepage-scroll")) event.preventDefault();
el.moveDown();
});
Expand All @@ -282,22 +283,22 @@

}
}

/*-------------------------------------------*/
/* Initialize scroll detection */
/*-------------------------------------------*/

function init_scroll(event, delta) {
var deltaOfInterest = delta;

if (deltaOfInterest < 0) {
el.moveDown()
} else {
el.moveUp()
}
}


/*-------------------------------------------*/
/* Prepare Everything */
/*-------------------------------------------*/
Expand All @@ -313,10 +314,10 @@
}
});

$(document).swipeEvents().on("swipeDown", function(event){
$(document).swipeEvents().on("swipeDown", function(event){
if (!$("body").hasClass("disabled-onepage-scroll")) event.preventDefault();
el.moveUp();
}).on("swipeUp", function(event){
}).on("swipeUp", function(event){
if (!$("body").hasClass("disabled-onepage-scroll")) event.preventDefault();
el.moveDown();
});
Expand All @@ -342,7 +343,7 @@
if(settings.pagination == true) $(".onepage-pagination li a" + "[data-index='" + init_index + "']").addClass("active");

document.body.className = document.body.className.replace(/\bviewing-page-\d.*?\b/g, '');

body.addClass("viewing-page-" + next_index)
if (history.replaceState && settings.updateURL == true) {
var href = window.location.href.substr(0,window.location.href.indexOf('#')) + "#" + (init_index);
Expand Down Expand Up @@ -372,9 +373,14 @@
}

function mouseWheelHandler(event) {
event.preventDefault();
var delta = event.wheelDelta || -event.detail;
if(!body.hasClass("disabled-onepage-scroll")) init_scroll(event, delta);
if (!($(event.target).hasClass('disabled-scroll'))) {
event.preventDefault();
var delta = event.wheelDelta || -event.detail;
if (Math.abs(event.wheelDelta) < settings.threshold) {
return;
}
if(!body.hasClass("disabled-onepage-scroll")) init_scroll(event, delta);
}
}

$(document).on('mousewheel DOMMouseScroll', mouseWheelHandler);
Expand Down Expand Up @@ -409,7 +415,7 @@
}
return false;
}


})
})(Zepto)
Loading