-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.scrollFix.js
More file actions
executable file
·49 lines (44 loc) · 1.33 KB
/
jquery.scrollFix.js
File metadata and controls
executable file
·49 lines (44 loc) · 1.33 KB
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
/**
* jquery.scrollfix v1.0
*
* author: szm
* email: supermin6u@foxmail.com
* github: https://github.com/SUpermin6u/scrollFix
*
* Free to use under the MIT license.
*/
(function($) {
/**
* when scroll to the certain place, will fixed there
* @param {jQuery Selector} $selector selector to do the scrollfix
* @param {Object} absoluteCss style before fixed
* @param {Object} fixedCss style after fixed
*/
var scrollFix = function($selector,absoluteCss,fixedCss){
var isBarFixed = false;
var $bar = $selector;
if(!$bar.length){return;}
var aCss = absoluteCss || {
'position':'absolute',
'top':'auto'
}
var fCss = fixedCss || {
'position':'fixed',
'top':0
}
$bar.css(aCss);
var barOffsetTop = $bar.offset().top;
$(window).scroll(function(){
var top = $('body').scrollTop() || $('html').scrollTop();
if(top > barOffsetTop && !isBarFixed){
$bar.css(fCss);
isBarFixed = true;
}
if(top <= barOffsetTop&& isBarFixed){
$bar.css(aCss);
isBarFixed = false;
}
});
$(window).scroll();
}
})(jQuery);