123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <template>
- <view class="">
- <movable-area class="u-swipe-action" :style="{ backgroundColor: bgColor }">
- <movable-view
- class="u-swipe-view"
- @change="change"
- @touchend="touchend"
- @touchstart="touchstart"
- direction="horizontal"
- :disabled="disabled"
- :x="moveX"
- :style="{
- width: movableViewWidth ? movableViewWidth : '100%'
- }"
- >
- <view
- class="u-swipe-content"
- @tap.stop="contentClick"
- >
- <slot></slot>
- </view>
- <view class="u-swipe-del" v-if="showBtn" @tap.stop="btnClick(index)" :style="[btnStyle(item.style)]" v-for="(item, index) in options" :key="index">
- <view class="u-btn-text">{{ item.text }}</view>
- </view>
- </movable-view>
- </movable-area>
- </view>
- </template>
- <script>
- export default {
- name: 'u-swipe-action',
- props: {
-
- index: {
- type: [Number, String],
- default: ''
- },
-
- btnWidth: {
- type: [String, Number],
- default: 180
- },
-
- disabled: {
- type: Boolean,
- default: false
- },
-
- show: {
- type: Boolean,
- default: false
- },
-
- bgColor: {
- type: String,
- default: '#ffffff'
- },
-
- vibrateShort: {
- type: Boolean,
- default: false
- },
-
- options: {
- type: Array,
- default() {
- return [];
- }
- }
- },
- watch: {
- show: {
- immediate: true,
- handler(nVal, oVal) {
- if (nVal) {
- this.open();
- } else {
- this.close();
- }
- }
- }
- },
- data() {
- return {
- moveX: 0,
- scrollX: 0,
- status: false,
- movableAreaWidth: 0,
- elId: this.$u.guid(),
- showBtn: false,
- };
- },
- computed: {
- movableViewWidth() {
- return this.movableAreaWidth + this.allBtnWidth + 'px';
- },
- innerBtnWidth() {
- return uni.upx2px(this.btnWidth);
- },
- allBtnWidth() {
- return uni.upx2px(this.btnWidth) * this.options.length;
- },
- btnStyle() {
- return style => {
- let css = {};
- style.width = this.btnWidth + 'rpx';
- return style;
- };
- }
- },
- mounted() {
- this.getActionRect();
- },
- methods: {
-
- btnClick(index) {
- this.status = false;
-
- this.$emit('click', this.index, index);
- },
-
- change(e) {
- this.scrollX = e.detail.x;
- },
-
- close() {
- this.moveX = 0;
- this.status = false;
- },
-
- open() {
- if (this.disabled) return;
- this.moveX = -this.allBtnWidth;
- this.status = true;
- },
-
- touchend() {
- this.moveX = this.scrollX;
-
-
-
-
-
- this.$nextTick(function() {
- if (this.status == false) {
-
- if (this.scrollX <= -this.allBtnWidth / 4) {
- this.moveX = -this.allBtnWidth;
- this.status = true;
- this.emitOpenEvent();
-
- if (this.vibrateShort) uni.vibrateShort();
- } else {
- this.moveX = 0;
- this.status = false;
- this.emitCloseEvent();
- }
- } else {
-
- if (this.scrollX > (-this.allBtnWidth * 3) / 4) {
- this.moveX = 0;
- this.$nextTick(() => {
- this.moveX = 101;
- });
- this.status = false;
- this.emitCloseEvent();
- } else {
- this.moveX = -this.allBtnWidth;
- this.status = true;
- this.emitOpenEvent();
- }
- }
- });
- },
- emitOpenEvent() {
- this.$emit('open', this.index);
- },
- emitCloseEvent() {
- this.$emit('close', this.index);
- },
-
- touchstart() {},
- getActionRect() {
- this.$uGetRect('.u-swipe-action').then(res => {
- this.movableAreaWidth = res.width;
-
- this.$nextTick(() => {
- this.showBtn = true;
- })
- });
- },
-
- contentClick() {
-
- if (this.status == true) {
- this.status = 'close';
- this.moveX = 0;
- }
- this.$emit('content-click', this.index);
- }
- }
- };
- </script>
- <style scoped lang="scss">
- @import "../../libs/css/style.components.scss";
-
- .u-swipe-action {
- width: auto;
- height: initial;
- position: relative;
- overflow: hidden;
- }
- .u-swipe-view {
- @include vue-flex;
- height: initial;
- position: relative;
- /* 这一句很关键,覆盖默认的绝对定位 */
- }
- .u-swipe-content {
- flex: 1;
- }
- .u-swipe-del {
- position: relative;
- font-size: 30rpx;
- color: #ffffff;
- }
- .u-btn-text {
- position: absolute;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- }
- </style>
|