@@ -122,13 +122,16 @@ $.widget("ui.menu", {
122
122
activate : function ( event , item ) {
123
123
this . deactivate ( ) ;
124
124
if ( this . _hasScroll ( ) ) {
125
- var offset = item . offset ( ) . top - this . element . offset ( ) . top ,
125
+ var borderTop = parseFloat ( $ . curCSS ( this . element [ 0 ] , "borderTopWidth" , true ) ) || 0 ,
126
+ paddingtop = parseFloat ( $ . curCSS ( this . element [ 0 ] , "paddingTop" , true ) ) || 0 ,
127
+ offset = item . offset ( ) . top - this . element . offset ( ) . top - borderTop - paddingtop ,
126
128
scroll = this . element . attr ( "scrollTop" ) ,
127
- elementHeight = this . element . height ( ) ;
128
- if ( offset < 0 ) {
129
+ elementHeight = this . element . height ( ) ,
130
+ itemHeight = item . height ( ) ;
131
+ if ( offset < 0 ) {
129
132
this . element . attr ( "scrollTop" , scroll + offset ) ;
130
- } else if ( offset > elementHeight ) {
131
- this . element . attr ( "scrollTop" , scroll + offset - elementHeight + item . height ( ) ) ;
133
+ } else if ( offset + itemHeight > elementHeight ) {
134
+ this . element . attr ( "scrollTop" , scroll + offset - elementHeight + itemHeight ) ;
132
135
}
133
136
}
134
137
this . active = item . first ( )
@@ -187,19 +190,16 @@ $.widget("ui.menu", {
187
190
}
188
191
var base = this . active . offset ( ) . top ,
189
192
height = this . element . height ( ) ,
190
- // TODO replace children with nextAll
191
- // TODO replace filter with each, break once close > 0 and use that item as the result
192
- result = this . element . children ( ".ui-menu-item" ) . filter ( function ( ) {
193
- var close = $ ( this ) . offset ( ) . top - base - height + $ ( this ) . height ( ) ;
194
- // TODO replace with check close > 0
195
- return close < 10 && close > - 10 ;
196
- } ) ;
197
-
198
- // TODO try to catch this earlier when scrollTop indicates the last page anyway
199
- if ( ! result . length ) {
200
- result = this . element . children ( ".ui-menu-item" ) . last ( ) ;
201
- }
202
- this . activate ( event , result ) ;
193
+ result ;
194
+ this . active . nextAll ( ".ui-menu-item" ) . each ( function ( ) {
195
+ var close = $ ( this ) . offset ( ) . top - base - height ;
196
+ if ( close >= 0 ) {
197
+ result = $ ( this ) ;
198
+ return false ;
199
+ }
200
+ } ) ;
201
+
202
+ this . activate ( event , result || this . element . children ( ".ui-menu-item" ) . last ( ) ) ;
203
203
} else {
204
204
this . activate ( event , this . element . children ( ".ui-menu-item" )
205
205
// TODO use .first()/.last()
@@ -216,18 +216,17 @@ $.widget("ui.menu", {
216
216
}
217
217
218
218
var base = this . active . offset ( ) . top ,
219
- height = this . element . height ( ) ;
220
- result = this . element . children ( ".ui-menu-item" ) . filter ( function ( ) {
221
- var close = $ ( this ) . offset ( ) . top - base + height - $ ( this ) . height ( ) ;
222
- // TODO improve approximation
223
- return close < 10 && close > - 10 ;
224
- } ) ;
225
-
226
- // TODO try to catch this earlier when scrollTop indicates the last page anyway
227
- if ( ! result . length ) {
228
- result = this . element . children ( ".ui-menu-item" ) . first ( ) ;
229
- }
230
- this . activate ( event , result ) ;
219
+ height = this . element . height ( ) ,
220
+ result ;
221
+ this . active . prevAll ( ".ui-menu-item" ) . each ( function ( ) {
222
+ var close = $ ( this ) . offset ( ) . top - base + height ;
223
+ if ( close <= 0 ) {
224
+ result = $ ( this ) ;
225
+ return false ;
226
+ }
227
+ } ) ;
228
+
229
+ this . activate ( event , result || this . element . children ( ".ui-menu-item" ) . first ( ) ) ;
231
230
} else {
232
231
this . activate ( event , this . element . children ( ".ui-menu-item" )
233
232
// TODO use .first()/.last()
0 commit comments