当前位置:首页 > VUE

vue 实现货架效果

2026-01-20 16:08:05VUE

实现货架效果的基本思路

货架效果通常指在网页中展示商品或物品的网格布局,可能包含拖拽排序、分类筛选、动画交互等功能。Vue.js 结合第三方库(如 vue-draggable 或 CSS Grid)可以高效实现此类需求。

使用 CSS Grid 布局

通过 CSS Grid 实现基础的网格布局,适用于静态货架展示:

<template>
  <div class="shelf-container">
    <div v-for="(item, index) in items" :key="index" class="shelf-item">
      <img :src="item.image" :alt="item.name">
      <p>{{ item.name }}</p>
    </div>
  </div>
</template>

<style scoped>
.shelf-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
  padding: 20px;
}
.shelf-item {
  border: 1px solid #ddd;
  padding: 10px;
  text-align: center;
}
</style>

拖拽排序功能

集成 vue-draggable-next(Vue 3)实现拖拽交互:

npm install vue-draggable-next

组件实现:

vue 实现货架效果

<template>
  <draggable 
    v-model="items" 
    item-key="id" 
    class="shelf-container"
    @end="onDragEnd">
    <template #item="{element}">
      <div class="shelf-item">
        <img :src="element.image" :alt="element.name">
        <p>{{ element.name }}</p>
      </div>
    </template>
  </draggable>
</template>

<script setup>
import { ref } from 'vue';
import draggable from 'vue-draggable-next';

const items = ref([
  { id: 1, name: '商品A', image: 'url1' },
  { id: 2, name: '商品B', image: 'url2' }
]);

const onDragEnd = () => {
  console.log('当前顺序:', items.value);
};
</script>

动态筛选分类

添加分类筛选功能,结合计算属性:

<template>
  <div>
    <select v-model="selectedCategory">
      <option value="all">全部</option>
      <option v-for="cat in categories" :value="cat">{{ cat }}</option>
    </select>
    <div class="shelf-container">
      <div v-for="item in filteredItems" :key="item.id" class="shelf-item">
        <img :src="item.image" :alt="item.name">
        <p>{{ item.name }}</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';

const selectedCategory = ref('all');
const categories = ['食品', '电子', '服饰'];
const items = ref([
  { id: 1, name: '商品A', category: '食品', image: 'url1' }
]);

const filteredItems = computed(() => {
  return selectedCategory.value === 'all' 
    ? items.value 
    : items.value.filter(item => item.category === selectedCategory.value);
});
</script>

动画过渡效果

通过 Vue 的 <transition-group> 实现布局变化时的动画:

vue 实现货架效果

<template>
  <transition-group name="shelf" tag="div" class="shelf-container">
    <div v-for="item in items" :key="item.id" class="shelf-item">
      <img :src="item.image" :alt="item.name">
      <p>{{ item.name }}</p>
    </div>
  </transition-group>
</template>

<style scoped>
.shelf-move {
  transition: transform 0.5s ease;
}
</style>

响应式设计建议

通过媒体查询调整货架列数:

@media (max-width: 768px) {
  .shelf-container {
    grid-template-columns: repeat(2, 1fr);
  }
}
@media (max-width: 480px) {
  .shelf-container {
    grid-template-columns: 1fr;
  }
}

性能优化

对于大型数据集,建议使用虚拟滚动库如 vue-virtual-scroller

npm install vue-virtual-scroller

示例实现:

<template>
  <RecycleScroller 
    class="scroller" 
    :items="items" 
    :item-size="200"
    key-field="id">
    <template #default="{ item }">
      <div class="shelf-item">
        <img :src="item.image" :alt="item.name">
        <p>{{ item.name }}</p>
      </div>
    </template>
  </RecycleScroller>
</template>

标签: 货架效果
分享给朋友:

相关文章

vue网页实现拖动效果

vue网页实现拖动效果

Vue 实现拖动效果的方法 使用 HTML5 拖放 API HTML5 提供了原生的拖放 API,可以通过 draggable 属性和相关事件实现拖动效果。 <template> &…

vue实现吸附效果

vue实现吸附效果

Vue实现吸附效果的方法 吸附效果通常指页面滚动时,某个元素固定在特定位置(如顶部或底部)。以下是几种实现方式: 使用CSS的position: sticky 通过CSS的sticky定位实现吸附效…

vue实现效果

vue实现效果

Vue 实现常见效果的方法 数据绑定与响应式更新 Vue 的核心特性是数据绑定,通过 v-model 实现表单元素与数据的双向绑定。例如输入框与数据的实时同步: <input v-model…

js实现图片滚动效果

js实现图片滚动效果

使用CSS动画实现图片滚动 通过CSS的@keyframes和animation属性实现无缝滚动效果。将图片容器设置为横向排列,通过动画平移位置。 <style> .scroll-…

jquery 效果

jquery 效果

jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。jQuery 提供了丰富的效果(Effects)功能,可以轻松实现元素的动态显…

jquery效果

jquery效果

jQuery 动画效果 jQuery 提供了多种内置动画效果,可用于实现平滑的页面交互。常用的方法包括 show()、hide()、toggle()、fadeIn()、fadeOut()、slideU…