feat: ready add SeamlessScroll component

This commit is contained in:
xiaoxian521 2021-04-14 23:33:38 +08:00
parent 71181b9d2b
commit bd8348b432
5 changed files with 150 additions and 6 deletions

View File

@ -1,7 +1,7 @@
import type { CSSProperties } from 'vue'
import { defineComponent, onBeforeMount, nextTick, ref, unref, computed, PropType, getCurrentInstance } from 'vue'
import { defineComponent, onBeforeMount, nextTick, ref, unref, computed, PropType } from 'vue'
import { templateRef } from '@vueuse/core'
import { useAttrs } from '/@/utils/useAttrs'
import { emitter } from '/@/utils/mitt'
@ -64,8 +64,8 @@ export default defineComponent({
}
},
setup(props) {
let vm: any
const cropper: any = ref<Nullable<Cropper>>(null)
const imgElRef = templateRef<HTMLElement | null>('imgElRef', null)
const isReady = ref(false)
@ -88,7 +88,7 @@ export default defineComponent({
)
async function init() {
const imgEl = vm.refs.imgElRef
const imgEl = unref(imgElRef)
if (!imgEl) {
return
}
@ -102,7 +102,6 @@ export default defineComponent({
}
onBeforeMount(() => {
vm = getCurrentInstance()
nextTick(() => {
init()
// tsx语法返回渲染模板外部组件想调用内部方法或者获取setup里面的实例暂时想到的办法是通过公共事件

View File

@ -0,0 +1,10 @@
import { App } from "vue"
import seamlessScroll from "./src/SeamlessScroll.vue"
export const SeamlessScroll = Object.assign(seamlessScroll, {
install(app: App) {
app.component(seamlessScroll.name, seamlessScroll)
}
})
export default SeamlessScroll

View File

@ -0,0 +1,15 @@
<template>
<div></div>
</template>
<script lang='ts'>
export default {
name: "SeamlessScroll",
setup() {
return {};
}
};
</script>
<style scoped>
</style>

View File

@ -0,0 +1,114 @@
/**
* @desc AnimationFrame简单兼容hack
*/
const animationFrame = () => {
window.cancelAnimationFrame = (function () {
return window.cancelAnimationFrame ||
window.webkitCancelAnimationFrame ||
window.mozCancelAnimationFrame ||
window.oCancelAnimationFrame ||
window.msCancelAnimationFrame ||
function (id) {
return window.clearTimeout(id)
}
})()
window.requestAnimationFrame = function () {
return (
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback) {
return window.setTimeout(callback, 1000 / 60)
}
)
}()
}
/**
* @desc
* @param {arr1,arr2}
* @return {Boolean}
*/
const arrayEqual = (arr1: Array<any>, arr2: Array<any>) => {
if (arr1 === arr2) return true
if (arr1.length !== arr2.length) return false
for (let i = 0; i < arr1.length; ++i) {
if (arr1[i] !== arr2[i]) return false
}
return true
}
/**
* @desc
*/
function copyObj() {
if (!Array.isArray) {
// @ts-expect-error
Array.isArray = function (arg) {
return Object.prototype.toString.call(arg) === '[object Array]'
}
}
let name, options, src, copy, copyIsArray, clone,
i = 1,
target = arguments[0] || {}, // 使用||运算符排除隐式强制类型转换为false的数据类型
deep = false,
len = arguments.length
if (typeof target === 'boolean') {
deep = target
target = arguments[1] || {}
i++
}
if (typeof target !== 'object' && typeof target !== 'function') {
target = {}
}
// 如果arguments.length === 1 或typeof arguments[0] === 'boolean',且存在arguments[1]则直接返回target对象
if (i === len) {
return target
}
for (; i < len; i++) {
//所以如果源对象中数据类型为Undefined或Null那么就会跳过本次循环接着循环下一个源对象
if ((options = arguments[i]) != null) {
// 如果遇到源对象的数据类型为Boolean, Number for in循环会被跳过不执行for in循环// src用于判断target对象是否存在name属性
for (name in options) {
// src用于判断target对象是否存在name属性
src = target[name]
// 需要复制的属性当前源对象的name属性
copy = options[name]
// 判断copy是否是数组
copyIsArray = Array.isArray(copy)
// 如果是深复制且copy是一个对象或数组则需要递归直到copy成为一个基本数据类型为止
if (deep && copy && (typeof copy === 'object' || copyIsArray)) {
if (copyIsArray) {
copyIsArray = false
// 如果目标对象存在name属性且是一个数组
// 则使用目标对象的name属性否则重新创建一个数组用于复制
clone = src && Array.isArray(src) ? src : []
} else {
// 如果目标对象存在name属性且是一个对象则使用目标对象的name属性否则重新创建一个对象用于复制
clone = src && typeof src === 'object' ? src : {}
}
// 深复制所以递归调用copyObject函数
// 返回值为target对象即clone对象
// copy是一个源对象
// @ts-expect-error
target[name] = copyObj(deep, clone, copy)
} else if (copy !== undefined) {
// 浅复制直接复制到target对象上
target[name] = copy
}
}
}
}
return target
}
export default {
animationFrame,
arrayEqual,
copyObj
}

8
types/global.d.ts vendored
View File

@ -18,7 +18,13 @@ declare global {
}
declare interface Window {
// Global vue app instance
__APP__: App<Element>
__APP__: App<Element>,
mozCancelAnimationFrame: (id?: any) => any,
oCancelAnimationFrame: (id?: any) => any,
msCancelAnimationFrame: (id?: any) => any,
mozRequestAnimationFrame: (id?: any) => any,
oRequestAnimationFrame: (id?: any) => any,
msRequestAnimationFrame: (id?: any) => any
}
// vue