|
| 1 | +<template> |
| 2 | + <div |
| 3 | + ref="div" |
| 4 | + class="ys-float-btn" |
| 5 | + :style="{'width':itemWidth+'px','height':itemHeight+'px','left':left+'px','top':top+'px'}" |
| 6 | + @click="onBtnClicked" |
| 7 | + > |
| 8 | + <slot name="icon" /> |
| 9 | + <p :style="{'font-size': fontSize+'px'}">{{ text }}</p> |
| 10 | + </div> |
| 11 | +</template> |
| 12 | + |
| 13 | +<script> |
| 14 | +export default { |
| 15 | + name: 'FloatButton', |
| 16 | + props: { |
| 17 | + text: { |
| 18 | + type: String, |
| 19 | + default: '默认文字' |
| 20 | + }, |
| 21 | + itemWidth: { |
| 22 | + type: Number, |
| 23 | + default: 60 |
| 24 | + }, |
| 25 | + itemHeight: { |
| 26 | + type: Number, |
| 27 | + default: 60 |
| 28 | + }, |
| 29 | + gapWidth: { |
| 30 | + type: Number, |
| 31 | + default: 10 |
| 32 | + }, |
| 33 | + coefficientHeight: { |
| 34 | + type: Number, |
| 35 | + default: 0.8 |
| 36 | + }, |
| 37 | + fontSize: { |
| 38 | + type: Number, |
| 39 | + default: 12 |
| 40 | + } |
| 41 | + }, |
| 42 | + data() { |
| 43 | + return { |
| 44 | + timer: null, |
| 45 | + currentTop: 0, |
| 46 | + clientWidth: 0, |
| 47 | + clientHeight: 0, |
| 48 | + left: 0, |
| 49 | + top: 0 |
| 50 | + } |
| 51 | + }, |
| 52 | + created() { |
| 53 | + this.clientWidth = document.documentElement.clientWidth |
| 54 | + this.clientHeight = document.documentElement.clientHeight |
| 55 | + this.left = this.clientWidth - this.itemWidth - this.gapWidth |
| 56 | + this.top = this.clientHeight * this.coefficientHeight |
| 57 | + }, |
| 58 | + mounted() { |
| 59 | + window.addEventListener('scroll', this.handleScrollStart) |
| 60 | + this.$nextTick(() => { |
| 61 | + const div = this.$refs.div |
| 62 | + div.addEventListener('touchstart', () => { |
| 63 | + div.style.transition = 'none' |
| 64 | + }) |
| 65 | + div.addEventListener('touchmove', (e) => { |
| 66 | + if (e.targetTouches.length === 1) { |
| 67 | + const touch = event.targetTouches[0] |
| 68 | + this.left = touch.clientX - this.itemWidth / 2 |
| 69 | + this.top = touch.clientY - this.itemHeight / 2 |
| 70 | + } |
| 71 | + }) |
| 72 | + div.addEventListener('touchend', () => { |
| 73 | + div.style.transition = 'all 0.3s' |
| 74 | + if (this.left > this.clientWidth / 2) { |
| 75 | + this.left = this.clientWidth - this.itemWidth - this.gapWidth |
| 76 | + } else { |
| 77 | + this.left = this.gapWidth |
| 78 | + } |
| 79 | + }) |
| 80 | + }) |
| 81 | + }, |
| 82 | + beforeDestroy() { |
| 83 | + window.removeEventListener('scroll', this.handleScrollStart) |
| 84 | + }, |
| 85 | + methods: { |
| 86 | + onBtnClicked() { |
| 87 | + this.$emit('onFloatBtnClicked') |
| 88 | + }, |
| 89 | + handleScrollStart() { |
| 90 | + this.timer && clearTimeout(this.timer) |
| 91 | + this.timer = setTimeout(() => { |
| 92 | + this.handleScrollEnd() |
| 93 | + }, 300) |
| 94 | + this.currentTop = document.documentElement.scrollTop || document.body.scrollTop |
| 95 | + if (this.left > this.clientWidth / 2) { |
| 96 | + this.left = this.clientWidth - this.itemWidth / 2 |
| 97 | + } else { |
| 98 | + this.left = -this.itemWidth / 2 |
| 99 | + } |
| 100 | + }, |
| 101 | + handleScrollEnd() { |
| 102 | + const scrollTop = document.documentElement.scrollTop || document.body.scrollTop |
| 103 | + if (scrollTop === this.currentTop) { |
| 104 | + if (this.left > this.clientWidth / 2) { |
| 105 | + this.left = this.clientWidth - this.itemWidth - this.gapWidth |
| 106 | + } else { |
| 107 | + this.left = this.gapWidth |
| 108 | + } |
| 109 | + clearTimeout(this.timer) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | +</script> |
| 115 | + |
| 116 | +<style scoped> |
| 117 | + .ys-float-btn { |
| 118 | + background: rgb(255, 255, 255); |
| 119 | + box-shadow: 0 2px 10px 0 rgba(0, 0, 0, 0.1); |
| 120 | + border-radius: 50%; |
| 121 | + color: #666666; |
| 122 | + z-index: 20; |
| 123 | + transition: all 0.3s; |
| 124 | +
|
| 125 | + display: flex; |
| 126 | + flex-direction: column; |
| 127 | + justify-content: center; |
| 128 | + align-items: center; |
| 129 | +
|
| 130 | + position: fixed; |
| 131 | + bottom: 20vw; |
| 132 | + } |
| 133 | +
|
| 134 | + .ys-float-btn img { |
| 135 | + width: 50%; |
| 136 | + height: 50%; |
| 137 | + object-fit: contain; |
| 138 | + margin-bottom: 3px; |
| 139 | + } |
| 140 | +
|
| 141 | +</style> |
0 commit comments