forked from shrrub-secret/sap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.sap.js
74 lines (60 loc) · 2.38 KB
/
jquery.sap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Sap v1.0.1
*
* Inspired by Contained Sticky Scroll v1.1 By Matt Ward
* http://blog.echoenduring.com/2010/11/15/freebie-contained-sticky-scroll-jquery-plugin/
*
* (c) 2011 Forrst, LLC
*/
(function($) {
$.fn.sap = function(options) {
var defaults = {
distanceFromTheTop: 0,
distanceFromTheBottom: 0
};
options = $.extend(defaults, options);
var $objizzle = $(this);
var oldTop = $objizzle.offset().top;
var width = $objizzle.width() + 'px';
var $shim = $('<div class="sap-shimy-shim"></div>');
var theWindow = $(window);
theWindow.scroll(function() {
var top = theWindow.scrollTop();
var docheight = $(document).height();
var offset = (top + options.distanceFromTheTop + $objizzle.height()) - (docheight - options.distanceFromTheBottom);
var validBottomDistance = ($objizzle.offset().top) < (docheight - options.distanceFromTheBottom);
/* top + options.distanceFromTheTop + objizzle.height() gives the absolute distance to the
* bottom of the object. When this is greater than docheight - options.distanceFromTheBottom
* start decrementing the top value by the offset
*
* If the objizzle.offset().top is greater than docheight - options.distanceFromTheTop, then the user
* has placed in an invalid distanceFromTheTop which places it higher than it currently resides in the
* document. In this instance, keep it position: relative
*/
if ((top + options.distanceFromTheTop) > $objizzle.offset().top && validBottomDistance)
{
$objizzle.css({
position: 'fixed',
width: width,
top: options.distanceFromTheTop + 'px'
});
$shim.css({width: width, height: $objizzle.height()});
$objizzle.before($shim);
}
else if (top + options.distanceFromTheTop < oldTop)
{
$shim.remove();
$objizzle.css({
position: 'relative',
width: width,
top: ''
});
}
if (offset > 0 && validBottomDistance) {
$objizzle.css({
top: options.distanceFromTheTop - offset + 'px'
});
}
});
};
}(jQuery));