简单的图片列表排序

该篇文章介绍了如何在Vue组件中使用JavaScript的slice方法对图片数组进行切片,以展示不同的图片组别,如第一组、第二组等。
摘要由CSDN通过智能技术生成
<template>
<div class='no-rule'>
  <div class="imgs ">
    <div v-for="(img, index) in first" :key="index" >
      <img :src="img" alt="">
    </div>
  </div>
  <div class="second imgs">
    <div v-for="(img, index) in second" :key="index" >
      <img :src="img" alt="">
    </div>
  </div>

  <div class="imgs third">
    <div v-for="(img, index) in third" :key="index" >
      <img :src="img" alt="">
    </div>
  </div>

  <div class="fourth imgs">
    <div v-for="(img, index) in fourth" :key="index" >
      <img :src="img" alt="">
    </div>
  </div>
  
</div>
</template>

<script>

export default {
components: {},
data() {
return {
  images: [
    'https://images.chuoooo.com/paint/20240119/cdb30dea11667122ef3476cb6e5dc38d.jpg',
    'https://images.chuoooo.com/paint/20240119/311a5391c9ba7eda104ba545edbb0611.jpg',
    'https://images.chuoooo.com/paint/20240119/14cb86974a4547e8046f7a54013b41ee.png',
    'https://images.chuoooo.com/paint/20240117/bb2f4975253c6e950fc9661ceb6fdcff.jpg',
    'https://images.chuoooo.com/paint/20240119/cdb30dea11667122ef3476cb6e5dc38d.jpg',
    'https://images.chuoooo.com/paint/20240119/311a5391c9ba7eda104ba545edbb0611.jpg',
    'https://images.chuoooo.com/paint/20240119/14cb86974a4547e8046f7a54013b41ee.png',
    'https://images.chuoooo.com/paint/20240117/bb2f4975253c6e950fc9661ceb6fdcff.jpg',
    'https://images.chuoooo.com/paint/20240119/cdb30dea11667122ef3476cb6e5dc38d.jpg',
    'https://images.chuoooo.com/paint/20240119/311a5391c9ba7eda104ba545edbb0611.jpg',
    'https://images.chuoooo.com/paint/20240119/14cb86974a4547e8046f7a54013b41ee.png',
    'https://images.chuoooo.com/paint/20240117/bb2f4975253c6e950fc9661ceb6fdcff.jpg',
    'https://images.chuoooo.com/paint/20240119/cdb30dea11667122ef3476cb6e5dc38d.jpg',
    'https://images.chuoooo.com/paint/20240119/311a5391c9ba7eda104ba545edbb0611.jpg',
    'https://images.chuoooo.com/paint/20240119/14cb86974a4547e8046f7a54013b41ee.png',
    'https://images.chuoooo.com/paint/20240117/bb2f4975253c6e950fc9661ceb6fdcff.jpg',
    'https://images.chuoooo.com/paint/20240119/cdb30dea11667122ef3476cb6e5dc38d.jpg',
    'https://images.chuoooo.com/paint/20240119/311a5391c9ba7eda104ba545edbb0611.jpg',
    'https://images.chuoooo.com/paint/20240119/14cb86974a4547e8046f7a54013b41ee.png',
    'https://images.chuoooo.com/paint/20240117/bb2f4975253c6e950fc9661ceb6fdcff.jpg',
  ],
  first: [],
  second: [],
  third: [],
  fourth: [],
};
},
computed: {},
watch: {},
methods: {
  handleSlice() {
    this.first = this.images.slice(0, 5); // 截取索引0到3的元素,即第一组图片
    this.second = this.images.slice(5, 10); // 截取索引4到7的元素,即第二组
    this.third = this.images.slice(10, 15); // 截取索引8到11的元素,即第三组
    this.fourth = this.images.slice(15); // 截取索引12到最后的所有元素,即第四组
  },
},
created() {

},

mounted() {
  this.handleSlice()
},

beforeCreate() {}, 
beforeMount() {}, 
beforeUpdate() {}, 
updated() {
}, 
beforeDestroy() {}, 
destroyed() {},
activated() {}, //如果页面有keep-alive缓存功能,这个函数会触发
}
</script>
<style scoped>
.no-rule {
  display: flex;
  flex-wrap: wrap;

}

