11import './tooltip.scss'
2-
32import { defineComponent , reactive , ref , watch , onMounted , getCurrentInstance , onBeforeUnmount , renderSlot , useSlots } from 'vue'
43import { tooltipProps } from './tooltip-types'
54import EventListener from '../utils/EventListener'
@@ -16,133 +15,138 @@ export default defineComponent({
1615 let position = reactive ( {
1716 left : 0 ,
1817 top : 0
19- } ) ;
20- let show = ref ( false ) ;
18+ } )
19+ let show = ref ( false )
2120
2221 // slotElement元素的ref
23- let slotElement = ref ( null ) ;
22+ let slotElement = ref ( null )
2423 // tooltip元素的引用
25- let tooltip = ref ( null ) ;
24+ let tooltip = ref ( null )
2625 // arrow元素的引用
27- let arrow = ref ( null ) ;
26+ let arrow = ref ( null )
2827 // tooltipcontent的引用
29- let tooltipcontent = ref ( null ) ;
28+ let tooltipcontent = ref ( null )
3029
31- let enterEvent ;
32- let leaveEvent ;
30+ let enterEvent
31+ let leaveEvent
3332
3433 const arrowStyle = ( attr , value ) => {
35- arrow . value . style [ attr ] = value ;
36- } ;
34+ arrow . value . style [ attr ] = value
35+ }
3736
3837 // 延迟显示
39- const delayShowTrue = function ( fn , delay ) {
40- let start ;
38+ const delayShowTrue = function ( fn , delay = props . mouseEnterDelay ) {
39+ let start
4140 if ( parseInt ( delay ) >= 0 ) {
4241 return function ( ) {
4342 if ( start ) {
44- clearTimeout ( start ) ;
43+ clearTimeout ( start )
4544 }
46- start = setTimeout ( fn , parseInt ( delay ) ) ;
47- } ;
45+ start = setTimeout ( fn , parseInt ( delay ) )
46+ }
4847 } else {
49- console . error ( 'the value of delay is bigger than 0 and the type of delay must be string!' ) ;
48+ console . error ( 'the value of delay is bigger than 0 and the type of delay must be string!' )
5049 return
5150 }
52- } ;
51+ }
5352 // 延迟消失
54- const delayShowFalse = function ( fn , delay ) {
53+ const delayShowFalse = function ( fn , delay = props . mouseLeaveDelay ) {
5554 if ( show . value && parseInt ( delay ) >= 0 ) {
56- setTimeout ( fn , parseInt ( delay ) ) ;
55+ setTimeout ( fn , parseInt ( delay ) )
5756 }
58- } ;
57+ }
5958
6059 onMounted ( ( ) => {
6160 // 组件初始化不渲染tooltip
6261 if ( ! show . value ) {
63- tooltip . value . style . opacity = '0' ;
64- } ;
62+ tooltip . value . style . opacity = '0'
63+ }
6564
6665 // 注册鼠标引入事件
67- enterEvent = EventListener . listen ( slotElement . value . children [ 0 ] , 'mouseenter' , function ( ) {
68- show . value = true ;
69- } ) ;
66+ /*enterEvent = EventListener.listen(slotElement.value.children[0], 'mouseenter', function (){
67+ show.value = true
68+ })*/
69+ enterEvent = EventListener . listen ( slotElement . value . children [ 0 ] , 'mouseenter' , delayShowTrue ( function ( ) {
70+ show . value = true
71+ } , props . mouseEnterDelay ) )
7072
7173 // 注册鼠标移除事件
7274 leaveEvent = EventListener . listen ( slotElement . value . children [ 0 ] , 'mouseleave' , function ( ) {
73- show . value = false ;
74- } ) ;
75- } ) ;
75+ // show.value = false
76+ setTimeout ( function ( ) {
77+ show . value = false ;
78+ } , props . mouseLeaveDelay )
79+ } )
80+ } )
7681
7782 watch ( show , function ( newValue , oldValue ) {
7883 if ( newValue ) {
7984 // 鼠标悬浮为true,显示提示框
80- tooltip . value . style . opacity = '1' ;
81- tooltip . value . style . zIndex = '500' ;
82- arrow . value . style . border = '10px solid transparent' ;
85+ tooltip . value . style . opacity = '1'
86+ tooltip . value . style . zIndex = '999'
87+ arrow . value . style . border = '10px solid transparent'
8388 // 具体的判定规则
8489 switch ( props . position ) {
8590 case 'top' :
8691 // 设置 tooltip 内容的样式
87- position . left = slotElement . value . children [ 0 ] . offsetLeft - tooltip . value . offsetWidth / 2 + slotElement . value . children [ 0 ] . offsetWidth / 2 ;
88- position . top = slotElement . value . children [ 0 ] . offsetTop - 10 - tooltipcontent . value . offsetHeight ;
92+ position . left = slotElement . value . children [ 0 ] . offsetLeft - tooltip . value . offsetWidth / 2 + slotElement . value . children [ 0 ] . offsetWidth / 2
93+ position . top = slotElement . value . children [ 0 ] . offsetTop - 10 - tooltipcontent . value . offsetHeight
8994 // 设置箭头的样式
90- arrowStyle ( 'borderTop' , '10px solid cornflowerblue' ) ;
91- arrow . value . style . top = `${ tooltipcontent . value . offsetHeight } px` ;
92- arrow . value . style . left = `${ tooltipcontent . value . offsetWidth / 2 - 5 } px` ;
93- break ;
95+ arrowStyle ( 'borderTop' , '10px solid cornflowerblue' )
96+ arrow . value . style . top = `${ tooltipcontent . value . offsetHeight } px`
97+ arrow . value . style . left = `${ tooltipcontent . value . offsetWidth / 2 - 5 } px`
98+ break
9499
95100 case 'right' :
96101 // 设置tooltip 内容的样式
97- position . left = slotElement . value . children [ 0 ] . offsetLeft + slotElement . value . children [ 0 ] . offsetWidth ;
98- position . top = slotElement . value . children [ 0 ] . offsetTop + slotElement . value . children [ 0 ] . offsetHeight / 2 - tooltipcontent . value . offsetHeight / 2 ;
102+ position . left = slotElement . value . children [ 0 ] . offsetLeft + slotElement . value . children [ 0 ] . offsetWidth
103+ position . top = slotElement . value . children [ 0 ] . offsetTop + slotElement . value . children [ 0 ] . offsetHeight / 2 - tooltipcontent . value . offsetHeight / 2
99104 // 设置箭头的样式
100- arrowStyle ( 'borderRight' , '10px solid cornflowerblue' ) ;
101- arrow . value . style . top = `${ tooltipcontent . value . offsetHeight / 2 - 10 } px` ;
102- arrow . value . style . left = '-10px' ;
103- break ;
105+ arrowStyle ( 'borderRight' , '10px solid cornflowerblue' )
106+ arrow . value . style . top = `${ tooltipcontent . value . offsetHeight / 2 - 10 } px`
107+ arrow . value . style . left = '-10px'
108+ break
104109
105110 case 'bottom' :
106111 // 设置tooltip的样式
107- position . top = slotElement . value . children [ 0 ] . offsetHeight + slotElement . value . children [ 0 ] . offsetTop + 10 ;
108- position . left = slotElement . value . children [ 0 ] . offsetLeft + slotElement . value . children [ 0 ] . offsetWidth / 2 - tooltipcontent . value . offsetWidth / 2 ;
112+ position . top = slotElement . value . children [ 0 ] . offsetHeight + slotElement . value . children [ 0 ] . offsetTop + 10
113+ position . left = slotElement . value . children [ 0 ] . offsetLeft + slotElement . value . children [ 0 ] . offsetWidth / 2 - tooltipcontent . value . offsetWidth / 2
109114 // 设置arrow.value的样式
110115 arrowStyle ( 'borderBottom' , '10px solid cornflowerblue' )
111- arrow . value . style . top = '-20px' ;
112- arrow . value . style . left = `${ tooltipcontent . value . offsetWidth / 2 - 10 } px` ;
113- break ;
116+ arrow . value . style . top = '-20px'
117+ arrow . value . style . left = `${ tooltipcontent . value . offsetWidth / 2 - 10 } px`
118+ break
114119
115120 case 'left' :
116- position . top = slotElement . value . children [ 0 ] . offsetTop + slotElement . value . children [ 0 ] . offsetHeight / 2 - tooltipcontent . value . offsetHeight / 2 ;
117- position . left = slotElement . value . children [ 0 ] . offsetLeft - 20 - tooltipcontent . value . offsetWidth ;
121+ position . top = slotElement . value . children [ 0 ] . offsetTop + slotElement . value . children [ 0 ] . offsetHeight / 2 - tooltipcontent . value . offsetHeight / 2
122+ position . left = slotElement . value . children [ 0 ] . offsetLeft - 20 - tooltipcontent . value . offsetWidth
118123 // 设置arrow.value的样式
119- arrowStyle ( 'borderLeft' , '10px solid cornflowerblue' ) ;
124+ arrowStyle ( 'borderLeft' , '10px solid cornflowerblue' )
120125 arrow . value . style . left = `${ tooltipcontent . value . offsetWidth + 10 } px`
121- arrow . value . style . top = `${ tooltipcontent . value . offsetHeight / 2 - 10 } px` ;
122- break ;
126+ arrow . value . style . top = `${ tooltipcontent . value . offsetHeight / 2 - 10 } px`
127+ break
123128
124129 default :
125- console . error ( 'The attribute position value is wrong, the value is one of top、right、left、bottom' ) ;
126- break ;
127- } ;
128-
129- tooltip . value . style . top = position . top + 'px' ;
130- tooltip . value . style . left = position . left + 'px' ;
130+ console . error ( 'The attribute position value is wrong, the value is one of top、right、left、bottom' )
131+ break
132+ }
133+ tooltip . value . style . top = position . top + 'px'
134+ tooltip . value . style . left = position . left + 'px'
131135 } else {
132- position . top = 0 ;
133- position . left = 0 ;
136+ position . top = 0
137+ position . left = 0
134138 // 鼠标移走为false,隐藏提示框
135- tooltip . value . style . opacity = '0' ;
139+ tooltip . value . style . opacity = '0'
136140 }
137- } ) ;
141+ } )
138142
139143 onBeforeUnmount ( ( ) => {
140- enterEvent . remove ( ) ;
141- leaveEvent . remove ( ) ;
142- } ) ;
144+ enterEvent . remove ( )
145+ leaveEvent . remove ( )
146+ } )
143147
144148 return ( ) => {
145- const defaultSlot = renderSlot ( useSlots ( ) , 'default' ) ;
149+ const defaultSlot = renderSlot ( useSlots ( ) , 'default' )
146150 return (
147151 < div class = "d-tooltip" >
148152 < div class = 'slotElement' ref = { slotElement } >
@@ -158,4 +162,4 @@ export default defineComponent({
158162 )
159163 }
160164 }
161- } ) ;
165+ } )
0 commit comments