【vue】在页面右下角添加悬浮按钮组件

效果图:

如上图所示,需要在页面的左下角添加一个悬浮按钮,鼠标放上去时候展示更多详细按钮,点击触发相应方法。

<div class="floating-component">
    <!-- 悬浮内容 -->
    <el-tooltip placement="left" effect="light" popper-class="too">
      <div slot="content">
        <el-button icon="el-icon-s-comment" class="butto" @click="handleQuestion">问题反馈 </el-button>
        <br/>
        <el-button icon="el-icon-question" class="butto" @click="handleHelp">帮助手册 </el-button>
      </div>
      <el-button icon="el-icon-menu" circle class="but"></el-button>
    </el-tooltip>
</div>
<style scoped>
.floating-component {
  position: fixed; /* 固定位置 */
  bottom: 5%; /* 下边距 */
  right: 1%; /* 右边距 */
  padding: 0;
  border-radius: 50%; /* 圆角 */
  z-index: 10000; /* 设置 z-index 确保悬浮在顶层 */
}
.but {
  font-size: larger;
  color: rgb(0,119,216);
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.2); /* 阴影 */
}
.butto {
  border: 0;
  padding: 10px;
  margin: 0;
}
</style>

<style>
.too.el-tooltip__popper.is-light {
  border: none !important;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.2);
  padding: 5px
}
/* 修改箭头边框 这里方位是left,所以 x-placement^="left" 并且是设置 border-right-color 的颜色*/
.too.el-tooltip__popper.is-light[x-placement^="left"] .popper__arrow {
  border-left-color: #eaeaea !important;
}
.too.el-tooltip__popper[x-placement^="left"] .popper__arrow {
  border-left-color: #eaeaea !important;
}
</style>

如果需要在所有页面上层展示,在vue项目的顶层路由文件中添加即可。

### 创建右下角浮动按钮组件 为了在 Vue 项目中实现一个右下角悬浮按钮,可以按照以下方式构建: #### 组件结构设计 创建一个新的 Vue 组件 `FloatingButton.vue` 并将其放置在一个全局可见的位置,比如 `App.vue` 文件中。该组件应具有固定的布局样式并支持交互逻辑。 以下是具体的代码实现: ```vue <template> <div class="floating-button-container"> <!-- 浮动按钮 --> <button class="floating-button" @click="handleClick"> {{ buttonText }} </button> </div> </template> <script lang="ts"> import { defineComponent } from 'vue'; export default defineComponent({ name: 'FloatingButton', data() { return { buttonText: '点击我', // 默认按钮文字 }; }, methods: { handleClick(): void { console.log('按钮被点击了!'); // 可在此处添加更多业务逻辑 }, }, }); </script> <style scoped> .floating-button-container { position: fixed; bottom: 20px; /* 距离底部的距离 */ right: 20px; /* 距离右侧的距离 */ z-index: 1000; /* 确保浮于其他内容之上 */ } .floating-button { width: 50px; height: 50px; border-radius: 50%; /* 圆形按钮 */ background-color: #409eff; /* Element Plus 主色调 */ color: white; font-size: 16px; display: flex; align-items: center; justify-content: center; cursor: pointer; outline: none; border: none; } </style> ``` #### 将组件引入到 App.vue 为了让这个浮动按钮在整个应用中都可用,可以在 `App.vue` 中注册它,并确保其始终渲染。 ```vue <template> <div id="app"> <router-view /> <FloatingButton /> <!-- 注册浮动按钮组件 --> </div> </template> <script lang="ts"> import { defineComponent } from 'vue'; import FloatingButton from './components/FloatingButton.vue'; // 引入浮动按钮组件 export default defineComponent({ components: { FloatingButton, }, }); </script> ``` 上述方法基于引用描述[^3]实现了将按钮固定在页面右下角的功能。同时结合 TypeScript 和 Vue 的最佳实践来增强项目的可维护性和扩展性。 --- ### 功能扩展说明 如果需要进一步提升用户体验,还可以加入如下特性: - **拖拽功能**:通过监听鼠标事件(mousedown、mousemove、mouseup),允许用户调整按钮位置[^1]。 - **动态显示/隐藏**:根据某些条件控制按钮的显示状态,例如路由变化时自动隐藏或显示[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值