uni-app 的滑动效果

1、有的朋友说,滑动不显示,一定要注意以下几点:

1) 滑动组件的引用:

 

下面在index.vue 中的代码

<template>
    <view>
         <uni-section
            title="基本用法"
            type="line"
        ></uni-section>
        <uni-swipe-action>
            <uni-swipe-action-item
                :left-options="options2"
                :threshold="0"
                :right-options="options1"
                @click="bindClick"
            >
                <view class="content-box">
                    <text class="content-text">使用数据填充</text>
                </view>
            </uni-swipe-action-item>
            <uni-swipe-action-item @click="bindClick">
                <template v-slot:left>
                    <view class="slot-button">
                        <text
                            class="slot-button-text"
                            @click="bindClick({position:'left',content:{text:'置顶'}})"
                        >置顶</text>
                    </view>
                </template>
                <view class="content-box">
                    <text class="content-text">使用插槽</text>
                </view>
                <template v-slot:right>
                    <view class="slot-button"><text class="slot-button-text">删除</text></view>
                </template>
            </uni-swipe-action-item>
            <uni-swipe-action-item
                :right-options="options1"
                @click="bindClick"
            >
                <template v-slot:left>
                    <view class="slot-button"><text
                            class="slot-button-text"
                            @click="bindClick({position:'left',content:{text:'置顶'}})"
                        >置顶</text></view>
                </template>
                <view class="content-box">
                    <text class="content-text">混合使用</text>
                </view>
            </uni-swipe-action-item>
        </uni-swipe-action>
        <uni-section
            title="禁止滑动"
            type="line"
        ></uni-section>
        <uni-swipe-action>
            <uni-swipe-action-item :disabled="true">
                <view class="content-box">
                    <text class="content-text">禁止左右滚动</text>
                </view>
            </uni-swipe-action-item>
        </uni-swipe-action>
        <uni-section
            title="使用变量控制开关"
            type="line"
        ></uni-section>
        <view class="example-body">
            <view
                class="button"
                @click="setOpened"
            >
                <text class="button-text">当前状态:{{ isOpened }}</text>
            </view>
        </view>
        <uni-swipe-action>
            <uni-swipe-action-item
                :left-options="options2"
                :right-options="options2"
                :show="isOpened"
                :auto-close="false"
                @change="change"
                @click="bindClick"
            >
                <view class="content-box">
                    <text class="content-text">使用变量控制SwipeAction的开启状态</text>
                </view>
            </uni-swipe-action-item>
        </uni-swipe-action>
 
        <uni-section
            title="swipe-action 列表"
            type="line"
        ></uni-section>
        <uni-swipe-action>
            <uni-swipe-action-item
                v-for="(item, index) in swipeList"
                :right-options="item.options"
                :key="item.id"
                @change="swipeChange($event, index)"
                @click="swipeClick($event, index)"
            >
                <view class="content-box">
                    <text class="content-text">{{ item.content }}</text>
                </view>
            </uni-swipe-action-item>
        </uni-swipe-action>
    </view>
</template>

<script>
    export default {
        components: {},
        data() {
            return {
                show: false,
                isOpened: 'none',
                options1: [{
                    text: '取消置顶'
                }],
                options2: [{
                        text: '取消',
                        style: {
                            backgroundColor: '#007aff'
                        }
                    },
                    {
                        text: '确认',
                        style: {
                            backgroundColor: '#dd524d'
                        }
                    }
                ],
                swipeList: []
            };
        },
        onReady() {
            // 模拟延迟赋值
            setTimeout(() => {
                this.isOpened = 'right';
                this.swipeList = [{
                        options: [{
                            text: '添加',
                            style: {
                                backgroundColor: 'rgb(255,58,49)'
                            }
                        }],
                        id: 0,
                        content: 'item1'
                    },
                    {
                        id: 1,
                        options: [{
                                text: '置顶'
                            },
                            {
                                text: '删除',
                                style: {
                                    backgroundColor: 'rgb(255,58,49)'
                                }
                            }
                        ],
                        content: 'item2'
                    },
                    {
                        id: 2,
                        options: [{
                                text: '置顶'
                            },
                            {
                                text: '标记为已读',
                                style: {
                                    backgroundColor: 'rgb(254,156,1)'
                                }
                            },
                            {
                                text: '删除',
                                style: {
                                    backgroundColor: 'rgb(255,58,49)'
                                }
                            }
                        ],
                        content: 'item3'
                    }
                ]
            }, 1000);
        },
        methods: {
            bindClick(e) {
                uni.showToast({
                    title: `点击了${e.position === 'left' ? '左侧' : '右侧'} ${e.content.text}按钮`,
                    icon: 'none'
                });
            },
            setOpened() {
                if (this.isOpened === 'none') {
                    this.isOpened = 'left';
                    return;
                }
                if (this.isOpened === 'left') {
                    this.isOpened = 'right';
                    return;
                }
                if (this.isOpened === 'right') {
                    this.isOpened = 'none';
                    return;
                }
            },
            change(e) {
                this.isOpened = e;
                console.log('返回:', e);
            },
            swipeChange(e, index) {
                console.log('返回:', e);
                console.log('当前索引:', index);
            },
            swipeClick(e, index) {
                let {
                    content
                } = e;
                if (content.text === '删除') {
                    uni.showModal({
                        title: '提示',
                        content: '是否删除',
                        success: res => {
                            if (res.confirm) {
                                this.swipeList.splice(index, 1);
                            } else if (res.cancel) {
                                console.log('用户点击取消');
                            }
                        }
                    });
                } else if (content.text === '添加') {
                    if (this.swipeList.length < 10) {
                        this.swipeList.push({
                            id: new Date().getTime(),
                            options: [{
                                    text: '置顶'
                                },
                                {
                                    text: '标记为已读',
                                    style: {
                                        backgroundColor: 'rgb(254,156,1)'
                                    }
                                },
                                {
                                    text: '删除',
                                    style: {
                                        backgroundColor: 'rgb(255,58,49)'
                                    }
                                }
                            ],
                            content: '新增' + new Date().getTime()
                        });
                        uni.showToast({
                            title: `添加了一条数据`,
                            icon: 'none'
                        });
                    } else {
                        uni.showToast({
                            title: `最多添加十条数据`,
                            icon: 'none'
                        });
                    }
                } else {
                    uni.showToast({
                        title: `点击了${e.content.text}按钮`,
                        icon: 'none'
                    });
                }
            }
        }
    };
