123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- const dom = uni.requireNativePlugin('dom')
- const animation = uni.requireNativePlugin('animation')
- export default {
- data() {
- return {
-
- moving: false,
-
- status: 'close',
-
- startX: 0,
- startY: 0,
-
- buttons: [],
-
- buttonsWidth: 0,
-
- moveX: 0,
-
- lastX: 0
- }
- },
- computed: {
-
- getDuratin() {
- let duration = String(this.duration)
-
- if (duration.indexOf('ms') >= 0) return parseInt(duration)
-
- if (duration.indexOf('s') >= 0) return parseInt(duration) * 1000
-
- duration = Number(duration)
- return duration < 30 ? duration * 1000 : duration
- }
- },
- watch: {
- show: {
- immediate: true,
- handler(n) {
-
-
-
-
-
-
-
- }
- }
- },
- mounted() {
- uni.$u.sleep(20).then(() => {
- this.queryRect()
- })
- },
- methods: {
- close() {
- this.closeSwipeAction()
- },
-
- touchstart(event) {
- if (this.disabled) return
- this.closeOther()
- const { touches } = event
-
- this.startX = touches[0].pageX
- this.startY = touches[0].pageY
- },
-
- touchmove(event) {
- if (this.disabled) return
- const { touches } = event
- const { pageX } = touches[0]
- const { pageY } = touches[0]
- let moveX = pageX - this.startX
- const moveY = pageY - this.startY
- const { buttonsWidth } = this
- const len = this.buttons.length
-
- if (Math.abs(pageX - this.lastX) < 0.3) return
- this.lastX = pageX
-
- if (Math.abs(moveX) > Math.abs(moveY) || Math.abs(moveX) > this.threshold) {
- event.stopPropagation()
- }
-
- if (Math.abs(moveX) < Math.abs(moveY)) return
-
-
-
- if (this.status === 'open') {
-
- if (moveX < 0) moveX = 0
-
- if (moveX > buttonsWidth) moveX = buttonsWidth
-
- this.moveSwipeAction(-buttonsWidth + moveX)
- } else {
-
- if (moveX > 0) moveX = 0
-
- if (Math.abs(moveX) > buttonsWidth) moveX = -buttonsWidth
-
- this.moveSwipeAction(moveX)
- }
- },
-
- touchend(event) {
- if (this.disabled) return
- const touches = event.changedTouches ? event.changedTouches[0] : {}
- const { pageX } = touches
- const { pageY } = touches
- const { buttonsWidth } = this
- this.moveX = pageX - this.startX
- if (this.status === 'open') {
-
- if (this.moveX < 0) this.moveX = 0
- if (this.moveX > buttonsWidth) this.moveX = buttonsWidth
-
- if (this.moveX === 0) {
- return this.closeSwipeAction()
- }
-
- if (Math.abs(this.moveX) < this.threshold) {
- this.openSwipeAction()
- } else {
-
- this.closeSwipeAction()
- }
- } else {
-
- if (this.moveX >= 0) this.moveX = 0
- if (this.moveX <= -buttonsWidth) this.moveX = -buttonsWidth
-
- if (Math.abs(this.moveX) < this.threshold) {
- this.closeSwipeAction()
- } else {
- this.openSwipeAction()
- }
- }
- },
-
- moveSwipeAction(moveX) {
- if (this.moving) return
- this.moving = true
- let previewButtonsMoveX = 0
- const len = this.buttons.length
- animation.transition(this.$refs['u-swipe-action-item__content'].ref, {
- styles: {
- transform: `translateX(${moveX}px)`
- },
- timingFunction: 'linear'
- }, () => {
- this.moving = false
- })
-
- for (let i = len - 1; i >= 0; i--) {
- const buttonRef = this.$refs[`u-swipe-action-item__right__button-${i}`][0].ref
-
- const translateX = this.buttons[i].width / this.buttonsWidth * moveX
-
- const realTranslateX = translateX + previewButtonsMoveX
- animation.transition(buttonRef, {
- styles: {
- transform: `translateX(${realTranslateX}px)`
- },
- duration: 0,
- delay: 0,
- timingFunction: 'linear'
- }, () => {})
-
- previewButtonsMoveX += translateX
- }
- },
-
- closeSwipeAction() {
- if (this.status === 'close') return
- this.moving = true
- const { buttonsWidth } = this
- animation.transition(this.$refs['u-swipe-action-item__content'].ref, {
- styles: {
- transform: 'translateX(0px)'
- },
- duration: this.getDuratin,
- timingFunction: 'ease-in-out'
- }, () => {
- this.status = 'close'
- this.moving = false
- this.closeHandler()
- })
-
- const len = this.buttons.length
- for (let i = len - 1; i >= 0; i--) {
- const buttonRef = this.$refs[`u-swipe-action-item__right__button-${i}`][0].ref
-
- if (this.buttons.length === 0 || !this.buttons[i] || !this.buttons[i].width) return
- animation.transition(buttonRef, {
- styles: {
- transform: 'translateX(0px)'
- },
- duration: this.getDuratin,
- timingFunction: 'ease-in-out'
- }, () => {})
- }
- },
-
- openSwipeAction() {
- if (this.status === 'open') return
- this.moving = true
- const buttonsWidth = -this.buttonsWidth
- let previewButtonsMoveX = 0
- animation.transition(this.$refs['u-swipe-action-item__content'].ref, {
- styles: {
- transform: `translateX(${buttonsWidth}px)`
- },
- duration: this.getDuratin,
- timingFunction: 'ease-in-out'
- }, () => {
- this.status = 'open'
- this.moving = false
- this.openHandler()
- })
-
- const len = this.buttons.length
- for (let i = len - 1; i >= 0; i--) {
- const buttonRef = this.$refs[`u-swipe-action-item__right__button-${i}`][0].ref
-
- if (this.buttons.length === 0 || !this.buttons[i] || !this.buttons[i].width) return
-
- const translateX = this.buttons[i].width / this.buttonsWidth * buttonsWidth
-
- const realTranslateX = translateX + previewButtonsMoveX
- animation.transition(buttonRef, {
- styles: {
- transform: `translateX(${realTranslateX}px)`
- },
- duration: this.getDuratin,
- timingFunction: 'ease-in-out'
- }, () => {})
- previewButtonsMoveX += translateX
- }
- },
-
- queryRect() {
-
- const promiseAll = this.rightOptions.map((item, index) => this.getRectByDom(this.$refs[`u-swipe-action-item__right__button-${index}`][0]))
-
- Promise.all(promiseAll).then((sizes) => {
- this.buttons = sizes
-
- this.buttonsWidth = sizes.reduce((sum, cur) => sum + cur.width, 0)
- })
- },
-
- getRectByDom(ref) {
- return new Promise((resolve) => {
- dom.getComponentRect(ref, (res) => {
- resolve(res.size)
- })
- })
- }
- }
- }
|