refactor: use pointer event replace touch event

This commit is contained in:
zyronon 2024-04-15 00:14:21 +08:00
parent 7dc20b69b9
commit 3b3c5a3bb3
5 changed files with 34 additions and 25 deletions

View File

@ -6,16 +6,16 @@
</keep-alive> </keep-alive>
</transition> </transition>
</router-view> </router-view>
<BaseMask v-if="!isMobile" /> <!-- <BaseMask v-if="!isMobile" />-->
<div v-if="!isMobile" class="guide"> <!-- <div v-if="!isMobile" class="guide">-->
<Icon icon="mynaui:danger-triangle" /> <!-- <Icon icon="mynaui:danger-triangle" />-->
<div class="txt"> <!-- <div class="txt">-->
<h2>切换至手机模式才可正常使用</h2> <!-- <h2>切换至手机模式才可正常使用</h2>-->
<h3>1. F12 调出控制台</h3> <!-- <h3>1. F12 调出控制台</h3>-->
<h3>2. Ctrl+Shift+M或点击下面图标</h3> <!-- <h3>2. Ctrl+Shift+M或点击下面图标</h3>-->
</div> <!-- </div>-->
<img src="@/assets/img/guide.png" alt="" /> <!-- <img src="@/assets/img/guide.png" alt="" />-->
</div> <!-- </div>-->
<Call /> <Call />
</template> </template>
<script setup lang="ts"> <script setup lang="ts">

View File

@ -117,6 +117,7 @@ p {
} }
.slide { .slide {
touch-action: none;
height: 100%; height: 100%;
width: 100%; width: 100%;
transition: height 0.3s; transition: height 0.3s;

View File

@ -40,7 +40,8 @@ const state = reactive({
next: false, next: false,
start: { x: 0, y: 0, time: 0 }, start: { x: 0, y: 0, time: 0 },
move: { x: 0, y: 0 }, move: { x: 0, y: 0 },
wrapper: { width: 0, height: 0, childrenLength: 0 } wrapper: { width: 0, height: 0, childrenLength: 0 },
isDown: false
}) })
watch( watch(
@ -73,11 +74,11 @@ onUnmounted(() => {
ob.disconnect() ob.disconnect()
}) })
function touchStart(e: TouchEvent) { function touchStart(e: PointerEvent) {
slideTouchStart(e, wrapperEl.value, state) slideTouchStart(e, wrapperEl.value, state)
} }
function touchMove(e: TouchEvent) { function touchMove(e: PointerEvent) {
slideTouchMove( slideTouchMove(
e, e,
wrapperEl.value, wrapperEl.value,
@ -91,7 +92,7 @@ function touchMove(e: TouchEvent) {
) )
} }
function touchEnd(e: TouchEvent) { function touchEnd(e: PointerEvent) {
slideTouchEnd(e, state, canNext, () => {}) slideTouchEnd(e, state, canNext, () => {})
slideReset(wrapperEl.value, state, SlideType.HORIZONTAL, emit) slideReset(wrapperEl.value, state, SlideType.HORIZONTAL, emit)
} }
@ -109,9 +110,9 @@ function canNext(isNext: boolean) {
<div <div
class="slide-list" class="slide-list"
ref="wrapperEl" ref="wrapperEl"
@touchstart="touchStart" @pointerdown="touchStart"
@touchmove="touchMove" @pointermove="touchMove"
@touchend="touchEnd" @pointerup="touchEnd"
> >
<slot></slot> <slot></slot>
</div> </div>

View File

@ -68,7 +68,8 @@ const state = reactive({
next: false, next: false,
start: { x: 0, y: 0, time: 0 }, start: { x: 0, y: 0, time: 0 },
move: { x: 0, y: 0 }, move: { x: 0, y: 0 },
wrapper: { width: 0, height: 0, childrenLength: 0 } wrapper: { width: 0, height: 0, childrenLength: 0 },
isDown: false
}) })
const baseStore = useBaseStore() const baseStore = useBaseStore()
@ -342,9 +343,9 @@ function canNext(isNext) {
class="slide-list flex-direction-column" class="slide-list flex-direction-column"
ref="wrapperEl" ref="wrapperEl"
@click="null" @click="null"
@touchstart="touchStart" @pointerdown="touchStart"
@touchmove="touchMove" @pointermove="touchMove"
@touchend="touchEnd" @pointerup="touchEnd"
> >
<slot></slot> <slot></slot>
</div> </div>

View File

@ -21,8 +21,9 @@ export function slideInit(el, state, type) {
export function slideTouchStart(e, el, state) { export function slideTouchStart(e, el, state) {
Utils.$setCss(el, 'transition-duration', `0ms`) Utils.$setCss(el, 'transition-duration', `0ms`)
state.start.x = e.touches[0].pageX state.isDown = true
state.start.y = e.touches[0].pageY state.start.x = e.pageX
state.start.y = e.pageY
state.start.time = Date.now() state.start.time = Date.now()
} }
@ -55,8 +56,11 @@ export function slideTouchMove(
notNextCb, notNextCb,
slideOtherDirectionCb = null slideOtherDirectionCb = null
) { ) {
state.move.x = e.touches[0].pageX - state.start.x if (!state.isDown) {
state.move.y = e.touches[0].pageY - state.start.y return
}
state.move.x = e.pageX - state.start.x
state.move.y = e.pageY - state.start.y
let isNext = type === SlideType.HORIZONTAL ? state.move.x < 0 : state.move.y < 0 let isNext = type === SlideType.HORIZONTAL ? state.move.x < 0 : state.move.y < 0
@ -99,6 +103,8 @@ export function slideTouchEnd(
doNotNextCb, doNotNextCb,
type = SlideType.HORIZONTAL type = SlideType.HORIZONTAL
) { ) {
state.isDown = false
let isHorizontal = type === SlideType.HORIZONTAL let isHorizontal = type === SlideType.HORIZONTAL
let isNext = isHorizontal ? state.move.x < 0 : state.move.y < 0 let isNext = isHorizontal ? state.move.x < 0 : state.move.y < 0