Skip to content
This repository has been archived by the owner on Jan 11, 2020. It is now read-only.

Commit

Permalink
Enhance detection of mobile browser scrolling #80
Browse files Browse the repository at this point in the history
  • Loading branch information
prinsss committed Aug 5, 2017
1 parent 7f8aa60 commit 50d9b4b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
46 changes: 46 additions & 0 deletions resources/assets/src/js/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,51 @@ function getQueryString(key, defaultValue) {
}
}

/**
* Check if the `resize` event is fired by scrolling on a mobile browser
* whose address bar (e.g. Chrome) will hide automatically when scrolling.
*
* @return {Boolean}
*/
function isMobileBrowserScrolling() {
let currentWindowWidth = $(window).width();
let currentWindowHeight = $(window).height();

if ($.cachedWindowWidth === undefined) {
$.cachedWindowWidth = currentWindowWidth;
}

if ($.cachedWindowHeight === undefined) {
$.cachedWindowHeight = currentWindowHeight;
}

let isWidthChanged = (currentWindowWidth !== $.cachedWindowWidth);
let isHeightChanged = (currentWindowHeight !== $.cachedWindowHeight);

// If the window width & height changes simultaneously, the resize can't be fired by scrolling.
if (isWidthChanged && isHeightChanged) {
return false;
}

// If only width was changed, it also can't be.
if (isWidthChanged) {
return false;
}

// If width didn't change but height changed ?
if (isHeightChanged) {
let last = $.lastWindowHeight;
$.lastWindowHeight = currentWindowHeight;

if (last === undefined || currentWindowHeight == last) {
return true;
}
}

// If both width & height did not change
return false;
}

/**
* Return a debounced function
*
Expand Down Expand Up @@ -101,5 +146,6 @@ if (typeof require !== 'undefined' && typeof module !== 'undefined') {
isEmpty,
debounce,
getQueryString,
isMobileBrowserScrolling,
};
}
6 changes: 1 addition & 5 deletions resources/views/index.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@

<script type="text/javascript">

var cachedWindowWidth = $(window).width();

function changeWrapperHeight() {
var btn = $('p a.button');
var bottom = btn.offset().top + btn.height() + 80;
Expand All @@ -153,9 +151,7 @@
.scroll(changeHeaderTransparency)
.ready(changeWrapperHeight)
.resize(function () {
if ($(window).width() !== cachedWindowWidth) {
changeWrapperHeight();
}
isMobileBrowserScrolling() ? null : changeWrapperHeight();
});
</script>
</body>
Expand Down

0 comments on commit 50d9b4b

Please sign in to comment.