How it's expected to work
I created a scollable div for a project. The position of the scrollbar is calculated width jQuery. In a desktop environment there is a visible slider. On mobile devices there is not but it's still possible to swipe left to get more content.
(It does not yet update on window resize. Reload is required.)
Expected: "overflow_diff" should be equal to "Scoll pos". "Percentage" should be 100 when scolled to the end.
What works
It works fine in Chrome and Firefox on desktop and on Chrome on Android.
What does not work - My question
Firefox on Android does the calculations all wrong. Strange is that it works on desktop. If it's a bug in Firefox, how do I get around it?
Full example
http://xn--trnell-wxa.se/scrolltest2/index.php
HTML header
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
CSS
* {
padding: 0;
margin: 0;
}
.wrap {
padding: 40px;
background: #eee;
width: 75%;
}
.cropped {
overflow-x: auto;
}
.overflow {
width: 300%;
background: #ddd;
height: 100px;
}
JS
jQuery(window).ready(function($) {
var cropped_width = $('.cropped').width();
var overflow_width = $('.overflow').width();
var overflow_diff = overflow_width - cropped_width;
$( '.log' ).append( 'Overflow: ' + overflow_width + '<br>' );
$( '.log' ).append( 'Cropped: ' + cropped_width + '<br>' );
$( '.log' ).append( 'Overflow diff: ' + overflow_diff + '<br>' );
$( '.log' ).append( '<div class="scrolled"></div>' );
$( '.log' ).append( '<div class="scrolled2"></div>' );
$( '.cropped' ).on('scroll touchmove', function() {
var scroll_pos = $( this ).scrollLeft();
var percentage = Math.round( scroll_pos / overflow_diff * 100 );
$( '.log .scrolled' ).html( 'Scroll pos: ' + scroll_pos + '<br>Percentage: ' + percentage + '<br>' );
});
});
HTML
<div class="wrap">
<div class="cropped">
<div class="overflow">Swipe to the end</div>
</div>
</div>
<div class="log"></div>