前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >uni-app小程序开发-组件

uni-app小程序开发-组件

作者头像
码客说
发布2024-07-22 10:28:13
30
发布2024-07-22 10:28:13
举报
文章被收录于专栏:码客

相关文档

简介

https://uniapp.dcloud.net.cn/

组件

https://uniapp.dcloud.net.cn/component/

API

https://uniapp.dcloud.net.cn/api/

演示

https://hellouniapp.dcloud.net.cn/pages/component/view/view

状态栏

官方默认的高度--status-bar-height的值是固定的25px

这就导致不同型号的手机的状态栏的高度是不一样的,导致实际效果并不好。

导航栏在不同的型号上是不受影响的都是44px

代码语言:javascript
复制
.status_bar {
    height: var(--status-bar-height);
    width: 100%;
}

JS中

代码语言:javascript
复制
<script>
	export default {
		data() {
			return {
				windowInfo: {},
			}
		},
		beforeMount() {
			this.windowInfo = uni.getWindowInfo()
		},
		onLoad() {},
		methods: {}
	}
</script>

状态栏设置

代码语言:javascript
复制
<!-- 这里是状态栏 -->
<view class="status_bar" :style="{height:windowInfo.statusBarHeight+'px'}"></view>

导航条

pages.json

代码语言:javascript
复制
"globalStyle": {
    "navigationBarTextStyle": "black",
    //#ifdef MP-ALIPAY
    "navigationBarBackgroundColor": "#ffffff",
    //#endif
    "navigationBarTitleText": "",
    "backgroundColor": "#f8f8f8"
},

导航条颜色一般是通过navigationBarTextStyle配置

导航栏标题颜色及状态栏前景颜色,仅支持 black/white

但是支付宝小程序不支持

支付宝是通过navigationBarBackgroundColor影响,只有值是#ffffff的时候文字就是黑的,其它值文字都是白的。

TabBar

pages.json 中 配置

代码语言:javascript
复制
{
	"tabBar": {
		"color": "#555555",
		"selectedColor": "#FA9C31",
		"borderStyle": "black",
		"backgroundColor": "#ffffff",
		"height": "50px",
		"fontSize": "12px",
		"iconWidth": "24px",
		"spacing": "6px",
		"list": [{
			"pagePath": "pages/index/index",
			"iconPath": "/static/imgs/tab/tab_home1.png",
			"selectedIconPath": "/static/imgs/tab/tab_home2.png",
			"text": "首页"
		}, {
			"pagePath": "pages/category/index",
			"iconPath": "/static/imgs/tab/tab_category1.png",
			"selectedIconPath": "/static/imgs/tab/tab_category2.png",
			"text": "分类"
		}, {
			"pagePath": "pages/usercenter/index",
			"iconPath": "/static/imgs/tab/tab_user1.png",
			"selectedIconPath": "/static/imgs/tab/tab_user2.png",
			"text": "我的"
		}]
	}
}

如果要用图标字体

代码语言:javascript
复制
{
	"tabBar": {
		"color": "#7A7E83",
		"selectedColor": "#3cc51f",
		"borderStyle": "black",
		"backgroundColor": "#ffffff",
		"height": "50px",
		"fontSize": "10px",
		"iconWidth": "24px",
		"spacing": "3px",
		"iconfontSrc": "/static/iconfont.ttf", // app tabbar 字体.ttf文件路径 app 3.4.4+
		"list": [{
			"pagePath": "pages/index/index",
			"iconPath": "/static/imgs/tab/tab_home1.png",
			"selectedIconPath": "/static/imgs/tab/tab_home2.png",
			"text": "首页",
			"iconfont": { // 优先级高于 iconPath,该属性依赖 tabbar 根节点的 iconfontSrc
				"text": "\ue102",
				"selectedText": "\ue103",
				"fontSize": "17px",
				"color": "#000000",
				"selectedColor": "#0000ff"
			}
		}]
	}
}

展示类组件

Image

代码语言:javascript
复制
<image src="/static/imgs/home/search.png" mode="scaleToFill"></image>

如果我们想只设置宽度,让高度自适应

代码语言:javascript
复制
<image src="/static/imgs/01.jpg" mode="widthFix"></image>

mode 有效值

默认为scaleToFill

mode 有 14 种模式,其中 5 种是缩放模式,9 种是裁剪模式。

模式

说明

缩放

scaleToFill

