微信小程序——组件(二)
在上篇文章组件(一)里已经讲解了如何创建一个项目,现在继续。。。讲解一个页面布局以及各个组件的使用。在学习过程中,发现小程序支持flex布局,这对于学习过react native的人来说太好了,布局方面不用担心了。下面来看tab的创建
在使用微信小程序编译代码的工具时,快捷键的使用必不可少,所有快捷键在这里有讲解:小程序编译器快捷键
1.根据我上篇文章介绍,有讲如何创建一个小程序项目,效果如图所示:
2.之前讲解过app.json里是定义全局的一些东西,包括整体颜色风格,路由等等,详细配置讲解见官网。下面是app.json里的代码,调整了背景颜色以及创建了两个tab模块。
- {
- "pages": [
- "pages/component/index/index",
- "pages/component/logs/logs"
- ],
- "window": {
- "navigationBarBackgroundColor": "#F8F8F8",
- "navigationBarTitleText": "wxTestOne",
- "navigationBarTextStyle": "black",
- "backgroundColor": "#F8F8F8"
- },
- "tabBar": {
- "color": "#7A7E83",
- "selectedColor": "#3cc51f",
- "borderStyle": "black",
- "backgroundColor": "#ffffff",
- "list": [
- {
- "pagePath": "pages/component/index/index",
- "iconPath": "images/icon_component.png",
- "selectedIconPath": "images/icon_component_HL.png",
- "text": "组件"
- },
- {
- "pagePath": "pages/component/logs/logs",
- "iconPath": "images/icon_API.png",
- "selectedIconPath": "images/icon_API_HL.png",
- "text": "组件"
- }
- ]
- }
- }
注意看红色圈出的部分是新添加的模块,这里只要在app.json添加正确页面路由的代码,左侧边栏项目文件里就会多出相对应的页面文件(.js .json .wxml .wxss),当然也可以鼠标右键来添加想要的文件。效果如图:
3.“组件”这个tab模块对应的是“index”,“接口”tab对应的模块是logs(上图右边tab名字应该是“接口”)。接下来在 组件 页面显示列表,代码如下:
index.wxml代码:
- <!--pages/component/index/index.wxml-->
- <view class="index">
- <view class="index-hd">
- <image class="index-logo" src="../../resources/kind/logo.png"></image>
- <view class="index-desc">以下将展示小程序官方组件能力,组件样式仅供参考,开发者可根据自身需求自定义组件样式,具体属性参数详见小程序开发文档。</view>
- </view>
- <view class="index-bd">
- <view class="kind-list">
- <block wx:for-items="{{list}}" wx:key="{{item.id}}">
- <view class="kind-list-item">
- <view id="{{item.id}}" class="kind-list-item-hd {{item.open ? 'kind-list-item-hd-show' : ''}}" bindtap="kindToggle">
- <view class="kind-list-text">{{item.name}}</view>
- <image class="kind-list-img" src="../../resources/kind/{{item.id}}.png"></image>
- </view>
- <view class="kind-list-item-bd {{item.open ? 'kind-list-item-bd-show' : ''}}">
- <view class="navigator-box {{item.open ? 'navigator-box-show' : ''}}">
- <block wx:for-items="{{item.pages}}" wx:for-item="page" wx:key="*item">
- <navigator url="pages/{{page}}/{{page}}" class="navigator">
- <view class="navigator-text">{{page}}</view>
- <view class="navigator-arrow"></view>
- </navigator>
- </block>
- </view>
- </view>
- </view>
- </block>
- </view>
- </view>
- </view>
index.js代码:
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- list: [
- {
- id: 'view',
- name: '视图容器',
- open: false,
- pages: ['view', 'scroll-view', 'swiper']
- }, {
- id: 'content',
- name: '基础内容',
- open: false,
- pages: ['text', 'icon', 'progress']
- }, {
- id: 'form',
- name: '表单组件',
- open: false,
- pages: ['button', 'checkbox', 'form', 'input', 'label', 'picker', 'radio', 'slider', 'switch', 'textarea']
- }, {
- id: 'nav',
- name: '导航',
- open: false,
- pages: ['navigator']
- }, {
- id: 'media',
- name: '媒体组件',
- open: false,
- pages: ['image', 'audio', 'video']
- }, {
- id: 'map',
- name: '地图',
- pages: ['map']
- }, {
- id: 'canvas',
- name: '画布',
- pages: ['canvas']
- }
- ]
- },
- kindToggle: function (e) {
- var id = e.currentTarget.id, list = this.data.list;
- for (var i = 0, len = list.length; i < len; ++i) {
- if (list[i].id == id) {
- list[i].open = !list[i].open
- } else {
- list[i].open = false
- }
- }
- this.setData({
- list: list
- });
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- }
- })
index.wxss代码:
- .index-hd {
- padding: 80rpx;
- text-align: center;
- }
- .index-bd {
- padding: 0 30rpx 40rpx;
- }
- .index-ft {
- padding-bottom: 20rpx;
- text-align: center;
- }
- .index-logo {
- width: 86rpx;
- height: 86rpx;
- }
- .index-desc {
- margin-top: 20rpx;
- color: #888;
- font-size: 28rpx;
- }
- .navigator-box {
- opacity: 0;
- position: relative;
- background-color: #fff;
- line-height: 1.41176471;
- font-size: 34rpx;
- transform: translateY(-50%);
- transition: 0.3s;
- }
- .navigator-box-show {
- opacity: 1;
- transform: translateY(0);
- }
- .navigator {
- padding: 20rpx 30rpx;
- position: relative;
- display: flex;
- align-items: center;
- }
- .navigator:before {
- content: " ";
- position: absolute;
- left: 30rpx;
- top: 0;
- right: 30rpx;
- height: 1px;
- border-top: 1rpx solid #d8d8d8;
- color: #d8d8d8;
- }
- .navigator:first-child:before {
- display: none;
- }
- .navigator-text {
- flex: 1;
- }
- .navigator-arrow {
- padding-right: 26rpx;
- position: relative;
- }
- .navigator-arrow:after {
- content: " ";
- display: inline-block;
- height: 18rpx;
- width: 18rpx;
- border-width: 2rpx 2rpx 0 0;
- border-color: #888;
- border-style: solid;
- transform: matrix(0.71, 0.71, -0.71, 0.71, 0, 0);
- position: absolute;
- top: 50%;
- margin-top: -8rpx;
- right: 28rpx;
- }
- .kind-list-item {
- margin: 20rpx 0;
- background-color: #fff;
- border-radius: 4rpx;
- overflow: hidden;
- }
- .kind-list-item:first-child {
- margin-top: 0;
- }
- .kind-list-text {
- flex: 1;
- }
- .kind-list-img {
- width: 60rpx;
- height: 60rpx;
- }
- .kind-list-item-hd {
- padding: 30rpx;
- display: flex;
- align-items: center;
- transition: opacity 0.3s;
- }
- .kind-list-item-hd-show {
- opacity: 0.2;
- }
- .kind-list-item-bd {
- height: 0;
- overflow: hidden;
- }
- .kind-list-item-bd-show {
- height: auto;
- }
app.wxss代码:
- /**app.wxss**/
- /* reset */
- page {
- background-color: #F8F8F8;
- height: 100%;
- font-size: 32rpx;
- line-height: 1.6;
- }
- checkbox, radio{
- margin-right: 10rpx;
- }
- button{
- margin-top: 20rpx;
- margin-bottom: 20rpx;
- }
- form{
- width: 100%;
- }
- /* lib */
- .strong{
- font-weight: bold;
- }
- .tc{
- text-align: center;
- }
- /* page */
- .container {
- display: flex;
- flex-direction: column;
- min-height: 100%;
- justify-content: space-between;
- font-size: 32rpx;
- font-family: -apple-system-font,Helvetica Neue,Helvetica,sans-serif;
- }
- .page-head{
- padding: 60rpx 50rpx 80rpx;
- text-align: center;
- }
- .page-head-title{
- display: inline-block;
- padding: 0 40rpx 20rpx 40rpx;
- font-size: 32rpx;
- color: #BEBEBE;
- }
- .page-head-line{
- margin: 0 auto;
- width: 150rpx;
- height: 2rpx;
- background-color: #D8D8D8;
- }
- .page-head-desc{
- padding-top: 20rpx;
- color: #9B9B9B;
- font-size: 32rpx;
- }
- .page-body {
- width: 100%;
- flex-grow: 1;
- overflow-x: hidden;
- }
- .page-body-wrapper {
- display: flex;
- flex-direction: column;
- align-items: center;
- width: 100%;
- }
- .page-body-wording {
- text-align: center;
- padding: 200rpx 100rpx;
- }
- .page-body-info {
- display: flex;
- flex-direction: column;
- align-items: center;
- background-color: #fff;
- width: 100%;
- padding: 50rpx 0 150rpx 0;
- }
- .page-body-title {
- margin-bottom: 100rpx;
- font-size: 32rpx;
- }
- .page-body-text {
- font-size: 30rpx;
- line-height: 26px;
- color: #ccc;
- }
- .page-body-text-small {
- font-size: 24rpx;
- color: #000;
- margin-bottom: 100rpx;
- }
- .page-foot{
- margin: 100rpx 0 30rpx 0;
- text-align: center;
- color: #1aad19;
- font-size: 0;
- }
- .icon-foot{
- width: 152rpx;
- height: 23rpx;
- }
- .page-section{
- width: 100%;
- margin-bottom: 60rpx;
- }
- .page-section_center{
- display: flex;
- flex-direction: column;
- align-items: center;
- }
- .page-section:last-child{
- margin-bottom: 0;
- }
- .page-section-gap{
- box-sizing: border-box;
- padding: 0 30rpx;
- }
- .page-section-spacing{
- box-sizing: border-box;
- padding: 0 80rpx;
- }
- .page-section-title{
- font-size: 28rpx;
- color: #999999;
- margin-bottom: 10rpx;
- padding-left: 30rpx;
- padding-right: 30rpx;
- }
- .page-section-gap .page-section-title{
- padding-left: 0;
- padding-right: 0;
- }
- .page-section-ctn{
- }
- /* widget */
- .btn-area{
- margin-top: 60rpx;
- box-sizing: border-box;
- width: 100%;
- padding: 0 30rpx;
- }
- .image-plus {
- width: 150rpx;
- height: 150rpx;
- border: 2rpx solid #D9D9D9;
- position: relative;
- }
- .image-plus-nb{
- border: 0;
- }
- .image-plus-text{
- color: #888888;
- font-size: 28rpx;
- }
- .image-plus-horizontal {
- position: absolute;
- top: 50%;
- left: 50%;
- background-color: #d9d9d9;
- width: 4rpx;
- height: 80rpx;
- transform: translate(-50%, -50%);
- }
- .image-plus-vertical {
- position: absolute;
- top: 50%;
- left: 50%;
- background-color: #d9d9d9;
- width: 80rpx;
- height: 4rpx;
- transform: translate(-50%, -50%);
- }
- .demo-text-1{
- position: relative;
- align-items: center;
- justify-content: center;
- background-color: #1AAD19;
- color: #FFFFFF;
- font-size: 36rpx;
- }
- .demo-text-1:before{
- content: 'A';
- position: absolute;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- }
- .demo-text-2{
- position: relative;
- align-items: center;
- justify-content: center;
- background-color: #2782D7;
- color: #FFFFFF;
- font-size: 36rpx;
- }
- .demo-text-2:before{
- content: 'B';
- position: absolute;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- }
- .demo-text-3{
- position: relative;
- align-items: center;
- justify-content: center;
- background-color: #F1F1F1;
- color: #353535;
- font-size: 36rpx;
- }
- .demo-text-3:before{
- content: 'C';
- position: absolute;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- }
效果如图:
微信小程序——组件(二)的更多相关文章
- 微信小程序配置二
tabBar 客户端窗口底部的tab页面切换,只能配置最好两个.最多5个tab 属性说明: 属性 类型 必填 默认值 描述 color HexColor 是 tab上的文字默认颜色 selectedC ...
- 微信小程序组件设计规范
微信小程序组件设计规范 组件化开发的思想贯穿着我开发设计过程的始终.在过去很长一段时间里,我都受益于这种思想. 组件可复用 - 减少了重复代码量 组件做为抽离的功能单元 - 方便维护 组件作为temp ...
- 微信小程序组件学习 -- 注册页面
微信小程序组件使用手册地址: 1. 百度搜索"微信公众平台",扫码登录之后,点击帮助文档里面的普通小程序. 2. 接着选择"开发"-->"组件& ...
- 详解封装微信小程序组件及小程序坑(附带解决方案)
一.序 上一篇介绍了如何从零开发微信小程序,博客园审核变智障了,每次代码都不算篇幅,好好滴一篇原创,不到3分钟从首页移出来了.这篇介绍一下组件封装和我的踩坑历程. 二.封装微信小程序可复用组件 首先模 ...
- 前端笔记之微信小程序(二){{}}插值和MVVM模式&数据双向绑定&指令&API
一.双花括号{{}}插值和MVVM模式 1.1 体会{{}}插值 index.wxml的标签不是html的那些标签,这里的view就是div. {{}}这样的插值写法,叫做mustache语法.mus ...
- 微信小程序组件——详解wx:if elif else的用法
背景 在学习微信小程序开发wxml页面时,需要使用if,else来判断组件是否进行展示,代码如下 <view wx:if="{{is_login==1}}">成功登录& ...
- 微信小程序组件构建UI界面小练手 —— 表单登录注册微信小程序
通过微信小程序中丰富的表单组件来完成登录界面.手机快速注册界面.企业用户注册界面的微信小程序设计. 将会用到view视图容器组件.button按钮组件.image图片组件.input输入框组件.che ...
- 【腾讯Bugly干货分享】打造“微信小程序”组件化开发框架
本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:http://mp.weixin.qq.com/s/2nQzsuqq7Avgs8wsRizUhw 作者:Gc ...
- 微信小程序(组件demo)以及预览方法:(小程序交流群:604788754)
1. 获取微信小程序的 AppID 登录 https://mp.weixin.qq.com ,就可以在网站的"设置"-"开发者设置"中,查看到微信小程序的 Ap ...
随机推荐
- springboot整合spring data jpa 动态查询
Spring Data JPA虽然大大的简化了持久层的开发,但是在实际开发中,很多地方都需要高级动态查询,在实现动态查询时我们需要用到Criteria API,主要是以下三个: 1.Criteria ...
- java中Map转化为bean
Map 集合类用于存储元素对(称作“键”和“值”),其中每个键映射到一个值,在java编程中会经常用到.但是当我们进行业务逻辑的处理或着操作数据库时,往往应用的是我们自己定义的的Bean或VO来传递和 ...
- <a>标签里面嵌图片<img>下面出现一小段空白的原因
今天做项目的时候,发现在a标签,里面嵌入<img>会出现空白 css 内容: a{ border:1px solid black; } img{ width:200px; } html内容 ...
- es6之 async await 使用小计
var sleep = (time)=>{ return new Promise((resolve,reject)=>{ setTimeout(()=>{ resolve('ok') ...
- Java入门系列-21-多线程
什么是线程 在操作系统中,一个应用程序的执行实例就是进程,进程有独立的内存空间和系统资源,在任务管理器中可以看到进程. 线程是CPU调度和分派的基本单位,也是进程中执行运算的最小单位,可完成一个独立的 ...
- C#操作Redis String字符串
/// <summary> /// Redis String 操作 /// </summary> public static void Redis_String() { Red ...
- YII2应用结构
应用中最重要的目录和文件(假设应用根目录是 basic): 一般来说,应用中的文件可被分为两类:在 basic/web 下的和在其它目录下的.前者可以直接通过 HTTP 访问(例如浏览器),后者不能也 ...
- js confirm实现换行
js中confirm或者alert不识别标签,所以要换行的话可以采用下面方式 \u000d 或者 \r: <script> var res=confirm(\"这是测试工作: \ ...
- C#语言-08.序列化与反序列化
a. 序列化:是将对象的状态存储到特定存储介质中的过程 i. 语法:public void Serialize(序列化过程的文件流,保存的对象) b. 返序列化:是从特定存储介质中将数据重新构建对象的 ...
- 关于webApi使用session
1.关于webApi使用session 在Global.asax中注册session添加以下代码 public override void Init() { //开启session this.Post ...