uniapp实现自定义轮播图

1.讲解

效果图:

 

 

绑定数据:

 

 

 

 

白色圆点选中的颜色是白色, 选不中的是空的颜色:

以上就完成了自定义轮播图

2.封装

以下是对轮播图进行封装:

将标题和小圆点部分封装为一个组件:

 

 

 

 

3.完整代码

	<!-- 轮播图的区域 开始-->
		<!-- indicator-dots 小圆点    autoplay自动轮播   interval 轮播的间隔  duration每张轮播图轮播耗时   circular斜街滚动 -->
		<view class="my-swiper">
			<swiper :current="current" :autoplay="true" :interval="3000" :duration="1000" :circular="true"
				indicator-color="#ACACAC" indicator-active-color="#FFFFFF" @change='changeSwiper'>
				<swiper-item v-for="(item, index) in banner" :key="index">
					<!-- 动态绑定图片的 src 属性 -->
					<!-- <navigator :href="item.click_link" v-if="item.click_type == '2'"> -->
					<image :src="item.img_url" mode="" @click="handleNav(item.id)"></image>
					<!-- </navigator> -->
				</swiper-item>
			</swiper>
			<view class="swiper-shadow">
				<view class="swiper-text" @click="handleNav(banner[current].id)">
					{{banner[current].title||''}}
				</view>
				<view class="swiper-dot">
					<view class="item-dot" v-for="(item,index) in banner" :key="index"
						:class="index===current?'colorone':'colortow'">
					</view>
				</view>
			</view>
		</view>
		<!-- 轮播图的区域 结束-->
<script>
	import {
		findArticle,
	} from '../../api/home.js'


	export default {
		data() {
			return {
				cdnUrl: getApp().globalData.cdnUrl,

				current: 0,
				swiperList: [{
						id: 1,
						src: getApp().globalData.cdnUrl + 'static/img-home/topimg.png'
					},
					{
						id: 2,
						src: getApp().globalData.cdnUrl + 'static/img-home/topimg.png'
					},
					{
						id: 3,
						src: getApp().globalData.cdnUrl + 'static/img-home/topimg.png'
					}
				],
				banner: [],
				collect: [],
				important_news: [],
				announcement: [],
				news: [],
				announcementList: [],
				minList: []
			}
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(options) {
			console.log()
		},
		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady() {},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow() {
			this.getArticle();
		},
		methods: {
			handleNav(id) {
				$T.handleNav(id)
			},
			async getArticle() {
				const result = await findArticle()
				const {
					data
				} = result
				this.banner = data.banner
				this.announcement = data.announcement
				this.collect = data.collect
				this.important_news = data.important_news
				this.news = data.news
			},
	
			// 切换轮播图
			changeSwiper(e) {
				// console.log(e);
				this.current = e.detail.current
			}
		}
	}
</script>
// 轮播图
		.my-swiper {
			position: relative;
			width: 100%;
			height: 512rpx;

			// 轮播图片
			swiper {
				height: 100%;

				.swiper-item,
				image {
					width: 100%;
					height: 100%;
				}
			}

			// 轮播阴影
			.swiper-shadow {
				position: absolute;
				bottom: 0px;
				left: 0rpx;
				right: 0;
				display: flex;
				align-content: center;
				height: 80rpx;
				width: 100%;
				background-color: rgba(34, 34, 34, 0.56);

				.swiper-text {
					width: 75%;
					height: 80rpx;
					line-height: 80rpx;
					margin-left: 32rpx;

					color: #FFFFFF;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 1;
					/* 限制在一个块元素显示的文本的行数 */
					-webkit-box-orient: vertical;
					/* 垂直排列 */
					word-break: break-all;
					/* 内容自动换行 */

				}

				// 轮播图小圆点
				.swiper-dot {
					width: 20%;
					height: 80rpx;
					margin-left: 60rpx;

					.item-dot {
						margin-top: 35rpx;
						float: left;
						margin-left: 12rpx;
						width: 16rpx;
						height: 16rpx;
						border-radius: 50%;
					}

					.colorone {
						background: #FFFFFF;
					}

					.colortow {

						background: #ACACAC;
					}
				}
			}
		}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Uniapp实现卡片式轮播图,可以通过使用组件库或自定义组件来实现。以下是一个简单的实现示例: 1. 首先,在你的 Uniapp 项目中引入需要的组件库,如 vant、uView 等。或者你可以自己编写一个自定义组件。 2. 在需要使用轮播图的页面中,引入轮播图组件: ```vue <template> <view> <swiper class="swiper-container" indicator-dots autoplay circular> <block v-for="(item, index) in swiperList" :key="index"> <swiper-item> <!-- 卡片内容 --> <view class="card"> <image :src="item.image" mode="aspectFill" class="card-image" /> <text>{{ item.title }}</text> </view> </swiper-item> </block> </swiper> </view> </template> <script> export default { data() { return { swiperList: [ { image: '图片地址1', title: '标题1', }, { image: '图片地址2', title: '标题2', }, // 依此类推 ], }; }, }; </script> <style> .card { width: 200px; /* 设置卡片宽度 */ height: 300px; /* 设置卡片高度 */ background-color: #f5f5f5; border-radius: 8px; } .card-image { width: 100%; height: 200px; /* 设置卡片中图片的高度 */ border-top-left-radius: 8px; border-top-right-radius: 8px; } </style> ``` 在上述代码中,我们使用了 `swiper` 组件和 `swiper-item` 组件来实现轮播图的效果。每个 `swiper-item` 中放置一个卡片内容,其中使用了 `image` 组件来显示图片,`text` 组件来显示标题。 你可以根据实际需求调整卡片的样式和轮播图的配置,例如设置自动轮播、指示器等。同时,也可以根据项目需要自定义组件样式和交互效果。 希望这个示例能够帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值