当前位置:首页 > VUE

vue实现悬浮列表

2026-03-08 03:54:25VUE

Vue 实现悬浮列表的方法

使用 CSS 固定定位

通过 CSS 的 position: fixed 属性实现悬浮效果。适用于需要固定在页面某个位置的列表。

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

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' },
        { id: 3, text: 'Item 3' }
      ]
    }
  }
}
</script>

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

使用 Vue 指令实现拖拽悬浮

通过自定义指令实现拖拽功能,使列表可以自由移动。

vue实现悬浮列表

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

<script>
export default {
  directives: {
    draggable: {
      mounted(el) {
        el.style.position = 'fixed';
        el.style.cursor = 'move';

        let offsetX, offsetY;

        el.addEventListener('mousedown', (e) => {
          offsetX = e.clientX - el.getBoundingClientRect().left;
          offsetY = e.clientY - el.getBoundingClientRect().top;

          document.addEventListener('mousemove', move);
          document.addEventListener('mouseup', stop);
        });

        function move(e) {
          el.style.left = `${e.clientX - offsetX}px`;
          el.style.top = `${e.clientY - offsetY}px`;
        }

        function stop() {
          document.removeEventListener('mousemove', move);
          document.removeEventListener('mouseup', stop);
        }
      }
    }
  },
  data() {
    return {
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' },
        { id: 3, text: 'Item 3' }
      ]
    }
  }
}
</script>

<style>
.floating-list {
  background: white;
  padding: 10px;
  border-radius: 5px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
</style>

使用第三方库实现高级悬浮

借助第三方库如 vuedraggable 实现更复杂的拖拽和悬浮功能。

安装库:

vue实现悬浮列表

npm install vuedraggable

使用示例:

<template>
  <draggable v-model="items" class="floating-list">
    <template #item="{ element }">
      <li>{{ element.text }}</li>
    </template>
  </draggable>
</template>

<script>
import draggable from 'vuedraggable';

export default {
  components: { draggable },
  data() {
    return {
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' },
        { id: 3, text: 'Item 3' }
      ]
    }
  }
}
</script>

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

响应式悬浮列表

根据滚动位置动态显示或隐藏悬浮列表。

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

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

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

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

相关文章

vue 绑定实现

vue 绑定实现

Vue 绑定实现 Vue 提供了多种数据绑定的方式,包括文本插值、属性绑定、事件绑定、双向绑定等。以下是常见的绑定实现方法: 文本插值 使用双大括号 {{ }} 进行文本插值,将数据动态渲染到 DO…

vue实现发帖

vue实现发帖

使用 Vue 实现发帖功能 前端实现 创建发帖表单组件,包含标题和内容输入框,以及提交按钮。使用 v-model 绑定表单数据。 <template> <div>…

vue实现抽屉

vue实现抽屉

Vue 实现抽屉组件 抽屉组件是一种常见的 UI 模式,通常用于从屏幕边缘滑出内容。以下是几种实现抽屉的方法: 使用 Vue 原生实现 创建一个基本的抽屉组件,利用 Vue 的过渡和条件渲染功能。…

vue实现伸缩

vue实现伸缩

Vue实现伸缩功能的方法 使用CSS过渡和Vue的v-if或v-show 通过CSS的transition属性结合Vue的条件渲染指令(v-if或v-show)可以实现元素的伸缩效果。定义一个CSS类…

vue实现梯形

vue实现梯形

在Vue中实现梯形效果,可以通过CSS的transform属性或clip-path属性来实现。以下是几种常见的实现方法: 使用CSS transform实现梯形 通过CSS的transform: s…

vue排他思想实现

vue排他思想实现

Vue 排他思想实现 排他思想在 Vue 中通常指多个元素中只能有一个被选中或激活的状态。可以通过数据驱动和条件渲染实现。 使用 v-model 和计算属性 定义一个数据属性存储当前选中项的索引或标…