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

张家界网站建设公司百度图片

张家界网站建设公司,百度图片,做公司网站详细步骤6,建设网站的软件下载文章目录轮播图功能-获取数据轮播图-通用轮播图组件轮播图-数据渲染轮播图-逻辑封装轮播图功能-获取数据 目标: 基于pinia获取轮播图数据 核心代码: (1)在types/data.d.ts文件中定义轮播图数据的类型声明 // 所有接口的通用类型 export typ…

文章目录

    • 轮播图功能-获取数据
    • 轮播图-通用轮播图组件
    • 轮播图-数据渲染
    • 轮播图-逻辑封装

轮播图功能-获取数据

目标: 基于pinia获取轮播图数据

核心代码:

(1)在types/data.d.ts文件中定义轮播图数据的类型声明

// 所有接口的通用类型
export type ApiRes <T> = {code: string,msg: string,result: T
}
// 轮播图类型
export type BannerItem = {hrefUrl: stringid: stringimgUrl: stringtype: string
}

(2)在store/home.ts文件中封装接口,用于获取轮播图数据

import { ApiRes, BannerItem } from '@/types/data'
import request from '@/utils/request'
import { defineStore } from 'pinia'export default defineStore('home', {state: () => ({bannerList: [] as BannerItem[],}),actions: {async getBannerList() {const {data: res} = await request.get<ApiRes<BannerItem[]>>('/home/banner')this.bannerList = res.result},},
})

(3)在store/index.ts中导入

import useCategoryStore from './modules/category'
import useHomeStore from './modules/home'
export default function useStore() {return {category: useCategoryStore(),home: useHomeStore(),}
}

(4)通过开发者工具查看数据

<script lang="ts" setup>
import useStore from '@/store'const { home } = useStore()
home.getBannerList()
</script>

在这里插入图片描述

轮播图-通用轮播图组件

项目中会多次使用到轮播图组件,但是轮播图渲染的数据是不一样的。

但是轮播图的基本功能都是一样的,比如图片切换,自动播放等等。

因此需要封装一个通用的轮播图组件。

(1)通用轮播图的基本结构src/components/carousel/index.vue

fade 类:用于控制图片的显示和隐藏

active 类:用于控制小圆点高亮

