当前位置: 首页 > news >正文

vue3 - swiper插件 实现PC端的 视频滑动功能(仿抖音短视频)

 swiper官网

​​​​​​swiper属性/组件查询


vue中使用swiper

  • 步骤:

npm install swiper 安装

② 基础模板:

<div><swiper class="swiper-box" :direction="'vertical'":grabCursor="true" :mousewheel="true" :mousewheelControl="true" :resistanceRatio = "0":observeParents = "true":options="swiperOption"><!-- 幻灯片内容 --><swiper-slide class="slide-box" v-for="(item, index) in dataList" :key="index"><div><video class="video-box" ref="videos" controls autoplay :videoList="item" :src="item.url" :index="index" ></video></div><div class="info-bar-" >...</div><div class="video-box-right">...</div></swiper-slide></swiper>
</div>
<script>
import { Swiper, SwiperSlide } from 'swiper/vue';
import "swiper/swiper-bundle.css";
export default {name: 'VideoList',components: {Swiper,SwiperSlide,},data() {return {...}}
}
</script>

  如图:


  • 属性:

direction = " 'vertical' " ,滑动方向,vertical 垂直方向。(注:一定要两对引号包裹着,否则不生效,还要给swiper设置实高)

modules = "modules"

grabCursor="true",鼠标手掌形状,拖动时变抓手形状

slidesPerView = "1",slider容器能够同时显示的slides数量(carousel模式)

mousewheel = "true",鼠标滚轮控制Swiper切换

setWrapperSize="true",swiper使用flexbox布局,这个会在wrapper上   添加等于slides相加的宽和高

observeParents = "true",当Swiper 的祖先元素发生变化时,例如show/hide、第一级子元素增加/删除等,则更新Swiper 并触发 observerUpdate 事件

resistanceRatio: 0,抵抗率。边缘抵抗力的大小比例。值越小抵抗越大越难将slide拖离边缘,0时完全无法拖离。

navigation="true",导航栏

autoplay = "true",自动播放

loop = "true",无限循环播放

space-between,每张轮播图之间的距离,该属性不可以和margin属性同时使用。

pagination = "{ clickable: true }",分页器(是否点击圆点切换轮播)

注:在标签上格式为    :属性=”值“,如 :direction="vertical"

        在 js 中格式为  属性:值 ,如 direction:verical


  •   完整代码:

