uni-app弹窗 小程序弹窗

本文介绍了小程序中常见的几种弹窗交互,包括带对号提示、无图标提示、加载提示以及模态对话框的使用方法。通过uni.showToast、uni.showLoading和uni.showModal等API,展示了如何创建和关闭提示,以及实现确认与取消操作。这些功能在前端开发中对于提供用户反馈和交互体验至关重要。
摘要由CSDN通过智能技术生成

1.带对号弹窗 小程序把开头uni换成wx即可

uni.showToast({
	title: '标题',
	duration: 5000
});

2.无icon弹窗

uni.showToast({
	title: '标题',
	icon:'none',
	duration: 5000
});

3.加载弹窗

uni.showLoading({
    title: '加载中',
    duration: 5000
});

以上都可以去除时间 使用uni.hideToast();关闭

uni.hideToast();

4.只有确认提示

wx.showModal({
	title: '提示',
	content: '只有确认!',
	showCancel: false,
	success: function(res) {
	console.log('用户点击了确认')
})

5.带取消 确认交互弹窗

uni.showModal({
    title: '提示',
    content: '这是一个模态弹窗',
    success: function (res) {
        if (res.confirm) {
            console.log('用户点击确定');
        } else if (res.cancel) {
            console.log('用户点击取消');
        }
    }
});
uni-app开发微信小程序中,当弹窗(Modal)出现并且包含输入框(Input)时,你可以通过监听`input`元素的`focus`事件来获取焦点。首先,在弹窗组件的数据里,你需要定义一个表示输入框是否聚焦的状态,比如: ```javascript data() { return { modalVisible: false, focusOnInput: false // 新增一个变量用于追踪输入框焦点状态 } } ``` 然后,在你想显示弹窗的地方,添加打开弹窗并监听输入框焦点的代码: ```javascript openModal() { this.modalVisible = true; const inputEle = this.$refs.yourInputRef; // 假设输入框的ref名为yourInputRef if (inputEle) { inputEle.focus(); // 当弹窗打开时立即聚焦输入框 inputEle.addEventListener('focus', () => { // 监听输入框聚焦事件 this.focusOnInput = true; }); inputEle.addEventListener('blur', () => { // 监听输入框失去焦点事件 this.focusOnInput = false; }); } }, closeModal() { this.modalVisible = false; // 如果需要,移除之前的事件监听 if (this.focusOnInput) { // 这里可以移除inputEle的事件监听,防止内存泄漏 // 具体代码取决于uni-app API,可能类似于 this.$refs.yourInputRef.removeEventListener('focus', ...) } } ``` 在模板部分,确保将`ref`属性应用到输入框上: ```html <view v-if="modalVisible"> <input type="text" ref="yourInputRef" placeholder="请输入内容" @focus="handleFocus" /> </view> ``` 并在对应的JS文件中处理`handleFocus`函数: ```javascript methods: { handleFocus(e) { this.focusOnInput = true; // 更新状态 } } ``` 当你需要检查输入框是否有焦点时,可以直接访问`focusOnInput`这个数据字段。例如: ```javascript if (this.focusOnInput) { console.log('当前有输入框聚焦'); } else { console.log('输入框未聚焦'); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值