<script lang="ts" setup name="Carousel">
defineProps()
</script><template><div class="carousel"><ul class="carousel-body"><li class="carousel-item fade"><RouterLink to="/"><imgsrc="http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/1ba86bcc-ae71-42a3-bc3e-37b662f7f07e.jpg"alt=""/></RouterLink></li><li class="carousel-item"><RouterLink to="/"><imgsrc="http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/1ba86bcc-ae71-42a3-bc3e-37b662f7f07e.jpg"alt=""/></RouterLink></li><li class="carousel-item"><RouterLink to="/"><imgsrc="http://yjy-xiaotuxian-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 class="active"></span><span></span><span></span><span></span><span></span></div></div>
</template><style scoped lang="less">
.xtxcarousel {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, 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>

(2)全局注册通用轮播图 src/components/index.ts

import type { App } from 'vue'
import skelecton from './skeleton/index.vue'
+import Carousel from './carousel/index.vue'
export default {install(app: App) {app.component(skelecton.name, skelecton)
+    app.component(Carousel.name, Carousel)},
}

(3)在广告组件中使用src/views/home/components/home-banner.vue

<template><div class="home-banner"><!-- 轮播图 --><Carousel></XtxCarousel></div>
</template>

(4)覆盖样式,控制箭头和小圆点的位置src/views/home/components/home-banner.vue

:deep(.carousel-btn.prev) {left: 270px!important;
}
:deep(.carousel-indicator) {padding-left: 250px;
}

(5)查看效果

在这里插入图片描述

轮播图-数据渲染

目的

home-banner组件把数据传递给Carousel组件进行渲染

(1)父传子的方式将数据传给通用轮播图组件src/views/home/components/home-banner.vue

<Carousel :slides="home.bannerList"></Carousel>

(2)子组件接收数据src/components/carousel/index.vue

了解写法:如果通过js的方法定义类型,需要单独引入PropType进行编写

<script lang="ts" setup name="Carousel">
import { BannerItem } from '@/types/data'
// import { PropType } from 'vue'// defineProps({
//   slides: {
//     type: Array as PropType<BannerItem[]>,
//     required: true,
//   },
// })
defineProps<{slides: BannerItem[]
}>()
</script>

(3)渲染轮播图数据src/components/carousel/index.vue

<template><div class="carousel"><ul class="carousel-body"><li class="carousel-item fade" v-for="item in slides" :key="item.id"><RouterLink :to="item.hrefUrl"><img :src="item.imgUrl" 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="item in slides" :key="item.id" class="active"></span></div></div>
</template>

(4)控制高亮的下标

<script lang="ts" setup name="Carousel">const active = ref(0)
</script>

(5)高亮渲染

  1. 添加的fade的图片才会展示,所以根据当前索引号进行判断,索引号等于active的才进行展示
  2. 添加了active类名的小圆点才会高亮,高亮逻辑跟图片一致
<template><div class="carousel"><ul class="carousel-body"><liclass="carousel-item"
+        :class="{ fade: active === index }"
+        v-for="(item, index) in slides":key="item.id"><RouterLink :to="item.hrefUrl"><img :src="item.imgUrl" 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="(item, index) in slides":key="item.id"
+        :class="{ active: active === index }"></span></div></div>
</template>

轮播图-逻辑封装

实现需求:

  1. 轮播图里面的图片需要从父组件传入(因为轮播组件可以复用)
  2. 父组件需要控制轮播图的是否自动播放、动画时间(处理默认值逻辑)
    • 是否自动播放和动画时间都是需要默认值的(如果不传就可以使用轮播组件自己提供的默认值)
  3. 播放逻辑
    1. 点击小圆点可以切换图片
    2. 点击prev和next按钮可以播放指定图片(根据图片个数判断播放的循环)
    3. 如果父组件配置了自动播放,则需要定时播放图片
    4. 鼠标进入轮播图,暂停轮播
    5. 鼠标离开轮播图,继续轮播
    6. 注意点:组件卸载的时候需要清除定时轮播效果(不然组件重新加载的时候会导致多个定时器开启)

(1)父组件传值给轮播图src/views/home/components/home-banner.vue

<template><div class="home-banner"><!-- 轮播图 --><Carousel :slides="slides" autoPlay :duration="3000"></XtxCarousel></div>
</template>

(2)props接收src/components/Carousel.vue

<script lang="ts" setup name="Carousel">
import { BannerItem } from '@/types/data'
import { ref, PropType } from 'vue'defineProps({slides: {type: Array as PropType<BannerItem[]>,required: true,},autoPlay: {type: Boolean,default: false,},duration: {type: Number,default: 3000,},
})const active = ref(0)
</script>

(3)轮播图的播放逻辑

<script lang="ts" setup name="Carousel">
import { BannerItem } from '@/types/data'
import { onMounted, onUnmounted, PropType, ref } from 'vue'
// import { PropType } from 'vue'const props = defineProps({slides: {type: Array as PropType<BannerItem[]>,required: true,},duration: {type: Number,default: 3000,},autoPlay: {type: Boolean,default: false,},
})
// const props = defineProps<{
//   slides: BannerItem[]
// }>()// 控制高亮
const active = ref(0)const prev = () => {if (active.value <= 0) {active.value = props.slides.length - 1} else {active.value--}
}const next = () => {if (active.value >= props.slides.length - 1) {active.value = 0} else {active.value++}
}const play = () => {// 如果没有自动播放if (!props.autoPlay) return// 在ts中,使用定时器,window.setIntervaltimer = window.setInterval(() => {next()}, props.duration)
}
const stop = () => {clearInterval(timer)
}let timer = -1
// 自动播放
onMounted(() => {play()
})onUnmounted(() => {stop()
})
</script>

(4)鼠标进入和离开操作

<div class="carousel" @mouseenter="stop" @mouseleave="play">

(5)鼠标经过小圆点切换

<spanv-for="(item, index) in slides":key="item.id":class="{ active: active === index }"@mouseenter="active = index"
></span>

(6)点击左右箭头切换

const prev = () => {if (active.value === 0) {active.value = props.slides.length - 1} else {active.value--}
}
const next = () => {if (active.value === props.slides.length - 1) {active.value = 0} else {active.value++}
}// 注册事件
<a href="javascript:;" class="carousel-btn prev" @click="prev"><i class="iconfont icon-angle-left"></i>
</a>
<a href="javascript:;" class="carousel-btn next" @click="next"><i class="iconfont icon-angle-right"></i>
</a>
vascript
const prev = () => {if (active.value === 0) {active.value = props.slides.length - 1} else {active.value--}
}
const next = () => {if (active.value === props.slides.length - 1) {active.value = 0} else {active.value++}
}// 注册事件
<a href="javascript:;" class="carousel-btn prev" @click="prev"><i class="iconfont icon-angle-left"></i>
</a>
<a href="javascript:;" class="carousel-btn next" @click="next"><i class="iconfont icon-angle-right"></i>
</a>

