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
|
import dom from '@/utils/dom'
export default { bind(el){ dom.css(el , { width: '100%', zIndex: '1994214' }) }, inserted( el , {value} ){ if ( !value ) return const nextEl = el.nextElementSibling, nextElPaddingTop = Number.parseFloat( dom.getStyleValue( nextEl , 'paddingTop') ), elOffsetTop = Number.parseFloat( dom.offset( el ).top ) , elHeight = Number.parseFloat(dom.getStyleValue( el , 'height' ));
el._affix_scroll_handle = function(e){ let scroll_instance = window.pageYOffset || window.scrollY, position = 'static', paddingTop = nextElPaddingTop; if ( scroll_instance >= elOffsetTop ){ position = 'fixed' paddingTop = elHeight + nextElPaddingTop + 'px' }else{ position = 'static' paddingTop = nextElPaddingTop + 'px' }
dom.css( el , { position, left: '0px', top: '0px' })
dom.css(nextEl , {paddingTop}) } el._affix_click_handle = function(){ window.scrollTo(0,0) }
window.addEventListener( 'scroll' , el._affix_scroll_handle ) el.addEventListener( 'click', el._affix_click_handle ) }, unbind(el){ el._affix_scroll_handle && window.removeEventListener( 'scroll' , el._affix_scroll_handle ) el._affix_click_handle && el.removeEventListener( 'click' , el._affix_click_handle ) } }
|