不保持纵横比缩放图片,使图片的宽高完全拉伸至填满 image 元素

缩放

aspectFit

保持纵横比缩放图片,使图片的长边能完全显示出来。也就是说,可以完整地将图片显示出来。

缩放

aspectFill

保持纵横比缩放图片,只保证图片的短边能完全显示出来。也就是说,图片通常只在水平或垂直方向是完整的,另一个方向将会发生截取。

缩放

widthFix

宽度不变,高度自动变化,保持原图宽高比不变

缩放

heightFix

高度不变,宽度自动变化,保持原图宽高比不变App 和 H5 平台 HBuilderX 2.9.3+ 支持、微信小程序需要基础库 2.10.3

裁剪

top

不缩放图片,只显示图片的顶部区域

裁剪

bottom

不缩放图片,只显示图片的底部区域

裁剪

center

不缩放图片,只显示图片的中间区域

裁剪

left

不缩放图片,只显示图片的左边区域

裁剪

right

不缩放图片,只显示图片的右边区域

裁剪

top left

不缩放图片,只显示图片的左上边区域

裁剪

top right

不缩放图片,只显示图片的右上边区域

裁剪

bottom left

不缩放图片,只显示图片的左下边区域

裁剪

bottom right

不缩放图片,只显示图片的右下边区域

文本展示

代码语言:javascript
复制
<text>{{text}}</text>

轮播图

代码语言:javascript
复制
<swiper class="swiper" circular :indicator-dots="true" :autoplay="true" interval="5000" duration="500">
    <swiper-item>
        <view class="swiper-item uni-bg-red"></view>
    </swiper-item>
    <swiper-item>
        <view class="swiper-item uni-bg-green"></view>
    </swiper-item>
    <swiper-item>
        <view class="swiper-item uni-bg-blue"></view>
    </swiper-item>
</swiper>

官方文档

https://uniapp.dcloud.net.cn/component/swiper.html

其中

  • interval 是多长时间切换下一张 单位毫秒
  • duration 一张切换的时长 单位毫秒

样式

代码语言:javascript
复制
.swiper {
    height: 386rpx;
    background-color: white;

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

    .uni-bg-red {
        background-color: red;
    }

    .uni-bg-green {
        background-color: green;
    }

    .uni-bg-blue {
        background-color: blue;
    }
}

输入类组件

输入框

代码语言:javascript
复制
<input class="uni-input" focus placeholder="自动获得焦点" />

自定义组件

传统vue组件,需要安装、引用、注册,三个步骤后才能使用组件。

easycom将其精简为一步。

只要组件安装在项目的components目录下或uni_modules目录下,并符合components/组件名称/组件名称.(vue|uvue)目录结构,就可以不用引用、注册,直接在页面中使用。

状态栏占位组件

状态栏我们可能每个页面都用,为了方便,我们可以定义为组件。

文件目录/components/z-statusbar/z-statusbar.vue

页面

代码语言:javascript
复制
<template>
	<view class="status_bar" :style="{ height: windowInfo.statusBarHeight + 'px' }"></view>
</template>

<script>
export default {
	name: 'z-statusbar',
	data() {
		return {
			windowInfo: {}
		};
	},
	beforeMount() {
		this.windowInfo = uni.getWindowInfo();
	}
};
</script>

<style>
.status_bar {
	height: var(--status-bar-height);
	width: 100%;
    flex: none;
}
</style>

这样我们就可以直接使用了

代码语言:javascript
复制
<z-statusbar></z-statusbar>

导航条占位组件

文件目录/components/z-navigationbar/z-navigationbar.vue

页面

代码语言:javascript
复制
<template>
	<!-- 这里是导航 -->
	<view class="navigation_bar"></view>
</template>

<script>
export default {
	name: 'z-navigationbar',
	data() {
		return {};
	}
};
</script>

<style>
.navigation_bar {
	height: 44px;
	width: 100%;
    flex: none;
}
</style>

这样我们就可以直接使用了

代码语言:javascript
复制
<z-navigationbar></z-navigationbar>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-05-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 相关文档
  • 状态栏
  • 导航条
  • TabBar
  • 展示类组件
    • Image
      • 文本展示
        • 轮播图
        • 输入类组件
          • 输入框
          • 自定义组件
            • 状态栏占位组件
              • 导航条占位组件
              相关产品与服务
              云开发 CloudBase
              云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档