微信小程序弹框显示自定义内容(1)

1:通过查看详情绑定函数toClock
toClock 触发弹框

<view   bindtap="toClock" data-id="{{item.contentdesc}}">查看详情</view>

2:准备model模块
model放在需要触发按钮同样的文件wxxml即可

<modal hidden="{{hiddenmodalput}}" title="详细介绍" confirm-text="提交" cancel-text="取消" bindcancel="cancel" bindconfirm="confirm">
    <input type='text'  value="{{num}}"     bindinput='tdate'  auto-focus/>
 </modal>

hidden=“{{hiddenmodalput}}”
hiddenmodalput这个控制弹框显示或消失
默认消失也就是关闭的,所以在js文件data设置

data: {
    hiddenmodalput:true
  },

input即显示文件内容
3:toClock函数
将num显示在弹框里

 //弹框
  toClock(e){
 var that=this
 var num="123"
    that.setData({
      num:num,
      hiddenmodalput: !this.data.hiddenmodalput
   })
  },

4:确认和取消
点击会实现弹框的消失(想实现更多的内容可在取消确认自己写函数)

 //取消按钮
 cancel: function(){
  this.setData({
      hiddenmodalput: true
  });
},
//确认
confirm: function(e){
  this.setData({
    hiddenmodalput: true
});
},

关于微信弹框更多知识微信搜索公众号:小白XBIT

### 微信小程序自定义实现方法 #### 创建自定义窗组件 为了创建一个功能丰富的自定义窗,可以通过组合 CSS 和 JavaScript 来构建。首先,在项目中新建一个名为 `custom-modal` 的组件文件夹,并在其内部建立相应的 WXML、WXSS 及 JS 文件。 #### 定义组件结构与样式 在 `custom-modal.wxml` 中设计基本布局: ```html <view class="modal-mask" bindtap="cancel_and_close"> <view class="modal-content" catchtouchmove="preventTouchMove"> <text>{{title}}</text> <text>{{content}}</text> <button bindtap="confirm">确认</button> <button bindtap="cancel_and_close">取消</button> </view> </view> ``` 对于外观调整,则通过修改 `custom-modal.wxss` 文件中的类名对应的样式规则来完成[^3]。 #### 设置组件逻辑 编辑 `custom-modal.js` 文件以配置组件属性并编写交互事件处理函数: ```javascript Component({ /** * 组件的属性列表 */ properties: { show: Boolean, title: String, content: String }, /** * 组件的方法列表 */ methods: { preventTouchMove() {}, confirm(event) { // 执行确认操作后的回调动作 console.log('Confirmed'); this.triggerEvent('confirmed', event.detail); this.setData({ show: false }); }, cancel_and_close(event) { // 关闭窗的动作 console.log('Cancelled or closed'); this.setData({ show: false }); } } }) ``` 此部分代码实现了当用户点击“确认”或“取消”按钮时触发相应的行为,并关闭当前显示的对话。 #### 使用自定义窗组件 最后一步是在页面或其他组件里引入这个新创建好的模块。假设有一个按钮用于展示该窗效果,那么可以在关联视图内加入如下标记语句[^1]: ```html <button bindtap="openPopup">打开窗</button> <!-- 引入自定义窗 --> <custom-modal wx:if="{{isShow}}" show="{{true}}" title="提示" content="这是一个简单的例子." /> ``` 同时还需要更新对应脚本里的状态管理变量以及控制其可见性的方法: ```javascript Page({ data: { isShow: false }, openPopup() { this.setData({ isShow: true }); } }); ``` 这样就完成了整个流程的设计与编码工作,使得开发者能够灵活运用此类控件提升用户体验质量[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1029179954

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值