123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- export default {
- data() {
- return {
- state: {
- moving: false,
- startX: 0,
- startY: 0,
- buttonsWidth: 0
- }
- }
- },
- watch: {
- status(newValue) {
- if (this.disabled) return
-
- if (newValue === 'close' && this.status === 'open') {
- this.closeSwipeAction()
- } else if(newValue === 'open' && this.status === 'close') {
- this.openSwipeAction()
- }
- },
- options(newVal) {
- this.getBtnWidth()
- }
- },
- mounted() {
- this.getBtnWidth()
- },
- methods: {
- clickHandler() {
- },
- closeHandler() {
- this.closeSwipeAction()
- },
- setStatus(status) {
- this.status = status
- },
- getBtnWidth() {
- let view = uni.createSelectorQuery().in(this).select(".u-swipe-action-item__right");
- view.fields({
- size: true,
- scrollOffset: true
- }, data => {
- this.state.buttonsWidth = data.width
-
- }).exec();
- },
-
- touchstart(event) {
-
-
- this.state.moving = true
-
- var touches = event.touches
- this.state.startX = touches[0].pageX
- this.state.startY = touches[0].pageY
-
-
-
- this.parent && this.parent.closeOther(this)
- },
- touchmove(event) {
-
- if (this.disabled || !this.state.moving) return
- var touches = event.touches
- var pageX = touches[0].pageX
- var pageY = touches[0].pageY
- var moveX = pageX - this.state.startX
- var moveY = pageY - this.state.startY
-
- if (Math.abs(moveX) > Math.abs(moveY) || Math.abs(moveX) > this.threshold) {
- event.preventDefault && event.preventDefault()
- event.stopPropagation && event.stopPropagation()
- }
-
- if (Math.abs(moveX) < Math.abs(moveY)) return
-
-
-
- if (this.status === 'open') {
-
- if (moveX < 0) moveX = 0
-
- if (moveX > this.state.buttonsWidth) moveX = this.state.buttonsWidth
-
- this.moveSwipeAction(-this.state.buttonsWidth + moveX)
- } else {
-
- if (moveX > 0) moveX = 0
-
- if (Math.abs(moveX) > this.state.buttonsWidth) moveX = -this.state.buttonsWidth
-
- this.moveSwipeAction(moveX)
- }
- },
- touchend(event) {
-
- if (!this.state.moving || this.disabled) return
- this.state.moving = false
- var touches = event.changedTouches ? event.changedTouches[0] : {}
- var pageX = touches.pageX
- var pageY = touches.pageY
- var moveX = pageX - this.state.startX
- if (this.status === 'open') {
-
- if (moveX < 0) return
-
- if (moveX === 0) {
- return this.closeSwipeAction()
- }
-
- if (Math.abs(moveX) < this.threshold) {
- this.openSwipeAction()
- } else {
-
- this.closeSwipeAction()
- }
- } else {
-
- if (moveX > 0) return
-
- if (Math.abs(moveX) < this.threshold) {
- this.closeSwipeAction()
- } else {
- this.openSwipeAction()
- }
- }
- },
-
- openSwipeAction() {
-
- var duration = this.getDuration(this.duration)
-
- var buttonsWidth = -this.state.buttonsWidth
- this.sliderStyle = {
- 'transition': 'transform ' + duration,
- 'transform': 'translateX(' + buttonsWidth + 'px)',
- '-webkit-transform': 'translateX(' + buttonsWidth + 'px)',
- }
- this.setStatus('open')
- },
-
- closeSwipeAction() {
-
- var duration = this.getDuration(this.duration)
- this.sliderStyle = {
- 'transition': 'transform ' + duration,
- 'transform': 'translateX(0px)',
- '-webkit-transform': 'translateX(0px)'
- }
-
-
-
-
-
-
-
-
- this.setStatus('close')
- },
-
- moveSwipeAction(moveX) {
-
- this.sliderStyle = {
- 'transition': 'none',
- transform: 'translateX(' + moveX + 'px)',
- '-webkit-transform': 'translateX(' + moveX + 'px)'
- }
- },
-
- getDuration(value) {
- if (value.toString().indexOf('s') >= 0) return value
- return value > 30 ? value + 'ms' : value + 's'
- }
- }
- }
|