Vue3+Swiper实现PC端横向滑动拖拽

项目需求要页面可以横向拖动,原本项目里就使用了Swiper,所以决定利用Swiper实现。 选择Swiper的Free模式 / 自由滑动模式,设置后发现Swiper拖动到两边有默认的拉出回弹效果,但是项目不希望有这个效果,查Swiper的文档里相关参数、搜索解决办法和找deepseek给的结果都不好使。 这是deepseek给的答案和网上搜索的结果基本一样。

而自己尝试发现只需要将resistance参数设为true, resistanceRatio设为0就可以实现。 于是决定将此记录一下,方便以后查找。

vue3 复制代码
// HorizontalComp
<script setup lang="ts">
import { Swiper, SwiperSlide } from 'swiper/vue'
import { FreeMode, Mousewheel } from 'swiper/modules'

// 导入样式(核心 + 模块)
import 'swiper/css'
import 'swiper/css/free-mode'

const freeOptions = {
    enabled: true,
    sticky: false,
    momentum: false,
    momentumBounce: false,
    momentumBounceRatio: 0
}

const modules = [FreeMode, Mousewheel];

</script>

<template>
    <div class="swiper-container">
        <swiper
            :modules="modules"
            :slides-per-view="'auto'"
            :free-mode="freeOptions"
            :resistance="true"
            :resistanceRatio="0"
        >
            <swiper-slide>
                <div class="slide-item item-content">
                    <slot></slot>
                </div>
            </swiper-slide>
        </swiper>
    </div>
</template>

<style scoped lang="scss">
.swiper-container {
    width: 100%;
    padding:0;
    -ms-overflow-style: none;  /* IE/Edge */
    scrollbar-width: none;     /* Firefox */
}
.swiper-container::-webkit-scrollbar {
    display: none;  /* Chrome/Safari */
}

.swiper {
    overflow: visible; 
}

.swiper-slide {
    width: auto !important; 
}

.item-content {
    height: 500px;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: flex-start;
    user-select: none;
    cursor: grab;
}
</style>

      

使用

vue 复制代码
<template>
    <HorizontalComp >
        <!-- 测试效果部分 -->
        <div class="content"> 四川的美食之都,美食以川菜为主,口味鲜香麻辣。都江堰老腊肉和白果炖鸡是招牌,还有葱葱卷、冰粉、兔头之类的特色小吃。</div>
    </HorizontalComp>
</template>

<style scoped lang="scss">
.content {
    font-size: 48px;
    white-space: nowrap;
}
</style>
相关推荐
陆枫Larry2 分钟前
一次登录问题排查:接口没错,错的是旧业务逻辑
前端
小小小小宇8 分钟前
大模型打分与采样原理,以及 Pi Agent 核心原理
前端
一位正在转型AI全栈的前端工程师33 分钟前
AI 全栈学习之旅 -Week 5:RAG 知识库问答系统:从零到生产级部署的全栈实践总结
前端·python
OpenTiny社区44 分钟前
从流式消息到多会话:TinyRobot Kit 如何组织 AI 对话数据层
vue.js
光影少年1 小时前
react navite 安卓iOS 打包、签名、环境区分
前端·react native·react.js
coderCN1 小时前
Nodejs 第三十四章 数据库(表达式和函数、子查询和连表)
前端·node.js
ssshooter2 小时前
AI 时代你不能不知道的 git worktree
前端·后端·面试
观测云2 小时前
AI时代的用户访问监测:观测云带你身临其境体验用户与前端UI交互旅程
前端·可观测性·观测云·rum
喵本喵叁肆2 小时前
06-M6-部门过滤与综合研判-从问答机到研判助手
前端·javascript·jquery
卤蛋fg63 小时前
vue表格组件 vxe-table 鼠标滑动表头选择整列与选择整行的配置详解
vue.js