
Element UI Drawer 隐藏后元素绝对定位到右下角的解决方案
项目中,需要在 Drawer 隐藏后,其内部的 div 元素以绝对定位的方式悬浮在页面右下角。然而,Element UI Drawer 默认隐藏方式为 display: none;,导致 position: fixed; 和高 z-index 属性失效。
解决方法是使用 Vue 的 teleport 功能,将需要绝对定位的元素渲染到 body 元素下。当 Drawer 隐藏时,该元素仍然可见并保持其位置。
审查 Vue 管理后台的变更请求(Vue 2 + Element UI,Vue 3 + Element Plus)。重点关注最小化变更实现、上线风险检查以及回归检查清单生成。
实现代码如下:
<code class="vue"><template>
<el-drawer :visible.sync="visible">
<teleport to="body">
<div v-if="visible" class="floating-div" style="position: fixed; bottom: 20px; right: 20px; z-index: 9999;">悬浮窗口</div>
</teleport>
</el-drawer>
</template>
<script>
export default {
data() {
return {
visible: true
};
},
};
</script></code>
关键在于:
- 使用
teleport to="body"将floating-div元素移动到body元素内。 - 使用
v-if="visible"确保只有在 Drawer 可见时才渲染floating-div元素,避免隐藏后仍然显示。 -
floating-div元素使用position: fixed;实现绝对定位,并设置bottom和right属性控制其位置。z-index确保其显示在其他元素之上。
通过此方法,即使 Drawer 隐藏,floating-div 元素仍会保持在页面右下角。 请注意调整 bottom 和 right 属性值以适应您的页面布局。

















