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

50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | DoubleVerticalSlider(双垂直滑块)

📅 我们继续 50 个小项目挑战!—— DoubleVerticalSlider组件

仓库地址:https://github.com/SunACong/50-vue-projects

项目预览地址:https://50-vue-projects.vercel.app/

在这里插入图片描述


使用 Vue 3 的 Composition API(<script setup>)结合 TailwindCSS 创建一个全屏、左右联动的垂直轮播组件。左侧为文字内容,右侧为图片展示,两者通过按钮控制上下滑动切换。

这种设计非常适合用于企业官网首页、产品介绍页、作品集展示等需要视觉冲击力的页面。


🎯 组件目标

  • 创建一个全屏垂直轮播组件
  • 左右区域内容联动滑动
  • 支持点击按钮切换当前 Slide
  • 使用 Vue 响应式变量管理状态
  • 使用 TailwindCSS 快速构建布局和动画
  • 实现优雅的滑动过渡效果

⚙️ 技术实现点

技术点描述
Vue 3 <script setup>使用响应式变量管理当前 slide 索引
ref 响应式变量控制当前激活的 slide 索引 (activeSlideIndex)
动态类绑定 :class控制当前 slide 的层级和动画状态
内联样式绑定 :style动态设置每个 slide 的背景色或图片及位移动画
按钮事件绑定 @click触发上一页/下一页切换
TailwindCSS 布局类构建全屏容器、左右分栏、居中对齐
TailwindCSS 过渡类添加平滑的滑动动画

🧱 组件实现

模板结构 <template>

<template><div class="relative flex h-screen w-screen overflow-hidden"><!-- 左侧 --><div class="relative h-full w-[35%] overflow-hidden"><divv-for="(slide, index) in leftSlides":key="index"class="absolute inset-0 flex flex-col items-center justify-center text-white transition-all duration-500 ease-in-out":class="[index === activeSlideIndex ? 'z-10' : 'pointer-events-none z-0']":style="{backgroundColor: slide.bgColor,transform: `translateY(${-(index - activeSlideIndex) * 100}%)`,}"><h1 class="-mt-8 mb-2 text-4xl">{{ slide.title }}</h1><p class="text-lg">{{ slide.description }}</p></div><!-- 向下按钮 --><div class="absolute top-1/2 right-0 z-20 -translate-y-1/2"><buttonclass="rounded-l-md bg-white p-4 text-gray-500 shadow hover:text-black"@click="changeSlide('down')">👇</button></div></div><!-- 右侧 --><div class="relative h-full w-[65%] overflow-hidden"><divv-for="(slide, index) in rightSlides":key="index"class="absolute inset-0 h-full w-full bg-cover bg-center bg-no-repeat transition-all duration-500 ease-in-out":class="[index === activeSlideIndex ? 'z-10' : 'pointer-events-none z-0']":style="{backgroundImage: `url(${slide.imageUrl})`,transform: `translateY(${(index - activeSlideIndex) * 100}%)`,}"></div><!-- 向上按钮 --><div class="absolute top-1/2 left-0 z-20 -translate-y-1/2"><buttonclass="rounded-r-md bg-white p-4 text-gray-500 shadow hover:text-black"@click="changeSlide('up')">👆</button></div></div></div>
</template>

脚本逻辑 <script setup>

<script setup>
import { ref } from 'vue'const activeSlideIndex = ref(0)const leftSlides = [{ title: 'Flying eagle', description: 'in the sunset', bgColor: '#FFB866' },{ title: 'Lonely castle', description: 'in the wilderness', bgColor: '#252E33' },{ title: 'Bluuue Sky', description: "with it's mountains", bgColor: '#2A86BA' },{ title: 'Nature flower', description: 'all in pink', bgColor: '#FD3555' },
]const rightSlides = [{imageUrl:'https://images.unsplash.com/photo-1508768787810-6adc1f613514?auto=format&fit=crop&w=1350&q=80',},{imageUrl:'https://images.unsplash.com/photo-1519981593452-666cf05569a9?auto=format&fit=crop&w=715&q=80',},{imageUrl:'https://images.unsplash.com/photo-1486899430790-61dbf6f6d98b?auto=format&fit=crop&w=1002&q=80',},{imageUrl:'https://images.unsplash.com/photo-1510942201312-84e7962f6dbb?auto=format&fit=crop&w=1050&q=80',},
]function changeSlide(direction) {if (direction === 'up') {activeSlideIndex.value = (activeSlideIndex.value + 1) % leftSlides.length} else {activeSlideIndex.value =(activeSlideIndex.value - 1 + leftSlides.length) % leftSlides.length}
}
</script>

🔍 重点效果实现

✅ 全屏容器布局

我们使用了以下结构创建全屏容器:

<div class="relative flex h-screen w-screen overflow-hidden">

这样可以确保整个组件占满浏览器视口,同时防止滚动条出现。

💡 左右区域联动滑动

