欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > vue elementui select下拉库组件鼠标移出时隐藏下拉框

vue elementui select下拉库组件鼠标移出时隐藏下拉框

2025/2/21 3:23:49 来源:https://blog.csdn.net/weixin_45804281/article/details/145596732  浏览:    关键词:vue elementui select下拉库组件鼠标移出时隐藏下拉框

方案:
select 监听 mouseleave事件,当鼠标离开时通过唯一标识ref设置select 下拉框隐藏,并做失焦

<el-select 
v-model="value" 
:popper-append-to-body="false"   
class="select_drop_inner" size="small"
placeholder="支持模型体验" 
ref="selectRef" 
@mouseleave.native="closeSelect"><el-option label="xxx" value="xxx"></el-option><el-option label="xxx" value="xxx"></el-option>
</el-select>

popper-append-to-body 属性是设置弹出框的位置的,设置为false比较方便我们通过当前select去获取对应的下拉框元素,如果设置为true的话对应下拉框元素会在最外层,不方便拿到并设置
在这里插入图片描述
但是设置当前属性也会导致出现下拉框被盖住不显示的样式问题
如果有这样的样式问题可以给select设置单独的class,然后设置以下的scss

.select_drop_inner {padding-bottom: 0.2rem;  //为下方的下拉框保留位置(20px)position: relative;top: 0.1rem; //兼容之前的样式,可以不加.el-select__tags {top: calc(50% - 0.1rem);}.el-select-dropdown {position: fixed !important;  //主要是修改下拉框为固定定位来解决不显示问题top: 3.2rem !important;  //上方的高度需自己计算(相关整个页面视口)}
}

mouseleave事件方法中做操作

closeSelect(){// 注意 selectRef 要和 ref="selectRef" 对应this.$refs.selectRef.$el.querySelector('.el-select-dropdown').style.display = 'none';// 隐藏下拉框后 同时 使 input 失去焦点this.$refs.selectRef.blur()},

参考至 https://blog.csdn.net/Z18834071903/article/details/138799731

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com