123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <template>
- <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
- }"
- >
- <view class="u-swipe-content"><slot></slot></view>
- <view
- class="u-swipe-del dir-top-wrap main-center"
- @tap.stop="share"
- :style="{
- width: innerBtnWidth/2 + 'px',
- 'background-color': theme.background_s
- }"
- >
- <text class="u-btn-text" :style="{'color': theme.color}">分享</text>
- </view>
- <view
- class="u-swipe-del dir-top-wrap main-center"
- @tap.stop="del"
- :style="{
- width: innerBtnWidth / 2 + 'px',
- 'background-color': theme.background
- }"
- >
- <text class="u-btn-text">删除</text>
- </view>
- </movable-view>
- </movable-area>
- </template>
- <script>
- export default {
- props: {
- btnText: {
- type: String,
- default: '删除'
- },
- btnBgColor: {
- type: String,
- default: '#ff0033'
- },
- index: {
- type: [Number, String],
- default: ''
- },
- btnWidth: {
- type: [String, Number],
- default: 200
- },
- disabled: {
- type: Boolean,
- default: false
- },
- show: {
- type: Boolean,
- default: false
- },
- bgColor: {
- type: String,
- default: '#ffffff'
- },
- theme: {
- type: Object
- },
- touch: {
- type: Boolean
- }
- },
- 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.guid()
- }
- },
- computed: {
- movableViewWidth() {
- return (this.movableAreaWidth + this.innerBtnWidth) + 'px';
- },
- innerBtnWidth() {
- return uni.upx2px(this.btnWidth);
- },
- },
- mounted() {
- this.getActionRect();
- },
- methods: {
- guid(len = 32, firstU = true, radix = null) {
- let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
- let uuid = [];
- radix = radix || chars.length;
- if (len) {
- for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
- } else {
- let r;
- uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
- uuid[14] = '4';
- for (let i = 0; i < 36; i++) {
- if (!uuid[i]) {
- r = 0 | Math.random() * 16;
- uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
- }
- }
- }
- if(firstU) {
- uuid.shift();
- return 'u' + uuid.join('');
- } else {
- return uuid.join('');
- }
- },
- del() {
- this.status = false;
- this.moveX = 0;
- this.$emit('del', this.index);
- },
- share() {
- this.status = false;
- this.moveX = 0;
- this.$emit('share', this.index);
- },
- change(e) {
- this.scrollX = e.detail.x;
- },
- close() {
- this.moveX = 0;
- this.status = false;
- },
- open() {
- if(this.disabled) return ;
- this.moveX = - this.btnWidth;
- this.status = true;
- },
- touchend() {
- this.moveX = this.scrollX;
- this.$nextTick(function(){
- if(this.status == false) {
- if(this.scrollX <= -this.innerBtnWidth / 3) {
- this.moveX = -this.innerBtnWidth;
- this.status = true;
- this.emitOpenEvent();
- uni.vibrateShort();
- } else {
- this.moveX = 0;
- this.status = false;
- this.emitCloseEvent();
- }
- } else {
- if(this.scrollX > -this.innerBtnWidth * 2 / 3) {
- this.moveX = 0;
- this.$nextTick(() => {
- this.moveX = 101;
- })
- this.status = false;
- this.emitCloseEvent();
- } else {
- this.moveX = -this.innerBtnWidth;
- this.status = true;
- this.emitOpenEvent();
- }
- }
- })
- },
- emitOpenEvent() {
- this.$emit('open', this.index);
- },
- emitCloseEvent() {
- this.$emit('close', this.index);
- },
- touchstart() {
- },
- getActionRect() {
- const query = uni.createSelectorQuery().in(this);
- query.select('.u-swipe-action').boundingClientRect(data => {
- this.movableAreaWidth = data.width;
- }).exec();
- }
- }
- }
- </script>
- <style scoped lang="scss">
- .u-swipe-action {
- width: auto;
- height: initial;
- position: relative;
- overflow: hidden;
- }
- .u-swipe-view {
- display: flex;
- height: initial;
- position: relative; /* 这一句很关键,覆盖默认的绝对定位 */
- }
- .u-swipe-content {
- flex: 1;
- }
- .u-swipe-del {
- position: relative;
- font-size: 30rpx;
- color: #ffffff;
- }
- .u-btn-text {
- font-size: 30upx;
- text-align: center;
- }
- </style>
|