通过 transform: translateY(...) 来实现滑动动画:

transform: `translateY(${-(index - activeSlideIndex) * 100}%)`

左侧向上滑动时,文字向上;右侧向下,图片向上,形成“联动”视觉效果。

🎮 按钮控制滑动方向

通过两个按钮分别控制上一张和下一张:

function changeSlide(direction) {if (direction === 'up') {activeSlideIndex.value = (activeSlideIndex.value + 1) % leftSlides.length} else {activeSlideIndex.value =(activeSlideIndex.value - 1 + leftSlides.length) % leftSlides.length}
}

实现了循环切换功能,避免索引越界。


🎨 TailwindCSS 样式重点讲解

类名作用
flex h-screen w-screen全屏 Flex 容器
overflow-hidden防止内容溢出
absolute inset-0绝对定位,覆盖父容器
transition-all duration-500 ease-in-out平滑动画过渡
bg-cover, bg-center, bg-no-repeat图片自适应背景
text-white, text-4xl文字样式
rounded-l-md, p-4, hover:text-black按钮样式
top-1/2 -translate-y-1/2居中垂直定位按钮

这些 TailwindCSS 类帮助我们快速构建了一个美观、响应式的全屏轮播组件。


📁 数据分离建议(可选)

你可以将 leftSlidesrightSlides 提取到单独的 JSON 文件中,便于维护和国际化:

// slides.js
export const leftSlides = [{ title: 'Flying eagle', description: 'in the sunset', bgColor: '#FFB866' },...
]

并在组件中导入:

import { leftSlides, rightSlides } from '@/data/slides'

📁 常量定义 + 组件路由

constants/index.js 添加组件预览常量:

{id: 26,title: 'Double Vertical Slider',image: 'https://50projects50days.com/img/projects-img/26-double-vertical-slider.png',link: 'DoubleVerticalSlider',},

router/index.js 中添加路由选项:

{path: '/DoubleVerticalSlider',name: 'DoubleVerticalSlider',component: () => import('@/projects/DoubleVerticalSlider.vue'),},


🏁 总结

Vue 3 和 TailwindCSS 的全屏垂直轮播组件不仅实现了左右联动的滑动效果,还展示了如何通过响应式数据驱动 UI 变化。

适合用于需要高视觉表现力的网页场景,如企业主页、产品介绍页、摄影作品展示等。

你可以进一步扩展此组件的功能包括:

  • ✅ 自动播放(定时切换)
  • ✅ 支持键盘上下键切换
  • ✅ 添加分页指示器(圆点或数字)
  • ✅ 支持触摸滑动(移动端适配)
  • ✅ 将组件封装为 <AppCarousel /> 可复用组件

👉 下一篇,我们将完成ToastNotification组件,一个非常有趣的气泡消息通知。🚀

感谢阅读,欢迎点赞、收藏和分享 😊

http://www.lryc.cn/news/583884.html

相关文章:

  • JavaScript 树形菜单总结
  • SoC程序如何使用单例模式运行
  • vue3 el-table 列汉字 排序时排除 null 或空字符串的值
  • 第二章-AIGC入门-AI视频生成:几款实用AI视频生成工具全解析(7/36)
  • 2025年软件测试面试题,精选33道,附答案
  • 数据结构笔记10:排序算法
  • 【办公类-107-01】20250710视频慢速与视频截图
  • 用OpenCV标定相机内参应用示例(C++和Python)
  • window显示驱动开发—XR_BIAS 和 PresentDXGI
  • 图像亮度调整的简单实现
  • 0基础学Python系列【31】 详细讲解Python中SQLAlchemy包的用法:从入门到精通
  • k8s:安装 Helm 私有仓库ChartMuseum、helm-push插件并上传、安装Zookeeper
  • zookeeper etcd区别
  • 在 Mac 上安装 Java 和 IntelliJ IDEA(完整笔记)
  • macOS 上安装 Miniconda + Conda-Forge
  • 算法练习5-原地移除数组中所有的元素
  • 龙迅#LT8619B适用于HDMI转LVDS/RGB,芯片支持视频图像处理,OSD功能.
  • MacOS 终端(Terminal)配置显示日期时间
  • 在Docker中运行macOS的超方便体验!
  • 基于深度学习的自动调制识别网络(持续更新)
  • 【PTA数据结构 | C语言版】顺序队列的3个操作
  • 在 Mac 上使用 Git 拉取项目:完整指南
  • 【macos用镜像站体验】Claude Code入门使用教程和常用命令
  • 029_构造器重载与默认构造器
  • 基于多模态感知的裂缝2D及3D检测方案
  • 【leetcode】2236. 判断根节点是否等于子节点之和
  • git fetch的使用
  • vue3 uniapp 使用ref更新值后子组件没有更新 ref reactive的区别?使用from from -item执行表单验证一直提示没有值
  • TCP 保活(KeepAlive)机制详解
  • STM32F103之ModBus\RS232\RS422\RS485