van-overlay遮罩层出现后van-field的placeholder会在遮罩层上面

文章讨论了在van-overlay遮罩层显示时,van-field的placeholder如何被覆盖的问题。解决方法指出z-index对非定位元素无效,需检查van-field和overlay的父元素position属性,并将其设置为relative、absolute或fixed以确保z-index生效。

van-overlay遮罩层出现后van-field的placeholder会在遮罩层上面

遇到问题
如题,如图,加了z-index后不生效

<van-overlay show="{{ showOverlay }}" class="zindex" />
.zindex {
  z-index: 9999;
}

在这里插入图片描述
解决办法
z-index 只对定位元素生效!!! 检查 van-field 和 van-overlay 相关父级元素是否正确设置了 position 属性,比如 relative、absolute 或 fixed。修改后代码如下:

<van-overlay show="{{ showOverlay }}" class="zindex relative" />
.zindex {
  z-index: 9999;
}
.relative{
position: relative;
}
<!-- 新增错误提示样式 --> <van-popup show="{{ show }}" position="bottom" custom-class="custom-form-modal" close-on-click-overlay="{{ false }}" bind:close="closeModal"> <!-- 新增加载状态 --> <van-overlay show="{{ loading }}" /> <van-loading wx:if="{{ loading }}" custom-class="loading-overlay" size="24px" vertical>加载中...</van-loading> <view class="modal-header"> <view class="title">{{ title }}</view> <van-icon name="close" size="20px" bind:tap="closeModal" /> </view> <scroll-view scroll-y class="form-content"> <block wx:if="{{!loading && config.length}}"> <block wx:for="{{ config }}" wx:key="index"> <view class="form-row"> <view class="row-title">{{ item.title }}</view> <view class="row-content"> <block wx:for="{{ item.items }}" wx:key="field"> <!-- 新增错误提示 --> <view wx:if="{{ errors[subItem.field] }}" class="error-message"> {{ errors[subItem.field] }} </view> <!-- 输入框 --> <block wx:if="{{ subItem.type === 'input' }}"> <van-field value="{{ formData[subItem.field] }}" placeholder="{{ subItem.placeholder || '请输入' }}" data-field="{{ subItem.field }}" bind:change="handleInput" border="{{ false }}" /> </block> <!-- 时间选择器 --> <block wx:elif="{{ subItem.type === 'time' }}"> <view class="time-picker {{ errors[subItem.field] ? 'error' : '' }}" bindtap="openTimePicker" data-field="{{ subItem.field }}"> {{ formData[subItem.field] || subItem.placeholder || '选择时间' }} </view> <van-popup show="{{ tempData[subItem.field + '_show'] }}" position="bottom" data-field="{{ subItem.field }}" bind:close="closeTimePicker"> <van-datetime-picker type="time" value="{{ formData[subItem.field] }}" data-field="{{ subItem.field }}" bind:confirm="handleTimeChange" bind:cancel="closeTimePicker" /> </van-popup> </block> <!-- 下拉选择器 --> <block wx:elif="{{ subItem.type === 'select' }}"> <view class="select-picker {{ errors[subItem.field] ? 'error' : '' }}" bindtap="openSelectPicker" data-field="{{ subItem.field }}"> {{ getSelectLabel(subItem.options, formData[subItem.field]) || subItem.placeholder || '请选择' }} </view> <van-popup show="{{ tempData[subItem.field + '_show'] }}" position="bottom" data-field="{{ subItem.field }}" bind:close="closeSelectPicker"> <van-picker show-toolbar columns="{{ subItem.options }}" value-key="label" data-field="{{ subItem.field }}" bind:confirm="handleSelectChange" bind:cancel="closeSelectPicker" /> </van-popup> </block> <!-- 按钮 --> <block wx:elif="{{ subItem.type === 'button' }}"> <van-button type="primary" size="small" data-field="{{ subItem.field }}" bind:tap="handleButtonClick"> {{ subItem.text }} </van-button> </block> <!-- 自定义组件 --> <block wx:elif="{{ subItem.type === 'custom' }}"> <custom-component value="{{ formData[subItem.field] }}" config="{{ subItem.config }}" data-field="{{ subItem.field }}" bind:event="handleCustomEvent" /> </block> </block> </view> </view> </block> </block> </scroll-view> <view class="modal-footer"> <van-button type="default" size="large" bind:tap="closeModal">取消</van-button> <van-button type="primary" size="large" bind:tap="submitForm">提交</van-button> </view> </van-popup> 第二个for循环数据错误
08-02
/* 文件路径: webapp/components/custom-form-modal/index.js */ // components/custom-form-modal/index.js Component({ properties: { show: { type: Boolean, value: false }, // 是否显示模态框 title: { type: String, value: '表单标题' }, // 模态框标题 config: { type: Array, value: [] }, // 表单配置数组 formData: { type: Object, value: {} } // 表单数据 }, data: { // 用于存储临时数据(如下拉选择器的选中值) tempData: {} }, methods: { // 关闭模态框 closeModal() { this.triggerEvent('close'); }, // 表单提交 submitForm() { this.triggerEvent('submit', this.data.formData); }, // 输入框变化 handleInput(e) { const { field } = e.currentTarget.dataset; const value = e.detail; this.setData({ [`formData.${field}`]: value }); this.triggerEvent('change', { field, value }); }, // 时间选择器变化 handleTimeChange(e) { const { field } = e.currentTarget.dataset; const value = e.detail; this.setData({ [`formData.${field}`]: value, [`tempData.${field}_show`]: false // 关闭时间选择器 }); this.triggerEvent('change', { field, value }); }, // 下拉选择器变化 handleSelectChange(e) { const { field } = e.currentTarget.dataset; const value = e.detail; this.setData({ [`formData.${field}`]: value, [`tempData.${field}_show`]: false // 关闭选择器 }); this.triggerEvent('change', { field, value }); }, // 打开时间选择器 openTimePicker(e) { const { field } = e.currentTarget.dataset; this.setData({ [`tempData.${field}_show`]: true }); }, // 打开下拉选择器 openSelectPicker(e) { const { field } = e.currentTarget.dataset; this.setData({ [`tempData.${field}_show`]: true }); }, // 按钮点击事件 handleButtonClick(e) { const { field } = e.currentTarget.dataset; this.triggerEvent('buttonClick', { field }); }, // 自定义组件事件 handleCustomEvent(e) { const { field, event } = e.detail; this.triggerEvent('customEvent', { field, ...event }); } } }); ================================================================================ /* 文件路径: webapp/components/custom-form-modal/index.json */ { "component": true, "usingComponents": { "van-field": "@vant/weapp/field/index", "van-picker": "@vant/weapp/picker/index", "van-datetime-picker": "@vant/weapp/datetime-picker/index", "van-button": "@vant/weapp/button/index", "van-popup": "@vant/weapp/popup/index" } } ================================================================================ /* 文件路径: webapp/components/custom-form-modal/index.wxml */ <!-- components/custom-form-modal/index.wxml --> <van-popup show="{{ show }}" position="bottom" custom-class="custom-form-modal" close-on-click-overlay="{{ false }}" bind:close="closeModal" > <view class="modal-header"> <view class="title">{{ title }}</view> <van-icon name="close" size="20px" bind:tap="closeModal" /> </view> <scroll-view scroll-y class="form-content"> <block wx:for="{{ config }}" wx:key="index"> <view class="form-row"> <view class="row-title">{{ item.title }}</view> <view class="row-content"> <block wx:for="{{ item.items }}" wx:key="field"> <!-- 输入框 --> <block wx:if="{{ subItem.type === 'input' }}"> <van-field value="{{ formData[subItem.field] }}" placeholder="{{ subItem.placeholder || '请输入' }}" data-field="{{ subItem.field }}" bind:change="handleInput" /> </block> <!-- 时间选择器 --> <block wx:elif="{{ subItem.type === 'time' }}"> <view class="time-picker" bindtap="openTimePicker" data-field="{{ subItem.field }}"> {{ formData[subItem.field] || subItem.placeholder || '选择时间' }} </view> <van-popup show="{{ tempData[subItem.field + '_show'] }}" position="bottom" bind:close="closeTimePicker" > <van-datetime-picker type="time" value="{{ formData[subItem.field] }}" data-field="{{ subItem.field }}" bind:confirm="handleTimeChange" bind:cancel="closeTimePicker" /> </van-popup> </block> <!-- 下拉选择器 --> <block wx:elif="{{ subItem.type === 'select' }}"> <view class="select-picker" bindtap="openSelectPicker" data-field="{{ subItem.field }}"> {{ getSelectLabel(subItem.options, formData[subItem.field]) || subItem.placeholder || '请选择' }} </view> <van-popup show="{{ tempData[subItem.field + '_show'] }}" position="bottom" bind:close="closeSelectPicker" > <van-picker show-toolbar columns="{{ subItem.options }}" value-key="label" data-field="{{ subItem.field }}" bind:confirm="handleSelectChange" bind:cancel="closeSelectPicker" /> </van-popup> </block> <!-- 按钮 --> <block wx:elif="{{ subItem.type === 'button' }}"> <van-button type="primary" size="small" data-field="{{ subItem.field }}" bind:tap="handleButtonClick" > {{ subItem.text }} </van-button> </block> <!-- 自定义组件 --> <block wx:elif="{{ subItem.type === 'custom' }}"> <custom-component value="{{ formData[subItem.field] }}" config="{{ subItem.config }}" data-field="{{ subItem.field }}" bind:event="handleCustomEvent" /> </block> </block> </view> </view> </block> </scroll-view> <view class="modal-footer"> <van-button type="default" size="large" bind:tap="closeModal">取消</van-button> <van-button type="primary" size="large" bind:tap="submitForm">提交</van-button> </view> </van-popup> ================================================================================ /* 文件路径: webapp/components/custom-form-modal/index.wxss */ /* components/custom-form-modal/index.wxss */ .custom-form-modal { height: 80vh; border-top-left-radius: 16rpx; border-top-right-radius: 16rpx; display: flex; flex-direction: column; } .modal-header { display: flex; justify-content: space-between; align-items: center; padding: 24rpx 32rpx; border-bottom: 1rpx solid #eee; } .modal-header .title { font-size: 36rpx; font-weight: bold; } .form-content { flex: 1; padding: 0 32rpx; } .form-row { padding: 24rpx 0; border-bottom: 1rpx solid #f5f5f5; } .row-title { font-size: 28rpx; color: #333; margin-bottom: 16rpx; } .row-content { display: flex; flex-wrap: wrap; gap: 20rpx; } .row-content > view { flex: 1; min-width: 45%; } .time-picker, .select-picker { padding: 20rpx; border: 1rpx solid #ebedf0; border-radius: 8rpx; font-size: 28rpx; } .modal-footer { display: flex; padding: 24rpx 32rpx; gap: 20rpx; } .modal-footer van-button { flex: 1; }
07-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值