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

发布于:2025-02-13 ⋅ 阅读:(141) ⋅ 点赞:(0)

方案:
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


网站公告

今日签到

点亮在社区的每一天
去签到