背景
如上图,实现一个轮播卡片能力,支持手动滑动,手动拖动,已经点击两端切换按钮。
实现
<template><div class="top-menu"><!-- Swiper --><div v-if="headerMenuList.length > 9" class="swiper-button-prev" @click="goPre" /><div :class="['swiper-container', (headerMenuList.length < 10 ? 'swiper-no-swiping':'')]"><div class="swiper-wrapper"><div v-for="(item,index) in headerMenuList" :class="['swiper-slide', (currentIndex === index ? 'activeBtn':''),((headerMenuList && headerMenuList.length) > 9 ? '':'defaultWidth')]" :key="index" @click="clickMenuItem(item,index)"><div class="swiper-slide-container"><div class="swiper-slide-title">{{ item.name }}</div><div class="swiper-slide-content"><div>{{ item.status }}</div><div v-if="item.des">{{ item.des || '' }} {{ item.time || '' }} </div></div></div></div></div></div><div v-if="headerMenuList.length > 9" class="swiper-button-next" @click="goNextPage" /></div>
</template><script>
import Swiper from 'swiper';
import '@/assets/style/swiper.min.css';export default {name: 'TopMenu',filters: {beyondHiding(value) {const regex = /[\u4e00-\u9fa5]/g;if (!value) return '';if (regex.test(value)) {if (value.length > 7) {return value.substring(0, 5) + '...';} else {return value;}} else {if (value.length > 10) {return value.substring(0, 8) + '...';} else {return value;}}}},props: {handleSelectMenu: {type: Function,default: () => {}}},data() {return {swiper: null,currentIndex: 0,windowWidth: window.innerWidth,};},computed: {headerMenuList() {console.log(this.currentTenant, 'this.currentTenantthis.currentTenant');return [{name: '孙悟空',status: 1,}, {name: '孙悟空2',status: 2,des: '较上个月',time: 50}, {name: '孙悟空3',status: 1,}, {name: '孙悟空4',status: 3,des: '大家可能就'}, {name: '孙悟空5',status: 1,}, {name: '孙悟空6',status: 1,}, {name: '孙悟空7',status: 11,}, {name: '孙悟空8',status: 1,des: '平均响应时常',time: 80}, {name: '孙悟空9',status: 1,}, {name: '孙悟空10',status: 12,}, {name: '孙悟空11',status: 1,des: '大家可能得我就测九年级可男可女缴纳哦就弄v额靓女呢哦法令纹'}, {name: '孙悟空12',status: 14,}];},},mounted() {this.initSwiper();},methods: {initSwiper() {this.swiper = new Swiper('.swiper-container', {// 在这里配置Swiper选项,例如自动播放、分页器、导航等loop: false,speed: 50,noSwiping: true,mousewheel: true,slidesPerView: 6,spaceBetween: 16,navigation: {nextEl: '.swiper-button-next',prevEl: '.swiper-button-prev',disabledClass: 'my-button-disabled',},pagination: {el: '.swiper-pagination',clickable: true,}});},goPre() {// 每次跳转10条// console.log(this.currentIndex, "前进");if (this.currentIndex < 10) {this.swiper.slideTo(1);} else if (this.currentIndex > 10 <= 20) {this.swiper.slideTo(1);} else if (this.currentIndex > 20 <= 30) {this.swiper.slideTo(10);} else {return;}// this.swiper.slidePrev()},goNextPage() {// if(this.currentIndex < 10){// this.swiper.slideTo(10)// }else if( 10 < this.currentIndex <= 20){// this.swiper.slideTo(20)// }else if( 20 < this.currentIndex <= 30){// this.swiper.slideTo(30)// }else{// return;// }this.swiper.slideNext();},getQueryVariable() {const url = window.location.href;if (url.includes('#')) {return url.split('#')[0].split('/')[5];} else {return '';}},clickMenuItem(val, index) {this.currentIndex = index;console.log(val, index, '234567');}}
};
</script><style rel="stylesheet/scss" lang="scss" scoped>
.top-menu {width: 100%;height: 85px;position: relative;// border: 1px solid red;
}.swiper-container {width: 100%;margin-left: auto;margin-right: auto;}
.swiper-slide {text-align: center;font-size: 14px;height: 85px;width: 180px !important;cursor: pointer;border-radius: 12px;background: rgba(245, 237, 253, 1);/* Center slide text vertically */display: -webkit-box;display: -ms-flexbox;display: -webkit-flex;display: flex;-webkit-box-pack: center;-ms-flex-pack: center;-webkit-justify-content: center;justify-content: center;-webkit-box-align: center;-ms-flex-align: center;-webkit-align-items: center;align-items: center;transition-property: all;flex-direction:column;
}
.defaultWidth{width:180px;text-overflow: ellipsis;overflow: hidden;white-space: nowrap;
}
.defaultWidthMax{width:180px;text-overflow: ellipsis;overflow: hidden;white-space: nowrap;
}
.swiper-slide-container {width:100%;white-space: nowrap;text-overflow: ellipsis;overflow: hidden;display: flex;flex-direction: column;align-items: start;margin-left: 24px;.swiper-slide-title{color: rgba(27, 29, 31, 1);font-family: Poppins;font-weight: 500;font-size: 12px;}.swiper-slide-content{display: flex;color: rgba(27, 29, 31, 1);font-family: Poppins-SemiBold;font-size: 18px;margin-top: 8px;:nth-child(2){margin-left: 16px;padding: 0px 4px;border-radius: 12px;background: rgba(255, 255, 255, 1);max-width: 129px;white-space: nowrap;text-overflow: ellipsis;overflow: hidden;color: rgba(27, 29, 31, 1);font-family: Poppins;font-weight: 500;font-size: 12px;transform: scale(0.9);}}
}.swiper-button-next:after, .swiper-button-prev:after {font-family: swiper-icons;font-size: 12px;text-transform: none;letter-spacing: 0;text-transform: none;font-variant: initial;line-height: 1;color: rgb(114, 120, 128);font-weight: 500;margin-top: 4px;
}
.swiper-button-next, .swiper-button-prev{width: 32px;height: 32px;border-radius: 32px;border: 0.5px solid rgba(234, 234, 234, 1);box-sizing: border-box;background: rgba(255, 255, 255, 1);// background-color: red;
}
.my-button-disabled{opacity: 0.3;
}
.swiper-button-prev {left: -13px;right: auto;
}
.swiper-button-next{right: -13px;left: auto;
}
</style>
依赖版本
"swiper": "^5.4.5",
swiper.min.css
/*** Swiper 5.4.5* Most modern mobile touch slider and framework with hardware accelerated transitions* http://swiperjs.com** Copyright 2014-2020 Vladimir Kharlampidi** Released under the MIT License** Released on: June 16, 2020*/@font-face {font-family: swiper-icons;src: url("data:application/font-woff;charset=utf-8;base64, d09GRgABAAAAAAZgABAAAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABGRlRNAAAGRAAAABoAAAAci6qHkUdERUYAAAWgAAAAIwAAACQAYABXR1BPUwAABhQAAAAuAAAANuAY7+xHU1VCAAAFxAAAAFAAAABm2fPczU9TLzIAAAHcAAAASgAAAGBP9V5RY21hcAAAAkQAAACIAAABYt6F0cBjdnQgAAACzAAAAAQAAAAEABEBRGdhc3AAAAWYAAAACAAAAAj//wADZ2x5ZgAAAywAAADMAAAD2MHtryVoZWFkAAABbAAAADAAAAA2E2+eoWhoZWEAAAGcAAAAHwAAACQC9gDzaG10e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dURgDt8QeC8PDw7Fpji3fEA4z/PEJ6YOB5hKh4dj3EvXhxPqH/SKUY3rJ7srZ4FZnh1PMAtPhwP6fl2PMJMPDgeQ4rY8YT6Gzao0eAEA409DuggmTnFnOcSCiEiLMgxCiTI6Cq5DZUd3Qmp10vO0LaLTd2cjN4fOumlc7lUYbSQcZFkutRG7g6JKZKy0RmdLY680CDnEJ+UMkpFFe1RN7nxdVpXrC4aTtnaurOnYercZg2YVmLN/d/gczfEimrE/fs/bOuq29Zmn8tloORaXgZgGa78yO9/cnXm2BpaGvq25Dv9S4E9+5SIc9PqupJKhYFSSl47+Qcr1mYNAAAAeNptw0cKwkAAAMDZJA8Q7OUJvkLsPfZ6zFVERPy8qHh2YER+3i/BP83vIBLLySsoKimrqKqpa2hp6+jq6RsYGhmbmJqZSy0sraxtbO3sHRydnEMU4uR6yx7JJXveP7WrDycAAAAAAAH//wACeNpjYGRgYOABYhkgZgJCZgZNBkYGLQZtIJsFLMYAAAw3ALgAeNolizEKgDAQBCchRbC2sFER0YD6qVQiBCv/H9ezGI6Z5XBAw8CBK/m5iQQVauVbXLnOrMZv2oLdKFa8Pjuru2hJzGabmOSLzNMzvutpB3N42mNgZGBg4GKQYzBhYMxJLMlj4GBgAYow/P/PAJJhLM6sSoWKfWCAAwDAjgbRAAB42mNgYGBkAIIbCZo5IPrmUn0hGA0AO8EFTQAA") format("woff");font-weight: 400;font-style: normal
}:root {--swiper-theme-color: #007aff
}.swiper-container {margin-left: auto;margin-right: auto;position: relative;overflow: hidden;list-style: none;padding: 0;z-index: 1
}.swiper-container-vertical>.swiper-wrapper {flex-direction: column
}.swiper-wrapper {position: relative;width: 100%;height: 100%;z-index: 1;display: flex;transition-property: transform;box-sizing: content-box
}.swiper-container-android .swiper-slide,
.swiper-wrapper {transform: translate3d(0px, 0, 0)
}.swiper-container-multirow>.swiper-wrapper {flex-wrap: wrap
}.swiper-container-multirow-column>.swiper-wrapper {flex-wrap: wrap;flex-direction: column
}.swiper-container-free-mode>.swiper-wrapper {transition-timing-function: ease-out;margin: 0 auto
}.swiper-slide {flex-shrink: 0;width: 100%;height: 100%;position: relative;transition-property: transform
}.swiper-slide-invisible-blank {visibility: hidden
}.swiper-container-autoheight,
.swiper-container-autoheight .swiper-slide {height: auto
}.swiper-container-autoheight .swiper-wrapper {align-items: flex-start;transition-property: transform, height
}.swiper-container-3d {perspective: 1200px
}.swiper-container-3d .swiper-cube-shadow,
.swiper-container-3d .swiper-slide,
.swiper-container-3d .swiper-slide-shadow-bottom,
.swiper-container-3d .swiper-slide-shadow-left,
.swiper-container-3d .swiper-slide-shadow-right,
.swiper-container-3d .swiper-slide-shadow-top,
.swiper-container-3d .swiper-wrapper {transform-style: preserve-3d
}.swiper-container-3d .swiper-slide-shadow-bottom,
.swiper-container-3d .swiper-slide-shadow-left,
.swiper-container-3d .swiper-slide-shadow-right,
.swiper-container-3d .swiper-slide-shadow-top {position: absolute;left: 0;top: 0;width: 100%;height: 100%;pointer-events: none;z-index: 10
}.swiper-container-3d .swiper-slide-shadow-left {background-image: linear-gradient(to left, rgba(0, 0, 0, .5), rgba(0, 0, 0, 0))
}.swiper-container-3d .swiper-slide-shadow-right {background-image: linear-gradient(to right, rgba(0, 0, 0, .5), rgba(0, 0, 0, 0))
}.swiper-container-3d .swiper-slide-shadow-top {background-image: linear-gradient(to top, rgba(0, 0, 0, .5), rgba(0, 0, 0, 0))
}.swiper-container-3d .swiper-slide-shadow-bottom {background-image: linear-gradient(to bottom, rgba(0, 0, 0, .5), rgba(0, 0, 0, 0))
}.swiper-container-css-mode>.swiper-wrapper {overflow: auto;scrollbar-width: none;-ms-overflow-style: none
}.swiper-container-css-mode>.swiper-wrapper::-webkit-scrollbar {display: none
}.swiper-container-css-mode>.swiper-wrapper>.swiper-slide {scroll-snap-align: start start
}.swiper-container-horizontal.swiper-container-css-mode>.swiper-wrapper {scroll-snap-type: x mandatory
}.swiper-container-vertical.swiper-container-css-mode>.swiper-wrapper {scroll-snap-type: y mandatory
}:root {--swiper-navigation-size: 44px
}.swiper-button-next,
.swiper-button-prev {position: absolute;top: 50%;width: calc(var(--swiper-navigation-size)/ 44 * 27);height: var(--swiper-navigation-size);margin-top: calc(-1 * var(--swiper-navigation-size)/ 2);z-index: 10;cursor: pointer;display: flex;align-items: center;justify-content: center;color: var(--swiper-navigation-color, var(--swiper-theme-color))
}.swiper-button-next.swiper-button-disabled,
.swiper-button-prev.swiper-button-disabled {opacity: .35;cursor: auto;pointer-events: none
}.swiper-button-next:after,
.swiper-button-prev:after {font-family: swiper-icons;font-size: var(--swiper-navigation-size);text-transform: none !important;letter-spacing: 0;text-transform: none;font-variant: initial;line-height: 1
}.swiper-button-prev,
.swiper-container-rtl .swiper-button-next {left: 10px;right: auto
}.swiper-button-prev:after,
.swiper-container-rtl .swiper-button-next:after {content: 'prev'
}.swiper-button-next,
.swiper-container-rtl .swiper-button-prev {right: 10px;left: auto
}.swiper-button-next:after,
.swiper-container-rtl .swiper-button-prev:after {content: 'next'
}.swiper-button-next.swiper-button-white,
.swiper-button-prev.swiper-button-white {--swiper-navigation-color: #ffffff
}.swiper-button-next.swiper-button-black,
.swiper-button-prev.swiper-button-black {--swiper-navigation-color: #000000
}.swiper-button-lock {display: none
}.swiper-pagination {position: absolute;text-align: center;transition: .3s opacity;transform: translate3d(0, 0, 0);z-index: 10
}.swiper-pagination.swiper-pagination-hidden {opacity: 0
}.swiper-container-horizontal>.swiper-pagination-bullets,
.swiper-pagination-custom,
.swiper-pagination-fraction {bottom: 10px;left: 0;width: 100%
}.swiper-pagination-bullets-dynamic {overflow: hidden;font-size: 0
}.swiper-pagination-bullets-dynamic .swiper-pagination-bullet {transform: scale(.33);position: relative
}.swiper-pagination-bullets-dynamic .swiper-pagination-bullet-active {transform: scale(1)
}.swiper-pagination-bullets-dynamic .swiper-pagination-bullet-active-main {transform: scale(1)
}.swiper-pagination-bullets-dynamic .swiper-pagination-bullet-active-prev {transform: scale(.66)
}.swiper-pagination-bullets-dynamic .swiper-pagination-bullet-active-prev-prev {transform: scale(.33)
}.swiper-pagination-bullets-dynamic .swiper-pagination-bullet-active-next {transform: scale(.66)
}.swiper-pagination-bullets-dynamic .swiper-pagination-bullet-active-next-next {transform: scale(.33)
}.swiper-pagination-bullet {width: 8px;height: 8px;display: inline-block;border-radius: 100%;background: #000;opacity: .2
}button.swiper-pagination-bullet {border: none;margin: 0;padding: 0;box-shadow: none;-webkit-appearance: none;-moz-appearance: none;appearance: none
}.swiper-pagination-clickable .swiper-pagination-bullet {cursor: pointer
}.swiper-pagination-bullet-active {opacity: 1;background: var(--swiper-pagination-color, var(--swiper-theme-color))
}.swiper-container-vertical>.swiper-pagination-bullets {right: 10px;top: 50%;transform: translate3d(0px, -50%, 0)
}.swiper-container-vertical>.swiper-pagination-bullets .swiper-pagination-bullet {margin: 6px 0;display: block
}.swiper-container-vertical>.swiper-pagination-bullets.swiper-pagination-bullets-dynamic {top: 50%;transform: translateY(-50%);width: 8px
}.swiper-container-vertical>.swiper-pagination-bullets.swiper-pagination-bullets-dynamic .swiper-pagination-bullet {display: inline-block;transition: .2s transform, .2s top
}.swiper-container-horizontal>.swiper-pagination-bullets .swiper-pagination-bullet {margin: 0 4px
}.swiper-container-horizontal>.swiper-pagination-bullets.swiper-pagination-bullets-dynamic {left: 50%;transform: translateX(-50%);white-space: nowrap
}.swiper-container-horizontal>.swiper-pagination-bullets.swiper-pagination-bullets-dynamic .swiper-pagination-bullet {transition: .2s transform, .2s left
}.swiper-container-horizontal.swiper-container-rtl>.swiper-pagination-bullets-dynamic .swiper-pagination-bullet {transition: .2s transform, .2s right
}.swiper-pagination-progressbar {background: rgba(0, 0, 0, .25);position: absolute
}.swiper-pagination-progressbar .swiper-pagination-progressbar-fill {background: var(--swiper-pagination-color, var(--swiper-theme-color));position: absolute;left: 0;top: 0;width: 100%;height: 100%;transform: scale(0);transform-origin: left top
}.swiper-container-rtl .swiper-pagination-progressbar .swiper-pagination-progressbar-fill {transform-origin: right top
}.swiper-container-horizontal>.swiper-pagination-progressbar,
.swiper-container-vertical>.swiper-pagination-progressbar.swiper-pagination-progressbar-opposite {width: 100%;height: 4px;left: 0;top: 0
}.swiper-container-horizontal>.swiper-pagination-progressbar.swiper-pagination-progressbar-opposite,
.swiper-container-vertical>.swiper-pagination-progressbar {width: 4px;height: 100%;left: 0;top: 0
}.swiper-pagination-white {--swiper-pagination-color: #ffffff
}.swiper-pagination-black {--swiper-pagination-color: #000000
}.swiper-pagination-lock {display: none
}.swiper-scrollbar {border-radius: 10px;position: relative;-ms-touch-action: none;background: rgba(0, 0, 0, .1)
}.swiper-container-horizontal>.swiper-scrollbar {position: absolute;left: 1%;bottom: 3px;z-index: 50;height: 5px;width: 98%
}.swiper-container-vertical>.swiper-scrollbar {position: absolute;right: 3px;top: 1%;z-index: 50;width: 5px;height: 98%
}.swiper-scrollbar-drag {height: 100%;width: 100%;position: relative;background: rgba(0, 0, 0, .5);border-radius: 10px;left: 0;top: 0
}.swiper-scrollbar-cursor-drag {cursor: move
}.swiper-scrollbar-lock {display: none
}.swiper-zoom-container {width: 100%;height: 100%;display: flex;justify-content: center;align-items: center;text-align: center
}.swiper-zoom-container>canvas,
.swiper-zoom-container>img,
.swiper-zoom-container>svg {max-width: 100%;max-height: 100%;object-fit: contain
}.swiper-slide-zoomed {cursor: move
}.swiper-lazy-preloader {width: 42px;height: 42px;position: absolute;left: 50%;top: 50%;margin-left: -21px;margin-top: -21px;z-index: 10;transform-origin: 50%;animation: swiper-preloader-spin 1s infinite linear;box-sizing: border-box;border: 4px solid var(--swiper-preloader-color, var(--swiper-theme-color));border-radius: 50%;border-top-color: transparent
}.swiper-lazy-preloader-white {--swiper-preloader-color: #fff
}.swiper-lazy-preloader-black {--swiper-preloader-color: #000
}@keyframes swiper-preloader-spin {100% {transform: rotate(360deg)}
}.swiper-container .swiper-notification {position: absolute;left: 0;top: 0;pointer-events: none;opacity: 0;z-index: -1000
}.swiper-container-fade.swiper-container-free-mode .swiper-slide {transition-timing-function: ease-out
}.swiper-container-fade .swiper-slide {pointer-events: none;transition-property: opacity
}.swiper-container-fade .swiper-slide .swiper-slide {pointer-events: none
}.swiper-container-fade .swiper-slide-active,
.swiper-container-fade .swiper-slide-active .swiper-slide-active {pointer-events: auto
}.swiper-container-cube {overflow: visible
}.swiper-container-cube .swiper-slide {pointer-events: none;-webkit-backface-visibility: hidden;backface-visibility: hidden;z-index: 1;visibility: hidden;transform-origin: 0 0;width: 100%;height: 100%
}.swiper-container-cube .swiper-slide .swiper-slide {pointer-events: none
}.swiper-container-cube.swiper-container-rtl .swiper-slide {transform-origin: 100% 0
}.swiper-container-cube .swiper-slide-active,
.swiper-container-cube .swiper-slide-active .swiper-slide-active {pointer-events: auto
}.swiper-container-cube .swiper-slide-active,
.swiper-container-cube .swiper-slide-next,
.swiper-container-cube .swiper-slide-next+.swiper-slide,
.swiper-container-cube .swiper-slide-prev {pointer-events: auto;visibility: visible
}.swiper-container-cube .swiper-slide-shadow-bottom,
.swiper-container-cube .swiper-slide-shadow-left,
.swiper-container-cube .swiper-slide-shadow-right,
.swiper-container-cube .swiper-slide-shadow-top {z-index: 0;-webkit-backface-visibility: hidden;backface-visibility: hidden
}.swiper-container-cube .swiper-cube-shadow {position: absolute;left: 0;bottom: 0px;width: 100%;height: 100%;background: #000;opacity: .6;-webkit-filter: blur(50px);filter: blur(50px);z-index: 0
}.swiper-container-flip {overflow: visible
}.swiper-container-flip .swiper-slide {pointer-events: none;-webkit-backface-visibility: hidden;backface-visibility: hidden;z-index: 1
}.swiper-container-flip .swiper-slide .swiper-slide {pointer-events: none
}.swiper-container-flip .swiper-slide-active,
.swiper-container-flip .swiper-slide-active .swiper-slide-active {pointer-events: auto
}.swiper-container-flip .swiper-slide-shadow-bottom,
.swiper-container-flip .swiper-slide-shadow-left,
.swiper-container-flip .swiper-slide-shadow-right,
.swiper-container-flip .swiper-slide-shadow-top {z-index: 0;-webkit-backface-visibility: hidden;backface-visibility: hidden
}