当前位置:首页 > 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 模板的基本结构 Vue 模板基于 HTML 语法,通过 Vue 的指令和插值实现动态渲染。以下是一个基础模板示例: <template> <div id="app"…

vue 指令实现

vue 指令实现

Vue 指令实现 Vue 指令是 Vue.js 提供的特殊属性,用于在 DOM 元素上添加特殊行为。指令以 v- 前缀开头,例如 v-model、v-if、v-for 等。以下是实现自定义指令和常用内…

vue 实现过滤

vue 实现过滤

Vue 实现过滤的方法 在 Vue 中实现过滤功能可以通过多种方式完成,包括使用计算属性、自定义过滤器、第三方库等。以下是几种常见的实现方法: 使用计算属性进行过滤 计算属性是 Vue 中最常用的过…

vue实现图片

vue实现图片

Vue 实现图片功能的方法 静态图片引入 在 Vue 项目中,可以通过 src 属性直接引入静态图片。图片通常放在 public 或 assets 目录下。 <template> &…

vue实现逻辑

vue实现逻辑

Vue 实现逻辑的核心概念 Vue.js 是一个渐进式 JavaScript 框架,其核心逻辑围绕数据驱动和组件化开发。通过响应式系统、虚拟 DOM 和组件生命周期等机制,Vue 实现了高效的前端开发…

vue实现selectable

vue实现selectable

Vue 实现可选择的 Select 组件 在 Vue 中实现一个可选择的 Select 组件,可以通过以下几种方式: 使用原生 HTML select 元素 <template> &…