问题背景
自定义功能使得开发者能够更自由地调整样式,以便适应多样化的个性化需求。在本文中,我们将探讨微信小程序开发过程中如何实现自定义设置。
问题分析
微信小程序中,自定义的流程如下:
在app.json文件中,需在指定项下设置字段,并且确保其他相关配置得到完善。同时,在各个tab页的json文件中,必须声明该项,或者在app.json中全局启用。以下是一个示例代码:
{ "tabBar": { "custom": true, "color": "#000000", "selectedColor": "#000000", "backgroundColor": "#000000", "list": [{ "路径指向": "组件页面/index" "text": "组件" }, { "pagePath": "page/API/index", "text": "接口" }] }, "usingComponents": {} }
将代码文件进行添加,于代码的根目录中,创建一个新的子目录名为“-tab-bar”,并在该子目录中新增一个入口文件。
custom-tab-bar/index.js custom-tab-bar/index.json custom-tab-bar/index.wxml custom-tab-bar/index.wxss
编写 代码问题解决
不多言赘述,直接展示代码。(1)针对项目中的app.json文件,其内容如下:
{ "pages": [ "pages/index/index", "pages/login/index", "pages/logs/logs", "pages/lll" ], "window": { 背景文字风格设定为浅色,以增强阅读体验。 导航栏背景色设定为纯白色,以提供清晰明亮的视觉效果。 导航栏标题文字设定为“微信”。 导航栏文字颜色设置为黑色。 }, "tabBar": { "custom": true, "color": "#626262", "selectedColor": "#062da9", "borderStyle": "black", "list": [ { "路径指向": "位于pages目录下的index子目录中的index页面" 图标路径位于“static/img”文件夹中,具体文件名为“icon-admin.png”。 所选图标路径位于“static/img”文件夹,具体文件名为“icon-admin.png”。 "text": "首页" }, { "该路径指向"pages/login/index"页面" "iconPath": "static/img/icon-admin.png", "selectedIconPath": "static/img/icon-admin.png", "text": "企业诉求" } ] }, "style": "v2", 该文件位于sitemap.json路径下。 "usingComponents": {} }
在项目的主目录中,我创建了一个名为“-tab-bar”的子文件夹,该文件夹内包含一个.js文件,其代码内容如下。
let app = getApp(); Component({ data: { selected: 0, color: "#626262", selectedColor: "#062da9", list: [{ 该路径指向“/pages/index/index”页面。 图标路径位于静态图片文件夹中,具体地址为/icon-admin.png。 "指定的图标路径位于静态资源文件夹中,具体为:/static/img/icon-admin.png", "text": "首页" }, { 页面路径设置为“/pages/login/index”。 "iconPath": "/static/img/icon-admin.png", "selectedIconPath": "/static/img/icon-admin.png", "text": "分类" },{ "pagePath": "/pages/my/my", "iconPath": "/static/img/icon-admin.png", "selectedIconPath": "/static/img/icon-admin.png", "text": "我的" }] }, ready() { }, attached() { }, methods: { switchTab(e) { const data = 事件当前目标所携带的属性数据 const url = data.path wx.switchTab({url}) this.setData({ selected: data.index }) } } })
.json文件,代码如下:
{ "component": true }
.wxml文件,代码如下:
测试首页 中心简介 公告资讯 {{item.name}} | 查看全部 {{item.name}} {{item.lat}} {{item.lon}} {{item.lat}} {{item.lon}} 暂无任何资讯 常见办事入口 {{item.name}} 建议意见
.wxss文件,代码如下:
.swiper-block{ height: 400rpx; width: 100%; margin-top: 50rpx; z-index: 10; } .swiper-item{ display: flex; flex-direction: column; justify-content: center; align-items: flex-start; overflow:unset; } .slide-image{ height:380rpx; width: 90%; border-radius: 9rpx; 此样式定义了盒子的阴影效果,其水平方向和垂直方向上的偏移均为0像素,阴影的模糊半径为30rpx,颜色为半透明的黑色,透明度为0.2。 margin: 0rpx 30rpx; z-index: 1; } .active{ transform: scale(1.14); 过渡效果:全部属性在0.2秒内以平滑方式渐变,延迟时间为0秒。 z-index: 20; } .navclass{ width: 40rpx; height: 12rpx; display: flex; justify-content: center; align-items: center; margin-top: 30rpx; } /* 导航 */ .navigation-container { position: fixed; width: 100%; z-index: 99; top: 0; left: 0; background-color: #ffffff; } .navigation-bar { position: relative; display: flex; flex-direction: row; align-items: center; } .navigation-title { position: absolute; left: 104px; right: 104px; text-align: center; font-size: 16px; font-weight: bold; color: #000000; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .index-bg{ width: 100%; height: 450rpx; z-index: 0; position: absolute; top: 0; left: 0; } .pageWrapper{ height: 100%; width: 100%; /* display: flex; flex-direction: column; align-items: center; */ overflow-x: hidden; } .swiper-nav{ width: 100%; height: 12rpx; display: flex; justify-content: center; align-items: center; z-index: 10; } .firstTitle{ width: 95%; position: relative; margin: 30rpx 0; padding-left: 60rpx; font-weight: bold; } .firstTitle::before{ position:absolute; content: " "; height: 100%; width: 10rpx; background-color: #062da9; left: 5%; } .cont-title { width: 100%; height: 90rpx; padding: 0 40rpx; 底部边框设为2rpx的实线,颜色为#f0f0f0。 color: #0d91ff; font-size: 28rpx; line-height: 80rpx; box-sizing: border-box; } .cont-title text { line-height: 80rpx; } .flexbetween { display: flex; align-items: center; 内容对齐方式:两端对齐; } .menutabbox1 { padding: 0 30rpx; box-sizing: border-box; display: flex; width: 100%; color: #666666; } .menuitem { width: auto; height: 100%; background: #fff; } .label-title { display: inline-block; padding: 0 20rpx; white-space: nowrap; font-size: 28rpx; } .itemactive { color: #062da9; font-weight: 600; } .infobox:first-child { border-top: none; } .infobox { width: calc(100% - 40rpx); height: 100%; margin-left: 40rpx; padding: 20rpx 40rpx 20rpx 0; border-bottom: solid 2rpx #f0f0f0; box-sizing: border-box; position: relative; } .info-title { 该字体设置为源汉SansCN中文字体,中文字体粗体版。 font-size: 30rpx; font-weight: bold; font-stretch: normal; line-height: 45rpx; letter-spacing: 0rpx; color: #000000; overflow: hidden; white-space:pre; /*保持空白符和换行符不变*/ text-overflow: ellipsis; } .itemflex { display: flex; align-items: center; margin-top: 28rpx; } .info-text { color: #aebdd8; font-size: 20rpx; margin-left: 5rpx; } .icon1 { width: 17rpx; height: 21rpx; } .icon2 { width: 24rpx; height: 15rpx; margin-left: 20rpx; } .info-img { width: 236rpx; height: 133rpx; } .menutabbox1 { padding: 0 30rpx; box-sizing: border-box; display: flex; width: 100%; color: #666666; } .legalinfoitem{ display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; } .zxfw{ margin-left: 30rpx; width: 690rpx; height: 350rpx; display: flex; align-items: center; 内容对齐方式:均匀分布; } .zxfw1{ width: 40%; height: 280rpx; } .contactus{ height: 150rpx; width: 145rpx; position: fixed; right: 20rpx; }
运行结果如下:
问题总结
本文首先对微信小程序的开发过程进行了简要介绍,其中涉及如何进行自定义设置;对于对此感兴趣的同学,建议进一步进行深入学习和探究。