微信小程序自定义文本输入框-升级版
之前也有写过微信小程序自定义输入框的实现方法,请看文章;https://blog.csdn.net/chengdong1314/article/details/123577082
这里之所以再次写这篇文章的原因有如下两点:
1.原来的输入框的js代码部分事放在page里面做的,照成弹出框复用非常不方便
2.原来的文章因为开始写的不好,在评论区多次编辑,扰乱了读者的注意力
3.自己做了一部分改善,这里想体现出来
相对于之前的代码这里在模块里面加了js的文件:
其内容如下:
// modal.js/*** 文本弹窗https://blog.csdn.net/ZNYSYS520/article/details/81878598*/function showDialogBtn(res) {var that = res;var _hintinfo = that.data.hintinfo;_hintinfo.ishow = true;that.setData({hintinfo: _hintinfo})}/*** 隐藏模态对话框*/function hideModal(res) {var that = res;var _hintinfo = that.data.hintinfo;_hintinfo.ishow = false;that.setData({hintinfo: _hintinfo})}function inputChange(res){this.data.hintinfo.inputValue = res.detail.value; }function showswitch(res) {var that = this;var _hintinfo = that.data.hintinfo;_hintinfo.passwordshow = !_hintinfo.passwordshow;that.setData({hintinfo: _hintinfo})}/*文本弹窗类的特定数据*ishow是否显示,默认不显示*title弹窗标题,数据过多的时候会分行显示*inputplaceholder输入框预输入内容*inputValue当前输入的值,onConfirm事件可通过该值确定输入的内容*confirmtext确认按钮显示的文字*canletext取消按钮显示的文字*open是否显示密码*/ var modal_default_data = {ishow: false,title: "注意:手机号是获取云文件的凭证,输入错误将获取不到之前的文件",inputplaceholder: "请输入手机号",passwordplaceholder: "请输入密码",inputValue: "",confirmtext: "确定",canletext: "取消",passwordshow: false};module.exports = {showDialogBtn,hideModal,showswitch:showswitch,inputChange:inputChange,modal_default_data:modal_default_data}
wxml文件内容如下:
<!--文本弹窗https://blog.csdn.net/ZNYSYS520/article/details/81878598-->
<template name="TextPopupBox">
<view class="modal-mask" bindtap="hideModal" catchtouchmove="preventTouchMove" wx:if="{{ishow}}"></view>
<view class="modal-dialog" wx:if="{{ishow}}"><view class="modal-title">{{title}}</view><view class="modal-content"><text>手机号:</text><view class="modal-input"><input placeholder-class="input-holder" type="number" bindinput="inputChange" class="input" placeholder="{{inputplaceholder}}" maxlength="11"></input></view><text>密码:</text><view class="modal-input"><input type="text" password="{{!passwordshow}}" placeholder="{{passwordplaceholder}}" class="password_input"/><image src="{{passwordshow?'../../images/open_eye.png':'../../images/no_eye.png'}}" class="eye" catchtap="showDialogBtn" /></view></view><view class="modal-footer"><view class="btn-cancel" bindtap="onCancel" data-status="cancel">{{canletext}}</view><view class="btn-confirm" bindtap="onConfirm" data-status="confirm">{{confirmtext}}</view></view>
</view>
</template>
wxss文件内容如下:
/* component\modal\modal.wxss*/
.show-btn {margin-top: 100rpx;
color: #22cc22;
}.modal-mask {width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
background: #000;
opacity: 0.5;
overflow: hidden;
z-index: 9000;
color: #fff;
}.modal-dialog {width: 540rpx;
overflow: hidden;
position: fixed;
top: 50%;
left: 0;
z-index: 9999;
background: #f9f9f9;
margin: -180rpx 105rpx;
border-radius: 36rpx;
}.modal-title {padding-top: 50rpx;
font-size: 36rpx;
color: #030303;
text-align: center;
}.modal-content {padding: 50rpx 32rpx;
}.modal-input {display: flex;
background: #fff;
border: 2rpx solid #ddd;
border-radius: 4rpx;
font-size: 28rpx;
}.password_input {width: 100%;
height: 28rpx;
font-size: 28rpx;
line-height: 28rpx;
padding: 0 20rpx;
box-sizing: border-box;
color: #333;
}input-holder {color: #666;
font-size: 28rpx;
}.modal-footer {display: flex;
flex-direction: row;
height: 86rpx;
border-top: 1px solid #dedede;
font-size: 34rpx;
line-height: 86rpx;
}.btn-cancel {width: 50%;
color: #666;
text-align: center;
border-right: 1px solid #dedede;
}.btn-confirm {width: 50%;
color: #ec5300;
text-align: center;
}
.eye {width: 60rpx;height: 60rpx;margin-left: 20rpx;flex-shrink: 0;}
这里介绍怎么引用这个模块,在界面的js做如下引用并且定义数据:
var modal = require("../../component/modal/modal.js")
Page({/*** 页面的初始数据*/data: {hintinfo:modal.modal_default_data},
然后再界面中实现如下的函数:
/*.............................................文本弹窗类开始.............................................*/ /*** 文本弹窗https://blog.csdn.net/ZNYSYS520/article/details/81878598*//*** 对话框取消按钮点击事件*/onCancel: function () {modal.hideModal(this);util.showHintModal("请注意:取消输入将不能够获取到云文件,并且和文件相关功能不能够使用");},/*** 对话框确认按钮点击事件*/onConfirm: function (res) {modal.hideModal(this);console.log("set phone:",this.data.hintinfo.inputValue)if((this.data.hintinfo.inputValue.length!=11) || isNaN(this.data.hintinfo.inputValue)){util.showHintModal("手机号格式错误,请重新输入!");modal.showDialogBtn(this);}else{app.globalData.phone=this.data.hintinfo.inputValue;wx.setStorageSync('phone', app.globalData.phone)}},/*** 弹出框蒙层截断touchmove事件*/preventTouchMove: function () {},/*** 对话框湿输入数据改变事件*/inputChange:modal.inputChange,/*** 切换是否显示密码按键*/showDialogBtn:modal.showswitch/*.............................................文本弹窗类结束.............................................*/
到这里js文件的内容就结束了,接下来再界面的wxml做如下引用:
<import src="../../component/modal/modal" />
<template is="TextPopupBox" data="{{...hintinfo}}"/>
捯饬代码的移植工作已经结束,代码最大的改善是把可以服用的函数和数据都放到modal.js里面作为模块统一声明了!
因为本章节内容是从实际商务项目中抽出来的,所以源代码没有办法公开,请见谅,如果有需要或者说想一起讨论,请根据"个人简介"的联系方式联系本人,谢谢支持!
微信小程序自定义文本输入框-升级版相关推荐
- 微信小程序自定义文本输入框
本文摘录于:https://blog.csdn.net/ZNYSYS520/article/details/81878598-只是做学习备份之用,绝无抄袭之意,有疑惑请联系本人! 这里参考git仓库, ...
- 微信小程序-自定义底部导航
代码地址如下: http://www.demodashi.com/demo/14258.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.c ...
- 微信聊天自动解析html文本,微信小程序纯文本实现@功能
前言 大家肯定对@功能不陌生,在如今的各大社交软件中它是一种不可或缺的功能.实现@人的功能并不复杂,只需将@人员的id传给后端,后端下发通知即可.主要的复杂点在于一键删除功能与变色功能,web端可以使 ...
- 微信小程序富文本编辑器获取内容
1.新建wxParse文件夹 里面的结构是这样:wxParse :{ emojis (文件夹) html2json.js (文件) htmlparser.js(文件) showdown.js (文件) ...
- 微信小程序自定义搜索框(searchbar)
微信小程序自定义搜索框(searchbar) 样式截图展示: 功能描述:微信小程序页面需要加入搜索框时,可以直接引用到页面中使用,当用户未输入任何关键字时如第一张图所示,当用户输入要搜索的关键字时如第 ...
- 微信小程序自定义类似微信联系人组件
微信小程序自定义联系人弹窗 在小程序项目中需要有一个选择人员项,想着看着好看一些,所以做成类似微信联系人的界面,由于本人是后端人员,效果不是特别好看,ui使用的是weui 效果图如下: 使用的是小程序 ...
- 微信小程序富文本解析
微信小程序富文本解析 *人狠话不多,直接代码搞起* html代码(后台返回的html代码) <p>这是一段文字</p><p><strong>这是加粗的字 ...
- 微信小程序自定义模态框,官方版本与自定义可扩展版本
微信小程序自定义模态框,官方版本与自定义可扩展版本 提示:文章最后有源码,可自取 文章主要通过模仿官方的模态框进行自定义模态框的设计,我将会先讲述原理,然后给出源码,最后指出一些需要注意的地方 提示: ...
- 微信小程序自定义授权弹框
微信小程序自定义授权弹框 最近微信获取用户信息的接口有调整,就是这货:wx.getUserInfo(OBJECT),文档描述如下: 此接口有调整,使用该接口将不再出现授权弹窗,请使用 <butt ...
最新文章
- Batch Size对神经网络训练的影响
- 演练-基于lamp安装wordpress--含安装包
- Quartz 框架快速入门(四)
- 放个手机在单位自动打卡_1秒识别打卡,无感知考勤系统重磅来袭!
- extern的关键字用法(C# 参考)
- SPI接口通信协议详解:SPI时序、2线、3线、4线SPI及4种常用工作模式
- 三分钟学会缓存工具DiskLruCache
- mybatis insert返回主键_MyBatis官方文档XML 映射文件
- flask-admin初次使用遇到的几个小问题(显示对象内存地址,编辑无法正确跳转)
- 如果NATv6 是个笑话,那么 IPv6 是什么?
- 删除指定类型的所有字段
- L1-022 奇偶分家 (10 分) — 团体程序设计天梯赛
- 【vijos】P1190 繁忙的都市
- bodymovin导出没有html5,AE脚本-导出json格式的Web动画工具 Bodymovin v5.5.3+使用教程
- Mysql导出表结构和数据
- 怎么用计算机拨号手机,手机怎么连接电脑拨号打电话
- python找色_利用python检测色情图片简易实例
- 小米机型安全删除内置软件列表 miui12 miui13 可删除内置
- 深入解析Javascript异步编程
- 2022年中国镍期货成交量、成交金额、成交价格走势分析:镍开盘价持续走高[图]