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

Trigger native scrolling support for mobile devices #6

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions css/trackpad-scroll-emulator.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
width: 200px; /* Default value. Overwite this if you want. */
height: 300px; /* Default value. Overwite this if you want. */
overflow: hidden;
-webkit-overflow-scrolling: touch; /* Trigger native scrolling for mobile, if not supported, plugin is used. */
}
.tse-scrollable .tse-scroll-content {
overflow: hidden;
Expand Down
47 changes: 47 additions & 0 deletions jquery.trackpad-scroll-emulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
* Initialize plugin
*/
function init() {
if(hasOverflowScrolling()) {
$el.css('overflow', 'auto');

return;
}

if ($el.hasClass('horizontal')){
scrollDirection = 'horiz';
scrollOffsetAttr = 'scrollLeft';
Expand Down Expand Up @@ -270,6 +276,47 @@
}
}

/**
* Check for mobile Overflow scrolling support
* Source: https://gist.github.com/hay/4032527
*/
function hasOverflowScrolling() {
var prefixes = ['webkit', 'moz', 'o', 'ms'];
var div = document.createElement('div');
var body = document.getElementsByTagName('body')[0];
var hasIt = false;

body.appendChild(div);

for (var i = 0; i < prefixes.length; i++) {
var prefix = prefixes[i];
div.style[prefix + 'OverflowScrolling'] = 'touch';
}

// And the non-prefixed property
div.style.overflowScrolling = 'touch';

// Now check the properties
var computedStyle = window.getComputedStyle(div);

// First non-prefixed
hasIt = !!computedStyle.overflowScrolling;

// Now prefixed...
for (var i = 0; i < prefixes.length; i++) {
var prefix = prefixes[i];
if (!!computedStyle[prefix + 'OverflowScrolling']) {
hasIt = true;
break;
}
}

// Cleanup old div elements
div.parentNode.removeChild(div);

return hasIt;
}

init();

return {
Expand Down