当前位置:首页 > VUE

vue实现悬浮列表

2026-03-28 14:21:36VUE

实现悬浮列表的方法

使用 CSS 定位

在 Vue 中实现悬浮列表可以通过 CSS 的 position: fixedposition: sticky 属性来控制元素的位置。

vue实现悬浮列表

<template>
  <div class="floating-list">
    <ul>
      <li v-for="item in items" :key="item.id">{{ item.text }}</li>
    </ul>
  </div>
</template>

<style>
.floating-list {
  position: fixed;
  top: 20px;
  right: 20px;
  background: white;
  border: 1px solid #ddd;
  padding: 10px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
</style>

动态控制悬浮状态

可以通过 Vue 的 v-showv-if 指令动态控制悬浮列表的显示和隐藏。

vue实现悬浮列表

<template>
  <button @click="toggleList">显示/隐藏列表</button>
  <div class="floating-list" v-show="isVisible">
    <ul>
      <li v-for="item in items" :key="item.id">{{ item.text }}</li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isVisible: false,
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' }
      ]
    }
  },
  methods: {
    toggleList() {
      this.isVisible = !this.isVisible
    }
  }
}
</script>

响应式悬浮

结合 Vue 的响应式特性,可以根据滚动位置动态调整悬浮列表的样式。

<template>
  <div class="floating-list" :class="{ 'scrolled': isScrolled }">
    <ul>
      <li v-for="item in items" :key="item.id">{{ item.text }}</li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isScrolled: false,
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' }
      ]
    }
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll)
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll)
  },
  methods: {
    handleScroll() {
      this.isScrolled = window.scrollY > 100
    }
  }
}
</script>

<style>
.floating-list {
  position: fixed;
  top: 20px;
  right: 20px;
  transition: all 0.3s;
}
.floating-list.scrolled {
  background: #f5f5f5;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}
</style>

拖拽悬浮列表

通过第三方库如 vuedraggable 实现可拖拽的悬浮列表。

<template>
  <div class="floating-list">
    <draggable v-model="items" @end="onDragEnd">
      <div v-for="item in items" :key="item.id">{{ item.text }}</div>
    </draggable>
  </div>
</template>

<script>
import draggable from 'vuedraggable'
export default {
  components: { draggable },
  data() {
    return {
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' }
      ]
    }
  },
  methods: {
    onDragEnd() {
      console.log('拖拽结束')
    }
  }
}
</script>

标签: 列表vue
分享给朋友:

相关文章

vue前端实现登录

vue前端实现登录

实现登录功能的基本流程 使用Vue实现登录功能需要结合前端框架和后端API交互。以下是一个典型的实现方案: 安装必要依赖 确保项目已安装axios用于HTTP请求: npm install axi…

vue element实现

vue element实现

Vue Element UI 实现 Vue Element UI 是基于 Vue.js 的组件库,提供丰富的 UI 组件,适合快速开发企业级中后台产品。以下是一些关键实现方法和步骤: 安装 Vue…

vue实现sso

vue实现sso

Vue 实现 SSO(单点登录)方案 单点登录(SSO)允许用户通过一次登录访问多个相关系统。以下是基于 Vue 的实现方案: 使用 OAuth2/OpenID Connect 协议 OAuth2…

vue实现管道

vue实现管道

Vue 中实现管道(Pipe)功能 在 Vue 中可以通过过滤器(Filters)或计算属性(Computed Properties)实现类似管道的功能,将数据经过多个处理步骤后输出。 使用过滤器(…

vue实现全景

vue实现全景

Vue 实现全景效果的方法 使用 Vue 实现全景效果通常需要结合第三方库或框架,以下是几种常见的方法: 使用 Three.js 和 Vue-Three.js Three.js 是一个强大的 3D…

vue实现骰子

vue实现骰子

Vue 实现骰子功能 创建 Vue 组件 在 Vue 项目中创建一个骰子组件,例如 Dice.vue。该组件包含骰子的外观和逻辑。 <template> <div class=…