【uniapp】轮播图

前言

Uniapp的swiper组件是一个滑块视图容器组件,可以在其中放置多个轮播图或滑动卡片。它是基于微信小程序的swiper组件进行封装,可以在不同的平台上使用,如微信小程序、H5、App等。

效果图

前端代码

swiper组件

html 复制代码
<template>
  <view class="banner-content">
    <swiper class="banner" :indicator-dots="true" :autoplay="true" :interval="2000" :duration="300" :circular="true">
      <block v-for="(item, index) in bannerList" :key="index">
        <swiper-item>
			<a :href="item.jump_path" target="_blank">
				<image :src="item.image" mode="aspectFill" class="banner-image"></image>
			</a>
        </swiper-item>
      </block>
    </swiper>
  </view>
</template>
<script>
import axios from 'axios';
export default {
	data() {
		return {
			bannerList: [],
		};
	},
	onLoad() {
		this.getBanner()
	},
	methods: {
		// 轮播图数据
		getBanner(){
			axios.get('http://demo2.com/api/index/banner')// 替换成自己的接口
			.then(response => {
				this.bannerList = response.data.data;
			})
		},
	}
};
</script>
<style>
.banner-content {
  width: 100%;
  height: 300px;
}
.banner {
  width: 100%;
  height: 100%;
  margin: auto;
}
.banner-image {
  width: 100%;
  height: 100%;
}
</style>

数据库

sql 复制代码
CREATE TABLE `banner` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '名称',
  `jump_path` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '跳转路径',
  `image` varchar(200) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '图片',
  `weigh` int(10) DEFAULT '0' COMMENT '权重',
  `status` tinyint(1) DEFAULT '1' COMMENT '是否显示:1=是,0=否',
  `create_time` bigint(16) DEFAULT '0' COMMENT '创建时间',
  `update_time` bigint(16) DEFAULT '0' COMMENT '修改时间',
  PRIMARY KEY (`id`)
)

接口(PHP)

php 复制代码
public function banner(): void
{
    $data = Db::name('banner')->where(['status'=>1])
        ->order(['weigh'=>'desc'])
        ->field(['id','name','jump_path','image'])
        ->select()
        ->toArray();
    foreach ($data as $k => $v){
        $data[$k]['image'] = getFilePath($v['image']);
    }
    $this->success('成功', $data);
}

getFilePath获取文件完整路径

相关推荐
小盼江36 分钟前
水果生鲜农产品推荐系统 协同过滤余弦函数推荐水果生鲜农产品 Springboot Vue Element-UI前后端分离 代码+开发文档+视频教程
vue.js·spring boot·ui
祈澈菇凉1 小时前
如何结合使用thread-loader和cache-loader以获得最佳效果?
前端
垣宇1 小时前
Vite 和 Webpack 的区别和选择
前端·webpack·node.js
java1234_小锋1 小时前
一周学会Flask3 Python Web开发-客户端状态信息Cookie以及加密
前端·python·flask·flask3
化作繁星1 小时前
如何在 React 中测试高阶组件?
前端·javascript·react.js
初遇你时动了情1 小时前
react module.scss 避免全局冲突类似vue中scoped
vue.js·react.js·scss
Au_ust1 小时前
千峰React:函数组件使用(2)
前端·javascript·react.js
爱吃南瓜的北瓜2 小时前
npm install 卡在“sill idealTree buildDeps“
前端·npm·node.js
TTc_2 小时前
记录首次安装远古时代所需的运行环境成功npm install --save-dev node-sass
前端·npm·sass
翻滚吧键盘2 小时前
npm使用了代理,但是代理软件已经关闭导致创建失败
前端·npm·node.js