mirror of https://github.com/twbs/ratchet.git
Build mobile apps with simple HTML, CSS, and JS components.
http://goratchet.com/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
3.2 KiB
117 lines
3.2 KiB
12 years ago
|
/* ----------------------------------
|
||
12 years ago
|
* SLIDER v1.0.1
|
||
12 years ago
|
* Licensed under The MIT License
|
||
|
* Adapted from Brad Birdsall's swipe
|
||
|
* http://opensource.org/licenses/MIT
|
||
|
* ---------------------------------- */
|
||
|
|
||
|
!function () {
|
||
|
|
||
|
var pageX;
|
||
|
var pageY;
|
||
|
var slider;
|
||
|
var deltaX;
|
||
|
var deltaY;
|
||
|
var offsetX;
|
||
|
var lastSlide;
|
||
|
var startTime;
|
||
|
var resistance;
|
||
|
var sliderWidth;
|
||
|
var slideNumber;
|
||
|
var isScrolling;
|
||
|
var scrollableArea;
|
||
|
|
||
|
var getSlider = function (target) {
|
||
11 years ago
|
var i, sliders = document.querySelectorAll('.slider > .slide-group');
|
||
12 years ago
|
for (; target && target !== document; target = target.parentNode) {
|
||
|
for (i = sliders.length; i--;) { if (sliders[i] === target) return target; }
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var getScroll = function () {
|
||
|
var translate3d = slider.style.webkitTransform.match(/translate3d\(([^,]*)/);
|
||
|
return parseInt(translate3d ? translate3d[1] : 0)
|
||
|
};
|
||
|
|
||
|
var setSlideNumber = function (offset) {
|
||
|
var round = offset ? (deltaX < 0 ? 'ceil' : 'floor') : 'round';
|
||
|
slideNumber = Math[round](getScroll() / ( scrollableArea / slider.children.length) );
|
||
|
slideNumber += offset;
|
||
|
slideNumber = Math.min(slideNumber, 0);
|
||
|
slideNumber = Math.max(-(slider.children.length - 1), slideNumber);
|
||
|
}
|
||
|
|
||
|
var onTouchStart = function (e) {
|
||
|
slider = getSlider(e.target);
|
||
|
|
||
|
if (!slider) return;
|
||
|
|
||
11 years ago
|
var firstItem = slider.querySelector('.slide');
|
||
12 years ago
|
|
||
|
scrollableArea = firstItem.offsetWidth * slider.children.length;
|
||
|
isScrolling = undefined;
|
||
|
sliderWidth = slider.offsetWidth;
|
||
|
resistance = 1;
|
||
|
lastSlide = -(slider.children.length - 1);
|
||
|
startTime = +new Date;
|
||
|
pageX = e.touches[0].pageX;
|
||
|
pageY = e.touches[0].pageY;
|
||
11 years ago
|
deltaX = 0;
|
||
|
deltaY = 0;
|
||
12 years ago
|
|
||
|
setSlideNumber(0);
|
||
|
|
||
|
slider.style['-webkit-transition-duration'] = 0;
|
||
|
};
|
||
|
|
||
|
var onTouchMove = function (e) {
|
||
|
if (e.touches.length > 1 || !slider) return; // Exit if a pinch || no slider
|
||
|
|
||
|
deltaX = e.touches[0].pageX - pageX;
|
||
|
deltaY = e.touches[0].pageY - pageY;
|
||
|
pageX = e.touches[0].pageX;
|
||
|
pageY = e.touches[0].pageY;
|
||
|
|
||
|
if (typeof isScrolling == 'undefined') {
|
||
|
isScrolling = Math.abs(deltaY) > Math.abs(deltaX);
|
||
|
}
|
||
|
|
||
|
if (isScrolling) return;
|
||
|
|
||
|
offsetX = (deltaX / resistance) + getScroll();
|
||
|
|
||
|
e.preventDefault();
|
||
|
|
||
|
resistance = slideNumber == 0 && deltaX > 0 ? (pageX / sliderWidth) + 1.25 :
|
||
|
slideNumber == lastSlide && deltaX < 0 ? (Math.abs(pageX) / sliderWidth) + 1.25 : 1;
|
||
|
|
||
|
slider.style.webkitTransform = 'translate3d(' + offsetX + 'px,0,0)';
|
||
|
};
|
||
|
|
||
|
var onTouchEnd = function (e) {
|
||
|
if (!slider || isScrolling) return;
|
||
|
|
||
|
setSlideNumber(
|
||
|
(+new Date) - startTime < 1000 && Math.abs(deltaX) > 15 ? (deltaX < 0 ? -1 : 1) : 0
|
||
|
);
|
||
|
|
||
|
offsetX = slideNumber * sliderWidth;
|
||
|
|
||
|
slider.style['-webkit-transition-duration'] = '.2s';
|
||
|
slider.style.webkitTransform = 'translate3d(' + offsetX + 'px,0,0)';
|
||
|
|
||
|
e = new CustomEvent('slide', {
|
||
|
detail: { slideNumber: Math.abs(slideNumber) },
|
||
|
bubbles: true,
|
||
|
cancelable: true
|
||
|
});
|
||
|
|
||
|
slider.parentNode.dispatchEvent(e);
|
||
|
};
|
||
|
|
||
|
window.addEventListener('touchstart', onTouchStart);
|
||
|
window.addEventListener('touchmove', onTouchMove);
|
||
|
window.addEventListener('touchend', onTouchEnd);
|
||
|
|
||
|
}();
|