
Element UI Drawer 隐藏后元素绝对定位到右下角的解决方案
项目中,需要在 Drawer 隐藏后,其内部的 div 元素以绝对定位的方式悬浮在页面右下角。然而,Element UI Drawer 默认隐藏方式为 display: none;,导致 position: fixed; 和高 z-index 属性失效。
解决方法是使用 Vue 的 teleport 功能,将需要绝对定位的元素渲染到 body 元素下。当 Drawer 隐藏时,该元素仍然可见并保持其位置。
实现代码如下:
<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 属性值以适应您的页面布局。
以上就是Element-UI Drawer隐藏后如何将内部元素绝对定位到右下角?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号