当前位置:首页 > VUE

vue实现左右悬浮

2026-03-29 18:47:09VUE

实现左右悬浮布局

使用Vue实现左右悬浮布局可以通过CSS的float属性或Flexbox布局来完成。以下是两种常见方法:

使用Float实现

<template>
  <div class="container">
    <div class="left">左侧内容</div>
    <div class="right">右侧内容</div>
  </div>
</template>

<style>
.container {
  overflow: hidden; /* 清除浮动 */
}
.left {
  float: left;
  width: 200px;
  background: #f0f0f0;
}
.right {
  float: right;
  width: 200px;
  background: #e0e0e0;
}
</style>

使用Flexbox实现

<template>
  <div class="container">
    <div class="left">左侧内容</div>
    <div class="right">右侧内容</div>
  </div>
</template>

<style>
.container {
  display: flex;
  justify-content: space-between;
}
.left, .right {
  width: 200px;
}
.left {
  background: #f0f0f0;
}
.right {
  background: #e0e0e0;
}
</style>

实现悬浮按钮

如果需要实现悬浮在页面左右两侧的按钮(不随页面滚动而移动),可以使用固定定位:

<template>
  <div>
    <div class="float-left">左悬浮按钮</div>
    <div class="float-right">右悬浮按钮</div>
  </div>
</template>

<style>
.float-left {
  position: fixed;
  left: 20px;
  bottom: 20px;
  width: 50px;
  height: 50px;
  background: #42b983;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  cursor: pointer;
}

.float-right {
  position: fixed;
  right: 20px;
  bottom: 20px;
  width: 50px;
  height: 50px;
  background: #42b983;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  cursor: pointer;
}
</style>

响应式处理

为了在不同屏幕尺寸下保持良好的显示效果,可以添加媒体查询:

vue实现左右悬浮

@media (max-width: 768px) {
  .float-left, .float-right {
    width: 40px;
    height: 40px;
    font-size: 12px;
  }
}

以上方法可以根据实际需求选择使用,Float适合简单的左右布局,Flexbox提供了更灵活的布局选项,而固定定位则适合创建始终可见的悬浮元素。

标签: vue
分享给朋友:

相关文章

vue实现防抖节流

vue实现防抖节流

Vue 中实现防抖(Debounce) 防抖的核心是延迟执行函数,在连续触发时取消前一次的调用,只执行最后一次。适用于输入框搜索、窗口大小调整等场景。 方法1:使用 Lodash 的 _.debou…

vue路由实现内部切换

vue路由实现内部切换

Vue 路由实现内部切换的方法 Vue Router 是 Vue.js 官方的路由管理器,用于实现单页应用(SPA)的页面切换。以下是几种常见的内部路由切换方式: 使用 <router-lin…

vue 实现排序

vue 实现排序

Vue 实现排序的方法 使用计算属性实现排序 通过计算属性对数组进行排序,可以保持原始数据不变。示例代码展示了如何对列表按名称升序排序: <template> <div>…

vue 实现 confirm

vue 实现 confirm

实现确认对话框的方法 在Vue中实现确认对话框可以通过多种方式完成,包括使用内置组件、第三方库或自定义组件。 使用浏览器原生confirm 最简单的实现方式是直接调用浏览器原生的confirm方法。…

vue实现模糊

vue实现模糊

Vue实现模糊搜索的方法 在Vue中实现模糊搜索功能通常需要结合输入框和列表渲染,通过监听输入内容动态过滤数据。以下是几种常见实现方式: 使用计算属性实现 计算属性适合处理需要响应式更新的搜索逻辑:…

vue实现分站

vue实现分站

Vue 实现分站的方案 在 Vue 中实现分站功能通常涉及路由配置、环境变量管理和动态加载资源。以下是几种常见的实现方式: 基于路由的分站实现 通过 Vue Router 配置多路由,区分不同站点的…