.imgs {
    width: 25%;

  }

  img {
    width: 100%;
    overflow: hidden;
  }

  .fourth {
    background-color: skyblue;
  }
  .third {
    background-color: pink;
  }

  .second {
    background-color: red;
  }
</style>

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 以下是一个简单的 Vue.js 实现图片拖拽排序的代码示例: HTML 部分: ``` <template> <div> <ul> <li v-for="(item, index) in items" :key="index" @dragstart="dragStart(index)" @dragend="dragEnd" draggable="true"> <img :src="item" /> </li> </ul> </div> </template> ``` JS 部分: ``` <script> export default { data() { return { items: [ 'https://via.placeholder.com/100x100', 'https://via.placeholder.com/100x100', 'https://via.placeholder.com/100x100' ], currentIndex: null } }, methods: { dragStart(index) { this.currentIndex = index }, dragEnd() { this.currentIndex = null }, drop(toIndex) { const currentItem = this.items[this.currentIndex] this.items.splice(this.currentIndex, 1) this.items.splice(toIndex, 0, currentItem) } } } </script> ``` 该代码实现了一个图片列表,每个图片都可以通过拖拽进行排序。在 `dragstart` 事件中记录当前图片的索引,在 `dragend` 事件中清空当前图片的索引,在 `drop` 事件中改变图片列表的顺序。 该代码仅提供一个示例,在实际开发中可能需要根据需求进行修改和完善。 ### 回答2: 图片拖拽排序是一种常见的前端交互效果,可以让用户自由拖动图片进行排序。下面是一个用Vue实现的图片拖拽排序的代码示例: ```html <template> <div> <h2>图片拖拽排序</h2> <div class="image-list"> <div class="image-item" v-for="(image, index) in images" :key="image.id" :style="{ order: image.order }" draggable="true" @dragstart="onDragStart(index)" @dragover="onDragOver(index)" @drop="onDrop(index)" > <img :src="image.url" alt="image" /> </div> </div> </div> </template> <script> export default { data() { return { images: [ { id: 1, url: 'image1.jpg', order: 1 }, { id: 2, url: 'image2.jpg', order: 2 }, { id: 3, url: 'image3.jpg', order: 3 }, { id: 4, url: 'image4.jpg', order: 4 }, ], draggingIndex: null, dropIndex: null, }; }, methods: { onDragStart(index) { this.draggingIndex = index; }, onDragOver(index) { event.preventDefault(); this.dropIndex = index; }, onDrop() { const draggingImage = this.images[this.draggingIndex]; const dropImage = this.images[this.dropIndex]; this.images.splice(this.draggingIndex, 1); this.images.splice(this.dropIndex, 0, draggingImage); this.draggingIndex = null; this.dropIndex = null; }, }, }; </script> <style scoped> .image-list { display: flex; flex-wrap: wrap; } .image-item { width: 200px; height: 200px; margin: 10px; } img { width: 100%; height: 100%; object-fit: cover; } </style> ``` 上述代码中,images数组存储了要展示的图片信息,每个图片对象包含id、url和order属性。在组件的模板中,通过v-for指令遍历images数组并渲染图片元素。每个图片元素设置draggable="true",并监听相关的drag事件。onDragStart方法记录拖拽开始时的图片索引,onDragOver方法阻止默认行为并记录拖拽结束时的目标位置索引,onDrop方法根据目标位置索引重新排序images数组,并重置拖拽开始和结束的索引。 最终,用户可以通过拖拽图片实现排序,拖拽过程中会展示视觉上的交互效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值