@@ -35,52 +35,60 @@ export function useLayout(props: WaterfallProps, colWidth: Ref<number>, cols: Re
3535 const animation = addAnimation ( props )
3636
3737 // 排版
38- const layoutHandle = async ( ) => {
38+ const layoutHandle = async ( ) : Promise < boolean > => {
39+ return new Promise ( ( resolve ) => {
3940 // 初始化y集合
40- initY ( )
41-
42- // 构造列表
43- const items : HTMLElement [ ] = [ ]
44- if ( waterfallWrapper && waterfallWrapper . value ) {
45- waterfallWrapper . value . childNodes . forEach ( ( el : any ) => {
46- if ( el ! . className === 'waterfall-item' )
47- items . push ( el )
48- } )
49- }
41+ initY ( )
42+
43+ // 构造列表
44+ const items : HTMLElement [ ] = [ ]
45+ if ( waterfallWrapper && waterfallWrapper . value ) {
46+ waterfallWrapper . value . childNodes . forEach ( ( el : any ) => {
47+ if ( el ! . className === 'waterfall-item' )
48+ items . push ( el )
49+ } )
50+ }
51+
52+ // 获取节点
53+ if ( items . length === 0 ) return false
5054
51- // 获取节点
52- if ( items . length === 0 ) return false
55+ // 遍历节点
56+ for ( let i = 0 ; i < items . length ; i ++ ) {
57+ const curItem = items [ i ] as HTMLElement
58+ // 最小的y值
59+ const minY = Math . min . apply ( null , posY . value )
60+ // 最小y的下标
61+ const minYIndex = posY . value . indexOf ( minY )
62+ // 当前下标对应的x
63+ const curX = getX ( minYIndex )
5364
54- // 遍历节点
55- for ( let i = 0 ; i < items . length ; i ++ ) {
56- const curItem = items [ i ] as HTMLElement
57- // 最小的y值
58- const minY = Math . min . apply ( null , posY . value )
59- // 最小y的下标
60- const minYIndex = posY . value . indexOf ( minY )
61- // 当前下标对应的x
62- const curX = getX ( minYIndex )
65+ // 设置x,y,width
66+ const style = curItem . style as CssStyleObject
6367
64- // 设置x,y,width
65- const style = curItem . style as CssStyleObject
68+ // 设置偏移
69+ if ( transform ) style [ transform ] = `translate3d(${ curX } px,${ minY } px, 0)`
70+ style . width = `${ colWidth . value } px`
6671
67- // 设置偏移
68- if ( transform ) style [ transform ] = `translate3d(${ curX } px,${ minY } px, 0)`
69- style . width = `${ colWidth . value } px`
72+ style . visibility = 'visible'
7073
71- // 更新当前index的y值
72- const { height } = curItem . getBoundingClientRect ( )
73- posY . value [ minYIndex ] += height + props . gutter
74+ // 更新当前index的y值
75+ const { height } = curItem . getBoundingClientRect ( )
76+ posY . value [ minYIndex ] += height + props . gutter
7477
75- // 添加入场动画
76- animation ( curItem , ( ) => {
78+ // 添加入场动画
79+ animation ( curItem , ( ) => {
7780 // 添加动画时间
78- const time = props . animationDuration / 1000
79- if ( transition ) style [ transition ] = `transform ${ time } s`
80- } )
81- }
81+ const time = props . posDuration / 1000
82+ if ( transition ) style [ transition ] = `transform ${ time } s`
83+ } )
84+ }
85+
86+ wrapperHeight . value = Math . max . apply ( null , posY . value )
8287
83- wrapperHeight . value = Math . max . apply ( null , posY . value )
88+ setTimeout ( ( ) => {
89+ resolve ( true )
90+ } , props . posDuration )
91+ } )
8492 }
8593
8694 return {
@@ -97,7 +105,9 @@ function addAnimation(props: WaterfallProps) {
97105 const durationSec = `${ props . animationDuration / 1000 } s`
98106 const delaySec = `${ props . animationDelay / 1000 } s`
99107 const style = content . style as CssStyleObject
100- style . visibility = 'visible'
108+ addClass ( content , props . animationPrefix )
109+ addClass ( content , props . animationEffect )
110+
101111 if ( duration )
102112 style [ duration ] = durationSec
103113
@@ -107,9 +117,6 @@ function addAnimation(props: WaterfallProps) {
107117 if ( fillMode )
108118 style [ fillMode ] = 'both'
109119
110- addClass ( content , props . animationPrefix )
111- addClass ( content , props . animationEffect )
112-
113120 if ( callback ) {
114121 setTimeout ( ( ) => {
115122 callback ( )
0 commit comments