因为我的项目的非凡状况,所以须要手动封装一个轮播图,大抵步骤:
- 筹备一个Hz-carousel组件根底布局并全局注册
- 筹备home-banner组件,应用Hz-carousel组件,再首页注册应用。
- 深度作用选择器笼罩Hz-carousel组件的默认款式
上面是具体步骤:
第一步:
当初SRC下的components文件夹中创立一个专门为我的项目封装组件的文件夹,以便好治理.
例如:library/Hz-carousel.vue
轮播图根底构造代码
<template> <div class='Hz-carousel'> <ul class="carousel-body"> <li class="carousel-item fade"> <RouterLink to="/"> <img src="http://yjy-xxxxxx-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/1ba86bcc-ae71-42a3-bc3e-37b662f7f07e.jpg" alt=""> </RouterLink> </li> </ul> <a href="javascript:;" class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a> <a href="javascript:;" class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a> <div class="carousel-indicator"> <span v-for="i in 5" :key="i"></span> </div> </div></template><script>export default { name: 'HzCarousel'}</script><style scoped lang="less">.Hz-carousel{ width: 100%; height: 100%; min-width: 300px; min-height: 150px; position: relative; .carousel{ &-body { width: 100%; height: 100%; } &-item { width: 100%; height: 100%; position: absolute; left: 0; top: 0; opacity: 0; transition: opacity 0.5s linear; &.fade { opacity: 1; z-index: 1; } img { width: 100%; height: 100%; } } &-indicator { position: absolute; left: 0; bottom: 20px; z-index: 2; width: 100%; text-align: center; span { display: inline-block; width: 12px; height: 12px; background: rgba(0,0,0,0.2); border-radius: 50%; cursor: pointer; ~ span { margin-left: 12px; } &.active { background: #fff; } } } &-btn { width: 44px; height: 44px; background: rgba(0,0,0,.2); color: #fff; border-radius: 50%; position: absolute; top: 228px; z-index: 2; text-align: center; line-height: 44px; opacity: 0; transition: all 0.5s; &.prev{ left: 20px; } &.next{ right: 20px; } } } &:hover { .carousel-btn { opacity: 1; } }}</style>
**全局注册轮播图src/components/library/index.js
**
首页广告组件根底构造 src/views/home/components/home-banner.vue
首页应用广告组件
<template> <div class="home-banner"> <!-- l=轮播图组件 --> <!-- autoplay管制是否自动播放 --> <!-- duration管制轮播的工夫的距离 -->+ <XtxCarousel :sliders='sliders' :autoplay='true' :duration='2'/> </div></template><script>+import { ref } from 'vue'+import { findBanner } from '@/api/home'export default { name: 'HomeBanner',+ setup() { // 轮播图数据+ const sliders = ref([]) // 调用接口获取轮播图数据+ findBanner().then(ret => {+ sliders.value = ret.result+ })+ return { sliders } }}</script><style scoped lang="less">.home-banner { width: 1240px; height: 500px; position: absolute; left: 0; top: 0; z-index: 98; .xtx-carousel {+ :deep(.carousel-btn.prev) {+ left: 270px;+ }+ :deep(.carousel-indicator) { + padding-left: 250px; } }}</style>
笼罩轮播图组件款式 src/views/home/components/home-banner.vue