-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.scrollbox.js
165 lines (136 loc) · 5.02 KB
/
jquery.scrollbox.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (c) 2008 John Allison
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// begin closure
( function( $ ) {
var BUFFER_TIME = 20; // Need a buffer so our CSS attribute reset doesnt interfere
// with the animation modifying the same CSS attributes.
var opts;
var queue = new Array();
var boxes = null;
jQuery.fn.scrollbox = function( options ) {
// Extend out default options with those provided
opts = jQuery.extend( { }, jQuery.fn.scrollbox.defaults, options );
boxes = this;
// Iterate over each matched element
return boxes.each( function( ) {
$this = jQuery( this );
// Check structure - we expect a list element
// <div><ul></ul></div>
// If it doesn't exist, create the list
if ( $this.find( 'ul' ).length == 0 ) {
$this.append( '<ul></ul>' );
}
// modify CSS
$this.css( 'position', 'relative' );
$this.css( 'overflow', 'hidden' );
$this.find( 'ul' ).css( 'position', 'relative' );
$this.find( 'ul' ).css( 'margin', '0px' );
// TODO don't overwrite current values?
setHiddenSpaceSize( $this.find( 'ul' ), 0, 0 );
});
};
jQuery.fn.scrollbox.push = function( items ) {
if ( boxes == null ) return false;
return boxes.each( function( ) {
$this = jQuery( this );
var list = $this.find( 'ul' );
items.each( function( ) {
if ( parseInt( list.css( 'top' ) ) == 0 ) {
// prepend item
list.prepend( this );
// find height of item
var height = jQuery( this ).outerHeight( { margin: true } );
// set top and padding to proper height to hide item
setHiddenSpaceSize( list, -height, 0 );
// animate padding to height of item
list.animate( { paddingTop: height }, opts.speed );
// after animation finishes
// 1) return padding to '0px'
// 2) return positioning to '0px'
// 3) remove any children over the max
// 4) begin animating any queued items
setTimeout( function() {
setHiddenSpaceSize( list, 0, 0 );
if ( list.children( 'li' ).length > opts.maxsize )
jQuery( jQuery( list.children( 'li' ) ).slice( opts.maxsize ) ).remove();
processQueuedItems.call( $this );
}, opts.speed + BUFFER_TIME );
} else {
// We are currently animating, queue up the item
queue.push( jQuery( this ) );
}
});
});
};
jQuery.fn.scrollbox.pop = function( ) {
if ( boxes == null ) return false;
return boxes.each( function( ) {
$this = jQuery( this );
var list = $this.find( 'ul' );
var item = list.children( 'li:first' );
if ( parseInt( list.css( 'top' ) ) == 0 ) {
// find height of top item
var height = item.outerHeight( { margin: true } );
// set top and padding to proper height to hide item
setHiddenSpaceSize( list, -height, height );
// animate padding to '0px'
list.animate( { paddingTop: 0 }, opts.speed );
// after animation finishes
// 1) remove item
// 2) return padding to '0px'
// 3) return positioning to '0px'
// 4) begin animating any queued items
setTimeout( function() {
item.remove();
setHiddenSpaceSize( list, 0, 0 );
processQueuedItems.call( $this );
}, opts.speed + BUFFER_TIME );
} else {
// We are currently animating, queue up the pop command
queue.push( "POP" );
}
});
};
// Set default options for the scrollbox
jQuery.fn.scrollbox.defaults = {
maxsize : 20,
speed : 500
};
// Private functions
function processQueuedItems( ) {
var $this = this;
var list = $this.find( 'ul' );
if ( queue.length > 0 )
if ( parseInt( list.css( 'top' ) ) == 0 ) {
var item = queue.shift();
if ( item == "POP" ) $this.scrollbox.pop();
else $this.scrollbox.push( item );
} else {
setTimeout( function() {
processQueuedItems.call( $this )
}, 100 );
}
};
function setHiddenSpaceSize( list, top, padding ) {
list.css( 'top', top + 'px' );
list.css( 'padding-top', padding + 'px' );
}
// end closure
}) ( jQuery );