-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery.sticky.scroller.js
53 lines (40 loc) · 1.41 KB
/
jquery.sticky.scroller.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
/**
*
* StickyScroller
*
* Copyright 2012, Michael Wright
* Free to use and abuse under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*
* http://github.com/michaelw90/stickyscroller/
*
**/
(function($) {
var stickScrollerCount = 0;
$.fn.stickyScroller = function(params){
var params = $.extend({}, $.fn.stickyScroller.defaults, params);
var $el = $(this);
var $win = $(window);
var $position = $el.offset().top;
var $id = stickScrollerCount;
if(params.wrap){
$el.wrap('<div id="scrollMaster-' + $id + '" style="width: 100%;"' + (params.wrapClass != ''? ' class="' + params.wrapClass + '"' : "") + '>');
$el = $('#scrollMaster-' + $id);
}
$marginTop = $el.next().css('marginTop');
$marginTopStick = parseInt($marginTop.replace(/[^\d]/g, ''))*2 + $el.height();
$marginTopUnstuck = $marginTop;
$win.on('scroll', function(){
if(($win.scrollTop() >= $position && !params.hideAt) || ($win.scrollTop() >= $position && params.hideAt && $win.scrollTop() <= params.hideAt)){
$el.addClass('stickySlide').next().css('marginTop', $marginTopStick);
}else{
$el.removeClass('stickySlide').next().css('marginTop', $marginTopUnstuck);
}
});
}
$.fn.stickyScroller.defaults = {
wrap: true, // Whether to wrap the stick element with an outer div
wrapClass: '', // Class applied to the wrap div
hideAt: 0 // Scroll distance to stop the element sticking
}
})(jQuery);