当前位置:首页 > VUE

vue实现悬浮列表

2026-03-28 14:21:36VUE

实现悬浮列表的方法

使用 CSS 定位

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

<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 指令动态控制悬浮列表的显示和隐藏。

<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 实现可拖拽的悬浮列表。

vue实现悬浮列表

<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 实现菜单的方法 使用 Vue 实现菜单可以通过多种方式,以下是几种常见的方法: 使用 Vue Router 实现动态路由菜单 通过 Vue Router 可以动态生成菜单,根据路由配置自动渲…

vue代理实现

vue代理实现

Vue 代理实现方法 在 Vue 项目中,配置代理可以解决开发环境下的跨域问题,通常通过修改 vue.config.js 文件实现。以下是几种常见的代理配置方式: 基础代理配置 在 vue.conf…

vue实现ppt

vue实现ppt

Vue 实现 PPT 的功能 Vue 可以结合第三方库或自定义组件实现类似 PPT 的演示效果。以下是几种常见的方法: 使用第三方库 reveal.js reveal.js 是一个流行的演示框架,支…

vue实现curd

vue实现curd

Vue实现CRUD操作 初始化Vue项目 使用Vue CLI创建新项目,安装必要依赖如axios用于HTTP请求 vue create crud-demo cd crud-demo npm inst…

vue实现分步

vue实现分步

Vue 实现分步功能 在 Vue 中实现分步功能可以通过多种方式完成,以下是几种常见的方法: 使用动态组件 通过动态组件切换不同的步骤内容,结合 v-if 或 component 标签实现分步逻辑。…

vue 实现 confirm

vue 实现 confirm

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