首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

多个内容滑动轮播图【前端】

  • 25-04-25 00:02
  • 4526
  • 8159
blog.csdn.net

 

前几天写的前端项目当中自己写了一个比较好用的轮播组件,分享给大家 

图片具有点击效果和滑动效果,这里作者放了9张图片,大家有需要可以使用~ 

图片在这里大家可以直接进行测试 

 

 总组件代码:

这里的代码作者用的是Vue3,大家没有学过的可以使用al给你转成正常的js代码~~~ 

  1. <script setup>
  2. import { onMounted, onUnmounted } from 'vue'
  3. onMounted(() => {
  4. const container = document.querySelector('.product_box')
  5. const leftIcon = document.querySelector('.left_icon')
  6. const rightIcon = document.querySelector('.right_icon')
  7. const items1 = document.querySelectorAll('.product_detail')
  8. const itemCount = items1.length
  9. let currentIndex = 0
  10. let isAnimating = false
  11. let autoSlideInterval
  12. let visibleItems = 3 // 初始值
  13. // 初始化
  14. updateCarousel()
  15. // 监听视窗大小变化
  16. window.addEventListener('resize', () => {
  17. updateCarousel()
  18. })
  19. function updateCarousel() {
  20. if (isAnimating) return
  21. isAnimating = true
  22. // 计算位移百分比(每个项目占 1/visibleItems)
  23. const translateX = -currentIndex * (100 / visibleItems / 1.07)
  24. container.style.transform = `translateX(${translateX}%)`
  25. container.style.transition = '1s'
  26. // 重置动画状态
  27. setTimeout(() => {
  28. isAnimating = false
  29. }, 500)
  30. }
  31. function nextSlide1() {
  32. stopAutoSlide()
  33. if (currentIndex >= itemCount - visibleItems) {
  34. // 到达最后时回到第一个
  35. currentIndex = 0
  36. } else {
  37. currentIndex++
  38. }
  39. updateCarousel()
  40. startAutoSlide()
  41. }
  42. function prevSlide1() {
  43. stopAutoSlide()
  44. if (currentIndex <= 0) {
  45. // 到达第一个时跳到最后
  46. currentIndex = itemCount - visibleItems
  47. } else {
  48. currentIndex--
  49. }
  50. updateCarousel()
  51. startAutoSlide()
  52. }
  53. function startAutoSlide() {
  54. autoSlideInterval = setInterval(nextSlide1, 3000) // 每3秒切换一次
  55. }
  56. // 停止自动轮播的函数
  57. function stopAutoSlide() {
  58. if (autoSlideInterval) {
  59. clearInterval(autoSlideInterval)
  60. autoSlideInterval = null
  61. }
  62. }
  63. // 添加按钮事件
  64. rightIcon.addEventListener('click', nextSlide1)
  65. leftIcon.addEventListener('click', prevSlide1)
  66. startAutoSlide()
  67. onUnmounted(() => {
  68. clearInterval(autoSlideInterval)
  69. })
  70. })
  71. script>
  72. <template>
  73. <div class="product_content">
  74. <div class="product_box">
  75. <div class="product_content_box">
  76. <div class="product_detail product_detail_1">
  77. <div class="product_topImg">
  78. <img src="@/assets/image1.png" alt="" />
  79. div>
  80. <div class="product_h">动物头像div>
  81. <div class="product_hr">div>
  82. <div class="product_p">可爱小猫div>
  83. <div class="product_button" @click="toProduct('高压电器产业')">div>
  84. div>
  85. <div class="product_detail">
  86. <div class="product_topImg">
  87. <img src="@/assets/image2.png" alt="" />
  88. div>
  89. <div class="product_h">动物头像div>
  90. <div class="product_hr">div>
  91. <div class="product_p">可爱小猫div>
  92. <div class="product_button" @click="toProduct('运维检修业务')">div>
  93. div>
  94. <div class="product_detail">
  95. <div class="product_topImg">
  96. <img src="@/assets/image3.png" alt="" />
  97. div>
  98. <div class="product_h">动物头像div>
  99. <div class="product_hr">div>
  100. <div class="product_p">可爱小猫div>
  101. <div class="product_button" @click="toProduct('零部件制造产业')">div>
  102. div>
  103. <div class="product_detail">
  104. <div class="product_topImg">
  105. <img src="@/assets/image1.png" alt="" />
  106. div>
  107. <div class="product_h">动物头像div>
  108. <div class="product_hr">div>
  109. <div class="product_p">可爱小猫div>
  110. <div class="product_button" @click="toProduct('电力储能业务')">div>
  111. div>
  112. <div class="product_detail">
  113. <div class="product_topImg">
  114. <img src="@/assets/image2.png" alt="" />
  115. div>
  116. <div class="product_h">动物头像div>
  117. <div class="product_hr">div>
  118. <div class="product_p">可爱小猫div>
  119. <div class="product_button" @click="toProduct('配电网产业')">div>
  120. div>
  121. <div class="product_detail">
  122. <div class="product_topImg">
  123. <img src="@/assets/image1.png" alt="" />
  124. div>
  125. <div class="product_h">动物头像div>
  126. <div class="product_hr">div>
  127. <div class="product_p">可爱小猫div>
  128. <div class="product_button" @click="toProduct('系统集成业务')">div>
  129. div>
  130. <div class="product_detail">
  131. <div class="product_topImg">
  132. <img src="@/assets/image2.png" alt="" />
  133. div>
  134. <div class="product_h">动物头像div>
  135. <div class="product_hr">div>
  136. <div class="product_p">可爱小猫div>
  137. <div class="product_button" @click="toProduct('智慧配用电业务')">div>
  138. div>
  139. <div class="product_detail">
  140. <div class="product_topImg">
  141. <img src="@/assets/image3.png" alt="" />
  142. div>
  143. <div class="product_h">动物头像div>
  144. <div class="product_hr">div>
  145. <div class="product_p">可爱小猫div>
  146. <div class="product_button" @click="toProduct('综合能源服务业务')">div>
  147. div>
  148. div>
  149. div>
  150. <div class="left_icon"><span class="icon iconfont">span>div>
  151. <div class="right_icon"><span class="icon iconfont">span>div>
  152. div>
  153. template>
  154. <style scoped>
  155. .product_box {
  156. /* overflow: hidden; */
  157. width: 94%;
  158. min-width: 120rem;
  159. margin: auto;
  160. padding-left: 1.875rem;
  161. padding-right: 4.375rem;
  162. }
  163. .product_content_box {
  164. display: flex;
  165. width: 95%;
  166. margin: auto;
  167. }
  168. .product_detail {
  169. flex-shrink: 0;
  170. width: calc(33.333%);
  171. /* 每个图标占据三分之一的视口宽度 */
  172. height: 43.75rem;
  173. margin: 0.625rem;
  174. color: white;
  175. align-items: center;
  176. justify-content: center;
  177. transition: transform 0.3s ease;
  178. animation: scaleIn 1s ease-in-out;
  179. }
  180. .product_topImg {
  181. position: relative;
  182. width: 26.25rem;
  183. height: 26.25rem;
  184. margin: auto;
  185. cursor: pointer;
  186. z-index: 1000;
  187. animation: floatAnimation 3s ease-in-out infinite;
  188. }
  189. .product_topImg img {
  190. width: 100%;
  191. height: 100%;
  192. margin-left: -0.3125rem;
  193. margin: auto;
  194. }
  195. @keyframes floatAnimation {
  196. 0%,
  197. 100% {
  198. transform: translateY(0);
  199. }
  200. 50% {
  201. transform: translateY(-1.5625rem);
  202. }
  203. }
  204. .product_topImg:before {
  205. content: '';
  206. position: absolute;
  207. top: 0;
  208. left: 0;
  209. width: 100%;
  210. height: 100%;
  211. background-size: cover;
  212. background-position: center;
  213. transition: opacity 1s ease;
  214. /* 设置过渡效果 */
  215. opacity: 0.3;
  216. /* 默认透明 */
  217. z-index: -1;
  218. /* 确保在图片下方 */
  219. }
  220. .product_topImg:hover:before {
  221. background-image: url('@/assets/imgs/_10_homePageImgs/circle.png');
  222. /* 悬停时的背景图片 */
  223. opacity: 1;
  224. }
  225. .product_h {
  226. font-size: 2.5rem;
  227. font-family: 'AlibabaPuHuiTi_2_55_Regular';
  228. color: rgb(0, 111, 193);
  229. text-align: center;
  230. }
  231. .product_hr {
  232. width: 6.625rem;
  233. height: 0.125rem;
  234. background-color: #2081c9;
  235. text-align: center;
  236. margin: 1.25rem 0rem;
  237. margin: auto;
  238. margin-top: 0.625rem;
  239. margin-bottom: 0.9375rem;
  240. }
  241. .product_p {
  242. width: 21.625rem;
  243. height: 4.5rem;
  244. font-size: 1.25rem;
  245. font-family: 'AlibabaPuHuiTi_2_45_Light';
  246. color: rgb(89, 87, 87);
  247. text-align: center;
  248. margin: auto;
  249. }
  250. .product_button {
  251. position: relative;
  252. width: 8.6875rem;
  253. height: 2rem;
  254. margin: auto;
  255. text-align: center;
  256. background-image: url('@/assets/imgs/_10_homePageImgs/button.png');
  257. background-size: contain;
  258. margin-top: 1.875rem;
  259. transition: all 0.3s ease;
  260. cursor: pointer;
  261. background-repeat: no-repeat;
  262. }
  263. .product_button:hover {
  264. transform: scale(1.1);
  265. /* translate(0, -0.3125rem) 使文字在Y轴方向向上移动0.3125rem,scale(1.1) 使文字放大到原来的1.1倍 */
  266. color: #58ffa9;
  267. /* 改变文字颜色 */
  268. }
  269. .product_button span {
  270. position: absolute;
  271. top: 0;
  272. left: 2.1rem;
  273. font-size: 1.125rem;
  274. line-height: 2rem;
  275. font-family: 'AlibabaPuHuiTi_2_45_Light';
  276. color: rgb(0, 111, 193);
  277. }
  278. .left_icon .icon {
  279. top: 10rem;
  280. left: 3.75rem;
  281. position: absolute;
  282. font-size: 4.375rem;
  283. color: #006fc1;
  284. z-index: 100;
  285. cursor: pointer;
  286. }
  287. .right_icon .icon {
  288. position: absolute;
  289. font-size: 4.375rem;
  290. top: 10rem;
  291. right: 3.125rem;
  292. color: #006fc1;
  293. z-index: 100;
  294. cursor: pointer;
  295. }
  296. style>

 

注:本文转载自blog.csdn.net的心.c的文章"https://blog.csdn.net/2301_81253185/article/details/146460528"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

104
前端
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2024 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top