</script>

<style lang="scss">
    @import '@/common/uni-nvue.scss';

    .content-box {
        flex: 1;
        /* #ifdef APP-NVUE */
        justify-content: center;
        /* #endif */
        height: 44px;
        line-height: 44px;
        padding: 0 15px;
        position: relative;
        background-color: #fff;
        border-bottom-color: #f5f5f5;
        border-bottom-width: 1px;
        border-bottom-style: solid;
    }

    .content-text {
        font-size: 15px;
    }

    .example-body {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: row;
        justify-content: center;
        padding: 10px 0;
        background-color: #fff;
    }

    .button {
        border-color: #e5e5e5;
        border-style: solid;
        border-width: 1px;
        padding: 4px 8px;
        border-radius: 4px;
    }

    .button-text {
        font-size: 15px;
    }

    .slot-button {
        /* #ifndef APP-NVUE */
        display: flex;
        height: 100%;
        /* #endif */
        flex: 1;
        flex-direction: row;
        justify-content: center;
        align-items: center;
        padding: 0 20px;
        background-color: #ff5a5f;
    }

    .slot-button-text {
        color: #ffffff;
        font-size: 14px;
    }
</style>
2) css 中的定位一定要注意了,定位的css 如果没有对应的,也不出出现滑动效果

 

希望给你大家在前端编程的帮助!

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现仿抖音视频滑动效果,你可以使用uni-app框架中的swiper组件结合相关的动画效果来实现。下面是一种可能的实现方式: 1. 在你的uni-app项目中,使用swiper组件创建一个滑动容器,设置为横向滑动。 ```html <swiper class="swiper" :autoplay="false" :indicator-dots="false" :circular="true"> <swiper-item v-for="(item, index) in videoList" :key="index"> <video :src="item.url" autoplay muted loop></video> </swiper-item> </swiper> ``` 2. 使用css样式来设置容器的布局和样式。 ```css .swiper { width: 100%; height: 100%; overflow: hidden; } .swiper-item { width: 100%; height: 100%; } video { width: 100%; height: 100%; object-fit: cover; } ``` 3. 使用JavaScript或者Vue的生命周期钩子函数来监听滑动事件,并根据滑动的距离和方向来实现动画效果。 ```javascript export default { data() { return { videoList: [...], // 视频列表数据 startX: 0, // 触摸起始点的X坐标 startY: 0, // 触摸起始点的Y坐标 moveX: 0, // 触摸移动中的X坐标 moveY: 0, // 触摸移动中的Y坐标 direction: '', // 滑动方向 currentIndex: 0 // 当前显示的视频索引 }; }, methods: { onTouchStart(e) { this.startX = e.changedTouches[0].pageX; this.startY = e.changedTouches[0].pageY; }, onTouchMove(e) { this.moveX = e.changedTouches[0].pageX; this.moveY = e.changedTouches[0].pageY; const offsetX = this.moveX - this.startX; const offsetY = this.moveY - this.startY; if (Math.abs(offsetY) < Math.abs(offsetX)) { // 水平滑动 if (offsetX > 0) { this.direction = 'right'; } else { this.direction = 'left'; } } else { // 垂直滑动 if (offsetY > 0) { this.direction = 'down'; } else { this.direction = 'up'; } } }, onTouchEnd() { if (this.direction === 'left') { // 向左滑动,切换到下一个视频 this.currentIndex++; if (this.currentIndex >= this.videoList.length) { this.currentIndex = 0; } } else if (this.direction === 'right') { // 向右滑动,切换到上一个视频 this.currentIndex--; if (this.currentIndex < 0) { this.currentIndex = this.videoList.length - 1; } } // 根据currentIndex更新swiper组件的activeIndex属性,实现视图切换 this.$refs.swiper.swiperRef.setActiveItem(this.currentIndex); } } }; ``` 上述代码中,通过监听触摸事件,根据滑动方向切换到对应的视频,并将视图更新到当前的视频。 这是一种简单的实现方式,你可以根据自己的需求进行扩展和优化。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值