fix: reset xPos and yPos in seamlessScroll

This commit is contained in:
xiaoxian521 2021-04-15 19:40:38 +08:00
parent e09ed0fb47
commit f886a38694
2 changed files with 33 additions and 42 deletions

View File

@ -30,7 +30,7 @@ import {
ref, ref,
unref, unref,
watchEffect, watchEffect,
nextTick, nextTick
} from "vue"; } from "vue";
import { tryOnMounted, tryOnUnmounted, templateRef } from "@vueuse/core"; import { tryOnMounted, tryOnUnmounted, templateRef } from "@vueuse/core";
import * as utilsMethods from "./utils"; import * as utilsMethods from "./utils";
@ -56,14 +56,14 @@ export default defineComponent({
type: Array, type: Array,
default: () => { default: () => {
return []; return [];
}, }
}, },
classOption: { classOption: {
type: Object, type: Object,
default: () => { default: () => {
return {}; return {};
}, }
}, }
}, },
emits: ["ScrollEnd"], emits: ["ScrollEnd"],
setup(props, { emit }) { setup(props, { emit }) {
@ -117,7 +117,7 @@ export default defineComponent({
switchDelay: 400, switchDelay: 400,
switchDisabledClass: "disabled", switchDisabledClass: "disabled",
// singleWidth/singleHeight rem // singleWidth/singleHeight rem
isSingleRemUnit: false, isSingleRemUnit: false
}; };
}); });
@ -138,17 +138,16 @@ export default defineComponent({
return { return {
position: "absolute", position: "absolute",
margin: `${unref(height) / 2}px 0 0 -${unref(options).switchOffset}px`, margin: `${unref(height) / 2}px 0 0 -${unref(options).switchOffset}px`,
transform: "translate(-100%,-50%)", transform: "translate(-100%,-50%)"
}; };
}); });
let rightSwitch = computed(() => { let rightSwitch = computed(() => {
return { return {
position: "absolute", position: "absolute",
margin: `${unref(height) / 2}px 0 0 ${ margin: `${unref(height) / 2}px 0 0 ${unref(width) +
unref(width) + unref(options).switchOffset unref(options).switchOffset}px`,
}px`, transform: "translateY(-50%)"
transform: "translateY(-50%)",
}; };
}); });
@ -169,7 +168,7 @@ export default defineComponent({
return { return {
transform: `translate(${unref(xPos)}px,${unref(yPos)}px)`, transform: `translate(${unref(xPos)}px,${unref(yPos)}px)`,
transition: `all ${ease} ${unref(delay)}ms`, transition: `all ${ease} ${unref(delay)}ms`,
overflow: "hidden", overflow: "hidden"
}; };
}); });
@ -225,6 +224,8 @@ export default defineComponent({
}); });
function reset() { function reset() {
xPos.value = 0;
yPos.value = 0;
scrollCancle(); scrollCancle();
scrollInitMove(); scrollInitMove();
} }
@ -265,7 +266,7 @@ export default defineComponent({
//touch //touch
startPos = { startPos = {
x: touch.pageX, x: touch.pageX,
y: touch.pageY, y: touch.pageY
}; };
//touchStartposY //touchStartposY
startPosY = unref(yPos); startPosY = unref(yPos);
@ -280,7 +281,7 @@ export default defineComponent({
scrollCancle(); scrollCancle();
} }
} }
function touchMove(e) { function touchMove(e) {
//touchmove //touchmove
if ( if (
@ -293,7 +294,7 @@ export default defineComponent({
const { direction } = unref(options); const { direction } = unref(options);
let endPos = { let endPos = {
x: touch.pageX - startPos.x, x: touch.pageX - startPos.x,
y: touch.pageY - startPos.y, y: touch.pageY - startPos.y
}; };
// //
e.preventDefault(); e.preventDefault();
@ -350,7 +351,7 @@ export default defineComponent({
if (isHover) return; if (isHover) return;
//move touchMove //move touchMove
scrollCancle(); scrollCancle();
reqFrame = requestAnimationFrame(function () { reqFrame = requestAnimationFrame(function() {
// //
const h = unref(realBoxHeight) / 2; const h = unref(realBoxHeight) / 2;
// //
@ -538,8 +539,8 @@ export default defineComponent({
scrollMove, scrollMove,
scrollInitMove, scrollInitMove,
scrollStartMove, scrollStartMove,
scrollStopMove, scrollStopMove
}; };
}, }
}); });
</script> </script>

View File

@ -14,7 +14,6 @@
<ul class="item"> <ul class="item">
<li v-for="(item, index) in listData" :key="index"> <li v-for="(item, index) in listData" :key="index">
<span class="title" v-text="item.title"></span> <span class="title" v-text="item.title"></span>
<span class="date" v-text="item.date"></span>
</li> </li>
</ul> </ul>
</SeamlessScroll> </SeamlessScroll>
@ -29,65 +28,56 @@ import { templateRef } from "@vueuse/core";
import SeamlessScroll from "/@/components/SeamlessScroll"; import SeamlessScroll from "/@/components/SeamlessScroll";
export default { export default {
components: { components: {
SeamlessScroll, SeamlessScroll
}, },
setup() { setup() {
const scroll = templateRef<HTMLElement | null>("scroll", null); const scroll = templateRef<HTMLElement | null>("scroll", null);
let listData = ref([ let listData = ref([
{ {
title: "无缝滚动第一行无缝滚动第一行", title: "无缝滚动第一行无缝滚动第一行!!!!!!!!!!"
date: "2021-5-1",
}, },
{ {
title: "无缝滚动第二行无缝滚动第二行", title: "无缝滚动第二行无缝滚动第二行!!!!!!!!!!"
date: "2021-5-1",
}, },
{ {
title: "无缝滚动第三行无缝滚动第三行", title: "无缝滚动第三行无缝滚动第三行!!!!!!!!!!"
date: "2021-5-1",
}, },
{ {
title: "无缝滚动第四行无缝滚动第四行", title: "无缝滚动第四行无缝滚动第四行!!!!!!!!!!"
date: "2021-5-1",
}, },
{ {
title: "无缝滚动第五行无缝滚动第五行", title: "无缝滚动第五行无缝滚动第五行!!!!!!!!!!"
date: "2021-5-1",
}, },
{ {
title: "无缝滚动第六行无缝滚动第六行", title: "无缝滚动第六行无缝滚动第六行!!!!!!!!!!"
date: "2021-5-1",
}, },
{ {
title: "无缝滚动第七行无缝滚动第七行", title: "无缝滚动第七行无缝滚动第七行!!!!!!!!!!"
date: "2021-5-1",
}, },
{ {
title: "无缝滚动第八行无缝滚动第八行", title: "无缝滚动第八行无缝滚动第八行!!!!!!!!!!"
date: "2021-5-1",
}, },
{ {
title: "无缝滚动第九行无缝滚动第九行", title: "无缝滚动第九行无缝滚动第九行!!!!!!!!!!"
date: "2021-5-1", }
},
]); ]);
let classOption = ref({ let classOption = ref({
direction: "top", direction: "top"
}); });
function changeDirection(val) { function changeDirection(val) {
scroll.value.scrollInitMove(); scroll.value.reset();
classOption.value.direction = val; classOption.value.direction = val;
} }
return { return {
listData, listData,
classOption, classOption,
changeDirection, changeDirection
}; };
}, }
}; };
</script> </script>