<script>
import { Swiper, SwiperSlide } from 'swiper/vue';
import "swiper/swiper-bundle.css";
export default {name: 'VideoList',components: {Swiper,SwiperSlide,},data() {return {page: 1,swiperOption: {on: {// tap方法 是swiper组件提供的点击方法tap: () => {this.playAction(this.page - 1)},// 上滑(屏幕向上滑时,跳到下一个视频)slideNextTransitionStart: () => {this.page += 1;this.nextVideo(this.page - 1)console.log(this.page);},// 下滑(屏幕向下滑时)slidePrevTransitionEnd: ()=> {if(this.page > 1){this.page -= 1;this.prevVideo(this.page - 1)console.log(this.page);}},}}}},methods: {playAction(index) {     // 入参 是 当前屏幕显示的是第几个视频this.$refs.videos[index].playOrStop();},// 向下nextVideo(index){this.$refs.videos[index-1].stop()this.$refs.videos[index].play()},// 向上prevVideo(index){this.$refs.videos[index+1].stop()this.$refs.videos[index].play()},},setup(){}
}
</script><script setup>
import { ref } from 'vue';const dataList = [{id: "1",url:"http://s32p8v831.hd-bkt.clouddn.com/2023-09-23%2012.00.00_%23%E5%B4%A9%E5%9D%8F%E6%98%9F%E7%A9%B9%E9%93%81%E9%81%93__%23%E8%82%AF%E5%BE%B7%E5%9F%BA%E6%98%9F%E7%A9%B9%E9%93%81%E9%81%93__%23%E5%B8%AE%E5%B8%AE%E6%88%91%E8%82%AF%E5%BE%B7%E5%9F%BA%E7%88%B7%E7%88%B7___%E7%82%B8%E9%B8%A1_%E6%89%A7%E8%A1%8C__KI%E4%B8%8A%E6%A0%A1%E7%AB%9F%E7%84%B6%E4%B9%9F%E6%9C%89%E9%9A%90%E8%97%8F%E7%9A%84%E5%8A%9B%E9%87%8F___KI%E4%B8%8A%E6%A0%A1%E6%88%90%E5%8A%9F%E5%8F%91%E5%8A%A8%E8%82%AF%E5%BE%B7%E5%9F%BA%E7%BB%88%E7%BB%93%E6%8A%80_%E7%8E%B0%E5%9C%BA%E8%B6%85%E9%9C%87%E6%92%BC%E8%A7%86%E9%A2%91%E4%BC%A0%E5%9B%9E_%E4%B8%80%E8%B5%B7%E6%9D%A5%E7%9C%8B%E7%9C%8B%E5%B8%95__video-transcode..mp4",},{id: "2",url:"http://s32p8v831.hd-bkt.clouddn.com/2023-09-21%2021.00.08_%23%E8%82%AF%E5%BE%B7%E5%9F%BA%E7%87%83%E6%83%85%E4%BA%9A%E8%BF%90%E5%AD%A3_%E5%92%8C%E5%8F%AF%E7%88%B1%E7%9A%84%E5%90%89%E7%A5%A5%E7%89%A9_%E5%B0%8F%E6%9C%8B%E5%8F%8B%E4%BB%AC%E4%B8%80%E8%B5%B7%E8%B7%B3%E4%BA%9A%E8%BF%90%E5%8A%A0%E6%B2%B9%E6%93%8D_%E4%B8%BA%E4%BA%9A%E8%BF%90%E5%8A%A0%E6%B2%B9%E5%8A%A9%E5%A8%81__video-transcode..mp4",},
]</script>
<template><div class="swiper-wrapper"><swiper class="swiper-box" :direction="'vertical'":grabCursor="true" :mousewheel="true" :mousewheelControl="true" :resistanceRatio = "0":observeParents = "true":options="swiperOption"><!-- 幻灯片内容 --><swiper-slide class="slide-box" v-for="(item, index) in dataList" :key="index"><div><video class="video-box" controls ref="videos" :videoList="item" :src="item.url" :index="index" autoplay></video></div><div class="infobar_warp" ><InfoBar :info-data="item" /></div><div class="video-box-right"><LikeComponent :like-data="item"></LikeComponent></div></swiper-slide></swiper></div>
</template><style scoped lang="scss">
.swiper-wrapper {/* width: 100vw ;  */height: calc(100vh - 100px);.swiper-box {width: 100%;.slide-box {.video-box {width: 100%;height: calc(100vh - 100px);}.video-box-right {position: absolute;right: 0;bottom: 30vh;top: 30vh;/* z-index: 80; */}.infobar_warp {position: absolute;bottom: 55px;left: 0;}}}
}
</style>
  • 比较完整的swiper教程:

vue3中使用swiper7轮播图插件_swiper7实例_爱唱歌的前端的博客-CSDN博客

vue3中使用swiper完整版教程_swiper vue3-CSDN博客

如何在vue3中使用swiper插件(教程)_vue3 安装swiper-CSDN博客


滑动功能 实现参考文章:

vue实战 实现视频类webapp:(十二) 视频评论列表 - 小专栏vue项目中使用swiper(版本3.1.3)实现视频轮播_vue 视频轮播-CSDN博客

基于vue封装移动触摸滑动组件——简易的swiper_vue 滑动-CSDN博客

vue+swiper仿抖音视频滑动_swiper + video 短视频-CSDN博客 (多个播放按钮)

相关文章:

  • 宠物社区系统宠物领养小程序,宠物救助小程序系统多少钱?
  • 【打卡】牛客网:BM48 数据流中的中位数
  • OpenAI调查ChatGPT故障;向量搜索的优势与局限
  • 小程序微信登录授权突然没反应的原因和解决方案
  • 数据结构之双向链表
  • 在以BUF,字节存储区中,存放有n个带符号整数。试编写统计其中负偶数个数(假设≤9)并且显示。
  • 【Spring Boot 源码学习】初识 SpringApplication
  • 【开题报告】基于SpringBoot的高校实验室管理系统的设计与实现
  • Springboot项目部署及多环境开发
  • AlGaN/GaN HFET 五参数模型
  • deployment.yaml文件详解
  • Python Selenium元素定位方法详解
  • 【网络】TCP协议理论
  • idea报错java: 程序包com.alibaba.fastjson不存在,明明存在!
  • 操作系统——内存管理(一文搞懂操作系统的内存管理)
  • 时间复杂度分析经典问题——最大子序列和
  • __proto__ 和 prototype的关系
  • 【干货分享】SpringCloud微服务架构分布式组件如何共享session对象
  • 5分钟即可掌握的前端高效利器:JavaScript 策略模式
  • echarts花样作死的坑
  • js写一个简单的选项卡
  • k8s 面向应用开发者的基础命令
  • magento 货币换算
  • vue2.0开发聊天程序(四) 完整体验一次Vue开发(下)
  • 分布式熔断降级平台aegis
  • 分享一个自己写的基于canvas的原生js图片爆炸插件
  • 前端面试题总结
  • 体验javascript之美-第五课 匿名函数自执行和闭包是一回事儿吗?
  • 详解移动APP与web APP的区别
  • 小程序开发之路(一)
  • Play Store发现SimBad恶意软件,1.5亿Android用户成受害者 ...
  • 函数计算新功能-----支持C#函数
  • 智能情侣枕Pillow Talk,倾听彼此的心跳
  • #Linux杂记--将Python3的源码编译为.so文件方法与Linux环境下的交叉编译方法
  • (1)(1.9) MSP (version 4.2)
  • (30)数组元素和与数字和的绝对差
  • (8)Linux使用C语言读取proc/stat等cpu使用数据
  • (pt可视化)利用torch的make_grid进行张量可视化
  • (Redis使用系列) Springboot 在redis中使用BloomFilter布隆过滤器机制 六
  • (八十八)VFL语言初步 - 实现布局
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (附源码)python房屋租赁管理系统 毕业设计 745613
  • (亲测)设​置​m​y​e​c​l​i​p​s​e​打​开​默​认​工​作​空​间...
  • (十一)手动添加用户和文件的特殊权限
  • (转)3D模板阴影原理
  • (转)利用PHP的debug_backtrace函数,实现PHP文件权限管理、动态加载 【反射】...
  • ***原理与防范
  • .Net 转战 Android 4.4 日常笔记(4)--按钮事件和国际化
  • .net遍历html中全部的中文,ASP.NET中遍历页面的所有button控件
  • .NET开源的一个小而快并且功能强大的 Windows 动态桌面软件 - DreamScene2
  • .NET面试题(二)
  • .Net下使用 Geb.Video.FFMPEG 操作视频文件
  • /proc/interrupts 和 /proc/stat 查看中断的情况
  • @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
  • [ C++ ] STL_stack(栈)queue(队列)使用及其重要接口模拟实现