File tree 2 files changed +58
-0
lines changed
2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 12
12
13
13
ionic . DomUtil = {
14
14
15
+ /*
16
+ * Find an element's offset, then add it to the offset of the parent
17
+ * until we are at the direct child of parentEl
18
+ * use-case: find scroll offset of any element within a scroll container
19
+ */
20
+ getPositionInParent : function ( el , parentEl ) {
21
+ var left = 0 , top = 0 ;
22
+ while ( el && el !== parentEl ) {
23
+ left += el . offsetLeft ;
24
+ top += el . offsetTop ;
25
+ el = el . parentNode ;
26
+ }
27
+ return {
28
+ left : left ,
29
+ top : top
30
+ } ;
31
+ } ,
32
+
15
33
ready : function ( cb ) {
16
34
if ( document . readyState === "complete" ) {
17
35
window . rAF ( cb ) ;
Original file line number Diff line number Diff line change
1
+
2
+ describe ( 'js/utils/dom' , function ( ) {
3
+
4
+ describe ( 'getPositionInParent' , function ( ) {
5
+ it ( 'should return 0,0 if el===parentEl' , function ( ) {
6
+ var el = { } ;
7
+ expect ( ionic . DomUtil . getPositionInParent ( el , el ) ) . toEqual ( {
8
+ left : 0 ,
9
+ top : 0
10
+ } ) ;
11
+ } ) ;
12
+ it ( 'should return 0,0 if el is null' , function ( ) {
13
+ expect ( ionic . DomUtil . getPositionInParent ( null , null ) ) . toEqual ( {
14
+ left : 0 ,
15
+ top : 0
16
+ } ) ;
17
+ } ) ;
18
+ it ( 'should return element offset{Top,Left} of el if el is parent\'s child' , function ( ) {
19
+ var parent = { } ;
20
+ var el = { parentNode : parent , offsetLeft : 3 , offsetTop : 2 } ;
21
+ expect ( ionic . DomUtil . getPositionInParent ( el , parent ) ) . toEqual ( {
22
+ left : 3 ,
23
+ top : 2
24
+ } ) ;
25
+ } ) ;
26
+ it ( 'should return added element offset{Top,Left} of all children up to parent' , function ( ) {
27
+ var parent = { } ;
28
+ var child1 = { parentNode : parent , offsetLeft : 5 , offsetTop : 6 } ;
29
+ var child2 = { parentNode : child1 , offsetLeft : 10 , offsetTop : 11 } ;
30
+ expect ( ionic . DomUtil . getPositionInParent ( child1 , parent ) ) . toEqual ( {
31
+ left : 5 ,
32
+ top : 6
33
+ } ) ;
34
+ expect ( ionic . DomUtil . getPositionInParent ( child2 , parent ) ) . toEqual ( {
35
+ left : 15 ,
36
+ top : 17
37
+ } ) ;
38
+ } ) ;
39
+ } ) ;
40
+ } ) ;
You can’t perform that action at this time.
0 commit comments