Skip to content
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ Impetus will register itself as an AMD module if it's available.
<td><code>true</code></td>
<td>Whether to stretch and rebound values when pulled outside the bounds.</td>
</tr>
<tr>
<th scope="row" align="left"><code>axis</code></th>
<td><code>String|Boolean</code></td>
<td><code>true</code></td>
<td>Whether to lock scrolling to a given axis ("x" or "y"), to unlock both (true), or to completely prevent scrolling (false).</td>
</tr>
</tbody>
</table>

Expand All @@ -93,6 +99,10 @@ Impetus will register itself as an AMD module if it's available.
</tr>
</thead>
<tbody>
<tr>
<th scope="row" align="left"><code>.getAxis()</code></th>
<td>Retrieve the current the locked axis</td>
</tr>
<tr>
<th scope="row" align="left"><code>.pause()</code></th>
<td>Disable movement processing.</td>
Expand All @@ -117,6 +127,10 @@ Impetus will register itself as an AMD module if it's available.
<th scope="row" align="left"><code>.setBoundY( &lt;number[2]&gt; )</code></th>
<td>Adjust the Y bound</td>
</tr>
<tr>
<th scope="row" align="left"><code>.setAxis( &lt;string|boolean&gt; )</code></th>
<td>Change the locked axis</td>
</tr>
<tr>
<th scope="row" align="left"><code>.destroy()</code></th>
<td>
Expand Down
44 changes: 30 additions & 14 deletions dist/impetus.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
var Impetus = function Impetus(_ref) {
var _ref$source = _ref.source;
var sourceEl = _ref$source === undefined ? document : _ref$source;
var _ref$axis = _ref.axis;
var axis = _ref$axis === undefined ? false : _ref$axis;
var updateCallback = _ref.update;
var _ref$multiplier = _ref.multiplier;
var multiplier = _ref$multiplier === undefined ? 1 : _ref$multiplier;
Expand Down Expand Up @@ -163,6 +165,16 @@
boundYmax = boundY[1];
};

/**
* Set the axis that will be scrolled
* @public
* @param {String|Boolean} axis
*/
this.setAxis = function (val) {
var lowerCaseVal = (val || '').toLowerCase();
axis = lowerCaseVal === 'x' ? 'x' : lowerCaseVal === 'y' ? 'y' : false;
};

/**
* Executes the update function
*/
Expand Down Expand Up @@ -204,8 +216,8 @@
decelerating = false;
pointerId = event.id;

pointerLastX = pointerCurrentX = event.x;
pointerLastY = pointerCurrentY = event.y;
pointerLastX = pointerCurrentX = axis !== 'y' ? event.x : pointerLastX;
pointerLastY = pointerCurrentY = axis !== 'x' ? event.y : pointerLastY;
trackingPoints = [];
addTrackingPoint(pointerLastX, pointerLastY);

Expand All @@ -227,8 +239,8 @@
var event = normalizeEvent(ev);

if (pointerActive && event.id === pointerId) {
pointerCurrentX = event.x;
pointerCurrentY = event.y;
pointerCurrentX = axis !== 'y' ? event.x : pointerCurrentX;
pointerCurrentY = axis !== 'x' ? event.y : pointerCurrentY;
addTrackingPoint(pointerLastX, pointerLastY);
requestTick();
}
Expand Down Expand Up @@ -282,8 +294,8 @@
* Calculate new values, call update function
*/
function updateAndRender() {
var pointerChangeX = pointerCurrentX - pointerLastX;
var pointerChangeY = pointerCurrentY - pointerLastY;
var pointerChangeX = pointerCurrentX - pointerLastX || 0; // prevent NaN
var pointerChangeY = pointerCurrentY - pointerLastY || 0;

targetX += pointerChangeX * multiplier;
targetY += pointerChangeY * multiplier;
Expand Down Expand Up @@ -333,16 +345,20 @@
var xDiff = 0;
var yDiff = 0;

if (boundXmin !== undefined && targetX < boundXmin) {
xDiff = boundXmin - targetX;
} else if (boundXmax !== undefined && targetX > boundXmax) {
xDiff = boundXmax - targetX;
if (axis !== 'y') {
if (boundXmin !== undefined && targetX < boundXmin) {
xDiff = boundXmin - targetX;
} else if (boundXmax !== undefined && targetX > boundXmax) {
xDiff = boundXmax - targetX;
}
}

if (boundYmin !== undefined && targetY < boundYmin) {
yDiff = boundYmin - targetY;
} else if (boundYmax !== undefined && targetY > boundYmax) {
yDiff = boundYmax - targetY;
if (axis !== 'x') {
if (boundYmin !== undefined && targetY < boundYmin) {
yDiff = boundYmin - targetY;
} else if (boundYmax !== undefined && targetY > boundYmax) {
yDiff = boundYmax - targetY;
}
}

if (restrict) {
Expand Down
2 changes: 1 addition & 1 deletion dist/impetus.min.js

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

2 changes: 1 addition & 1 deletion docs/impetus.min.js

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

Loading