Skip to content
Open
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
16 changes: 9 additions & 7 deletions jquery.scrolldepth.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
percentage: true,
userTiming: true,
pixelDepth: true,
nonInteraction: true
nonInteraction: true,
$scrollContainer: $(window),
$scrollDocument: $(document)
};

var $window = $(window),
Expand All @@ -35,7 +37,7 @@
options = $.extend({}, defaults, options);

// Return early if document height is too small
if ( $(document).height() < options.minHeight ) {
if (options.$scrollDocument.height() < options.minHeight) {
return;
}

Expand Down Expand Up @@ -222,15 +224,15 @@
* Scroll Event
*/

$window.on('scroll.scrollDepth', throttle(function() {
options.$scrollContainer.on('scroll.scrollDepth', throttle(function () {
/*
* We calculate document and window height on each scroll event to
* account for dynamic DOM changes.
*/

var docHeight = $(document).height(),
winHeight = window.innerHeight ? window.innerHeight : $window.height(),
scrollDistance = $window.scrollTop() + winHeight,
var docHeight = options.$scrollDocument.height(),
winHeight = options.$scrollContainer.height(),
scrollDistance = options.$scrollContainer.scrollTop() + winHeight,

// Recalculate percentage marks
marks = calculateMarks(docHeight),
Expand All @@ -240,7 +242,7 @@

// If all marks already hit, unbind scroll event
if (cache.length >= 4 + options.elements.length) {
$window.off('scroll.scrollDepth');
options.$scrollContainer.off('scroll.scrollDepth');
return;
}

Expand Down