Skip to content

Commit

Permalink
Change: pause scrolling on touch
Browse files Browse the repository at this point in the history
  • Loading branch information
joe223 committed Oct 27, 2019
1 parent 0737fcd commit 75aac93
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 16 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Tiny-Swiper
<p>
<a href="./docs/logo.png" target="_blank"><img width="240" src="./docs/logo.png"></a>
</p>

[![](https://badge.fury.io/js/tiny-swiper.svg)](https://www.npmjs.com/package/tiny-swiper)
[![npm bundle size](https://img.shields.io/bundlephobia/minzip/tiny-swiper)](https://www.npmjs.com/package/tiny-swiper)
Expand All @@ -7,6 +9,8 @@

📦 2kb gzipped library alternative to SwiperJS with the same modern API.

***

## Getting start

- [Getting Started Guide](#guide-to-usage)
Expand Down
Binary file added docs/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 30 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@
function detectTouch() {
return Boolean('ontouchstart' in window || window.navigator.maxTouchPoints > 0 || window.navigator.msMaxTouchPoints > 0 || window.DocumentTouch && document instanceof DocumentTouch);
}
/**
* get Element transform
* @param {HTMLElement} el
* @param {Boolean} isHorizontal
* @returns {Number}
*/

function getTranslate(el, isHorizontal) {
var matrix = getComputedStyle(el).transform.replace(/[a-z]|\(|\)|\s/g, '').split(',').map(parseFloat);
var arr;

if (matrix.length === 16) {
arr = matrix.slice(12, 14);
} else if (matrix.length === 6) {
arr = matrix.slice(4, 6);
}

return arr[isHorizontal ? 0 : 1] || 0;
}

/**
* Swiper Class
Expand Down Expand Up @@ -201,7 +220,7 @@
}
}

var oldTransform = -this.index * this.boxSize;
var oldTransform = getTranslate(this.$wrapper, this.isHorizontal);
this.$wrapper.style.transform = this.getTransform(oldTransform + px);
};

Expand Down Expand Up @@ -243,7 +262,7 @@
_proto.initStatus = function initStatus() {
this.touchStatus = {
touchTracks: [],
offset: 0,
startOffset: 0,
touchStartTime: 0,
isTouchStart: false,
isScrolling: false,
Expand All @@ -258,10 +277,14 @@
supportTouch = this.supportTouch;

var handleTouchStart = function handleTouchStart(e) {
_this3.initStatus();

var touchStatus = _this3.touchStatus;

var shouldPreventDefault = _this3.config.touchStartPreventDefault && _this3.formEls.indexOf(e.target.nodeName) === -1 || _this3.config.touchStartForcePreventDefault;

touchStatus.startOffset = getTranslate(_this3.$wrapper, _this3.isHorizontal);
_this3.$wrapper.style.transform = _this3.getTransform(touchStatus.startOffset);
_this3.$wrapper.style.transition = 'none';
touchStatus.isTouchStart = true;
touchStatus.touchStartTime = Date.now();
Expand All @@ -287,32 +310,33 @@
};
touchStatus.touchTracks.push(currentPosition);
var touchAngle = Math.atan2(Math.abs(diff.y), Math.abs(diff.x)) * 180 / Math.PI;
var offset = 0;

if (_this3.isHorizontal) {
if (touchAngle < config.touchAngle || touchStatus.isTouching) {
touchStatus.isTouching = true;
touchStatus.offset += diff.x;
offset = diff.x;
e.preventDefault();
} else {
touchStatus.isScrolling = true;
}
} else {
if (90 - touchAngle < config.touchAngle || touchStatus.isTouching) {
touchStatus.isTouching = true;
touchStatus.offset += diff.y;
offset = diff.y;
e.preventDefault();
} else {
touchStatus.isScrolling = true;
}
}

_this3.scrollPixel(touchStatus.offset * config.touchRatio);
_this3.scrollPixel(offset * config.touchRatio);
};

var handleTouchEnd = function handleTouchEnd() {
var touchStatus = _this3.touchStatus;
var swipTime = Date.now() - touchStatus.touchStartTime;
var computedOffset = touchStatus.offset * _this3.config.touchRatio;
var computedOffset = getTranslate(_this3.$wrapper, _this3.isHorizontal) - touchStatus.startOffset;
_this3.$wrapper.style.transition = "transform ease " + config.speed + "ms"; // long swip

if (swipTime > _this3.config.longSwipesMs) {
Expand Down
2 changes: 1 addition & 1 deletion lib/index.min.js

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

0 comments on commit 75aac93

Please sign in to comment.