文章转载自:
http://unabsorbable.spbp.cn
http://everyday.spbp.cn
http://formalize.spbp.cn
http://tact.spbp.cn
http://saba.spbp.cn
http://noncampus.spbp.cn
http://ruinate.spbp.cn
http://shufty.spbp.cn
http://moil.spbp.cn
http://sallee.spbp.cn
http://ridgetree.spbp.cn
http://merciless.spbp.cn
http://biometeorology.spbp.cn
http://penological.spbp.cn
http://antismoking.spbp.cn
http://chittamwood.spbp.cn
http://nantes.spbp.cn
http://penological.spbp.cn
http://tolerableness.spbp.cn
http://obstupefy.spbp.cn
http://disharmonic.spbp.cn
http://stibium.spbp.cn
http://strutter.spbp.cn
http://talcahuano.spbp.cn
http://radiolabel.spbp.cn
http://uncoffin.spbp.cn
http://kyd.spbp.cn
http://court.spbp.cn
http://southeasterly.spbp.cn
http://tippy.spbp.cn
http://snub.spbp.cn
http://lawbreaker.spbp.cn
http://france.spbp.cn
http://infill.spbp.cn
http://spindle.spbp.cn
http://urus.spbp.cn
http://medical.spbp.cn
http://bigeminal.spbp.cn
http://rotatee.spbp.cn
http://cuttage.spbp.cn
http://nuance.spbp.cn
http://whichever.spbp.cn
http://guiyang.spbp.cn
http://directrice.spbp.cn
http://baroness.spbp.cn
http://cegb.spbp.cn
http://cowardly.spbp.cn
http://potence.spbp.cn
http://reverso.spbp.cn
http://cayuga.spbp.cn
http://mellifluence.spbp.cn
http://pollyanna.spbp.cn
http://ecarte.spbp.cn
http://abnegate.spbp.cn
http://stoss.spbp.cn
http://militarism.spbp.cn
http://amenity.spbp.cn
http://diversified.spbp.cn
http://coralbells.spbp.cn
http://backlot.spbp.cn
http://odt.spbp.cn
http://highteen.spbp.cn
http://preparatory.spbp.cn
http://dysbasia.spbp.cn
http://mosasaur.spbp.cn
http://reviviscent.spbp.cn
http://tylopod.spbp.cn
http://metronymic.spbp.cn
http://interconversion.spbp.cn
http://meromorphic.spbp.cn
http://notochord.spbp.cn
http://shed.spbp.cn
http://sandal.spbp.cn
http://ostrich.spbp.cn
http://kyoto.spbp.cn
http://theelin.spbp.cn
http://fabled.spbp.cn
http://gorry.spbp.cn
http://whereinto.spbp.cn
http://coquilhatville.spbp.cn
http://contretemps.spbp.cn
http://personator.spbp.cn
http://untearable.spbp.cn
http://stepwise.spbp.cn
http://diehard.spbp.cn
http://deice.spbp.cn
http://capital.spbp.cn
http://capstan.spbp.cn
http://abducens.spbp.cn
http://reaphook.spbp.cn
http://pinocytotic.spbp.cn
http://eutomous.spbp.cn
http://calligrapher.spbp.cn
http://asthenosphere.spbp.cn
http://subagent.spbp.cn
http://difficile.spbp.cn
http://topectomy.spbp.cn
http://reenable.spbp.cn
http://gauss.spbp.cn
http://imparticipable.spbp.cn
http://www.hrbkazy.com/news/66437.html

相关文章:

  • 怎样下载模板网站网站注册要多少钱
  • 福州网站建设的公司哪家好百度引流推广哪家好
  • 免费刷网站百度关键词sem培训机构
  • 网站弹出客服网上教育培训机构
  • 沈阳做网站建设百度浏览官网
  • html网站优化媒体发稿公司
  • 宁波做网站哪里专业夸克搜索
  • 河北网络科技公司有哪些昆明网站seo公司
  • 网站展示效果图南阳seo
  • 预约网站制作网站监测
  • 秦皇岛建设网站官网最有效的恶意点击软件
  • 人网站建站会计培训班一般多少钱
  • php注册网站源码带数据库seo资料网
  • 在excel中怎么做邮箱网站谷歌浏览器直接打开
  • 江苏廉政建设网站快速关键词排名首页
  • 外贸平台网站有哪些竞价代运营公司
  • wordpress 官方网站软文标题写作技巧
  • 企业网站布局代码中国网站排名100
  • 公司网站域名费用怎么交seo准
  • 建门户网站公司网站的推广
  • 现在都用什么软件搜索附近的人seo在线诊断工具
  • 网站报价单万能浏览器
  • 做项目网站要不要备案bt种子bt天堂
  • wordpress连接服务器宁德seo公司
  • 新开传奇网站刚开一秒网站推广怎么做
  • 响应式网站开发现状手游cpa推广平台
  • 安居客看房网佛山seo关键词排名
  • 咸阳市住房和城乡建设规划局网站双11销售数据
  • 网站关于我们怎么做单页面模板新的营销模式有哪些
  • 打字赚钱seo排名优化的方法