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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
/*! iOSList - v2.0.0 - * https://brianhadaway.github.io/iOSList
* Copyright (c) 2014 Brian Hadaway; Licensed MIT */
(function($, window, document, undefined) {
var IosList = function(elem, options) {
this.$elem = $(elem);
this.$elem.data('instance', this);
this.init(options);
};
IosList.prototype = {
defaults: {
classes: {
animated: 'ioslist-animated',
container: 'ioslist-wrapper',
hidden: 'ioslist-hidden',
stationaryHeader: 'ioslist-fake-header'
},
selectors: {
groupContainer: '.ioslist-group-container',
groupHeader: '.ioslist-group-header',
stationaryHeader: 'h2'
}
},
init: function(options) {
var scope = this,
isIOS = navigator.userAgent.match(/ipad|iphone|ipod/gi) ? true : false;
//set defaults
this.options = $.extend(true, {}, this.defaults, (options || {}));
this.elems = [];
//indicate that this is an ioslist
this.$elem.addClass('ioslist');
//wrap all the children
this.$elem.children().wrapAll(["<div class='", this.options.classes.container, "' data-ios='", isIOS, "'></div>"].join(''));
this.$elem.prepend(['<', this.options.selectors.stationaryHeader, '/>'].join(''));
this.$listWrapper = this.$elem.find('.' + this.options.classes.container);
this.$fakeHeader = this.$elem.find(this.options.selectors.stationaryHeader).eq(0);
this.$fakeHeader.addClass(this.options.classes.stationaryHeader);
this.$elem.find(this.options.selectors.groupContainer).each(function(index, elem) {
var $tmp_list = scope.$elem.find(scope.options.selectors.groupContainer).eq(index),
$tmp_header = $tmp_list.find(scope.options.selectors.groupHeader).eq(0),
$tmp_listHeight = $tmp_list.height(),
$tmp_listOffset = $tmp_list.position().top;
scope.elems.push({
'list': $tmp_list,
'header': $tmp_header,
'listHeight': $tmp_listHeight,
'headerText': $tmp_header.text(),
'headerHeight': $tmp_header.outerHeight(),
'listOffset': $tmp_listOffset,
'listBottom': $tmp_listHeight + $tmp_listOffset
});
});
this.$fakeHeader.text(this.elems[0].headerText);
var iScrollPos = 0;
this.$listWrapper.scroll(function() {
scope.testPosition();
});
},
testPosition: function() {
var currentTop = this.$listWrapper.scrollTop(),
topElement, offscreenElement, topElementBottom, i = 0;
while ((this.elems[i].listOffset - currentTop) <= 0) {
topElement = this.elems[i];
topElementBottom = topElement.listBottom - currentTop;
if (topElementBottom < -topElement.headerHeight) {
offscreenElement = topElement;
}
i++;
if (i >= this.elems.length) {
break;
}
}
if (topElementBottom < 0 && topElementBottom > -topElement.headerHeight) {
this.$fakeHeader.addClass(this.options.classes.hidden);
$(topElement.list).addClass(this.options.classes.animated);
} else {
this.$fakeHeader.removeClass(this.options.classes.hidden);
if (topElement) {
$(topElement.list).removeClass(this.options.classes.animated);
}
}
if (topElement) {
this.$fakeHeader.text(topElement.headerText);
}
}
};
$.fn.ioslist = function(options, args) {
if (typeof options === 'string') {
return this.each(function() {
$(this).data('instance')[options](args);
});
} else {
return this.each(function() {
new IosList(this, options);
});
}
};
})(jQuery, window, document);
$(function(){
$("#list1").ioslist();
});
|