VUE实现图片左右滚动

1、HTML

<el-row :gutter="20">
    <el-col :xs="24" :sm="24" :md="24">
        <div class="jxIndexFrame">
            <span class="jxIndexTitle">荣誉资质</span>
            <div class="certificateModel">
                <el-col :xs="2" :sm="2" :md="1" class="text-right certificateArrow" style="margin-left: 5px">
                    <i class="el-icon-d-arrow-left" @click="prevCenterHonorCertificate"></i>
                </el-col>
                <el-col :xs="20" :sm="20" :md="22">
                    <div class="certificateFrom">
                        <div v-bind:style="{ width: 278 * certificateList.length + 'px' ,transition: 'transform 1s linear', transform:'translateX(' + certificateWidth + 'px)'}">
                            <div v-for="(item, index) in certificateList" :key="index">
                                <div class="certificateImg">
                                    <img :src="item.certificatesImg" alt="">
                                    <span>{{item.certificatesName}}</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </el-col>
                <el-col :xs="2" :sm="2" :md="1" class="text-center certificateArrow" style="margin-left: -10px">
                    <i class="el-icon-d-arrow-right" @click="nextCenterHonorCertificate(certificateList.length, 6)"></i>
                </el-col>
            </div>
        </div>
    </el-col>
</el-row>

2、JS

data () {
    return {
        certificateList: [],
        certificateWidth: 0
    }
},
methods: {
// 获取证书列表
getHonorBookList () {
  this.$http.get(`/help/jxhonor/getAllHonorList`, {
    params: {}
  }).then(({ data: res }) => {
    this.certificateList = res.data
  }).catch(() => {
  })
},
/**
* 证书点击上一个
*/
 prevCenterHonorCertificate () {
   this.certificateWidth = this.certificateWidth + 278
   if (this.certificateWidth > 0) {
     this.certificateWidth = 0
   }
 },
 /**
  * 证书点击下一个
  */
 nextCenterHonorCertificate (length, lengthNum) {
   var xNumber = -278
   this.certificateWidth = this.certificateWidth + xNumber
   if (this.certificateWidth <= ((length - lengthNum) * xNumber)) {
     this.certificateWidth = ((length - lengthNum) * xNumber)
   }
 }
}

3、CSS

@media (min-width: 320px) and (max-width: 767px) {
    // 荣誉资质
  .certificateModel{
    padding-top: 30px;
  }
  .certificateModel .el-col-xs-2{
    padding-left: 0 !important;
    padding-right: 0 !important;
  }
  .certificateModel .el-col-xs-20{
    padding-left: 6px !important;
  }
  .certificateFrom{
    overflow: hidden;
    width: 270px;
  }
  .certificateImg{
    overflow: hidden;
    width: 278px;
    height: 216px;
    float: left;
  }
  .certificateImg img{
    display: block;
    width: 260px;
    height: 180px;
    margin: 0 9px;
  }
  .certificateImg span{
    display: block;
    text-align: center;
    padding: 15px 0;
    font-size: 16px;
  }
  .certificateArrow i{
    font-size: 24px;
    color: #73d19e;
    font-weight: bold;
    padding: 70px 0;
    cursor: pointer;
  }
}


@media (min-width: 768px) and (max-width: 1920px) {
    // 荣誉资质
  .certificateModel{
    margin: 90px 0 50px 0;
    overflow: hidden;
  }
  .certificateFrom{
    overflow: hidden;
  }
  .certificateImg{
    overflow: hidden;
    width: 278px;
    height: 220px;
    float: left;
  }
  .certificateImg img{
    display: block;
    width: 260px;
    height: 180px;
    margin: 0 9px;
  }
  .certificateImg span{
    font-size: 16px;
    text-align: center;
    display: block;
    padding-top: 15px;
    line-height: 28px;
  }
  .certificateArrow i{
    font-size: 36px;
    color: #73d19e;
    font-weight: bold;
    padding: 70px 0;
    cursor: pointer;
  }
}
### 实现基于Vue.js横向图片轮播滚动效果 为了创建一个高效的横向图片轮播组件,在 Vue.js 中可以利用 `v-for` 指令来渲染一组图片,并通过 CSS 和 JavaScript 控制其水平滑动行为。考虑到用户体验和性能优化,推荐采用轻量级库辅助实现平滑过渡动画。 #### 使用原生Vue.js构建基础结构 首先定义基本模板布局: ```html <template> <div class="carousel-container"> <ul ref="scrollWrapper" class="image-list"> <li v-for="(item, index) in images" :key="index" class="image-item"> <img :src="item.src" alt="" /> </li> </ul> <!-- 左右控制按钮 --> <button @click="prevSlide">❮</button> <button @click="nextSlide">❯</button> </div> </template> <script> export default { data() { return { currentIndex: 0, images: [ { src: 'path/to/image1.jpg' }, { src: 'path/to/image2.jpg' }, // 更多图像... ] }; }, methods: { prevSlide() { this.currentIndex = (this.currentIndex - 1 + this.images.length) % this.images.length; this.updateScrollPosition(); }, nextSlide() { this.currentIndex = (this.currentIndex + 1) % this.images.length; this.updateScrollPosition(); }, updateScrollPosition() { const itemWidth = this.$refs.scrollWrapper.offsetWidth / this.images.length; this.$refs.scrollWrapper.style.transform = `translateX(-${this.currentIndex * itemWidth}px)`; } }, mounted() { this.updateScrollPosition(); // 初始化位置 } }; </script> <style scoped> .carousel-container { overflow: hidden; } .image-list { display: flex; transition: transform 0.3s ease-in-out; } .image-item img { width: 100%; height: auto; } /* 隐藏超出容器部分 */ .image-list li { list-style-type: none; min-width: 100%; /* 占满整个父元素宽度 */ } </style> ``` 此方法实现了简单的手动切换功能[^1]。对于更复杂的需求如自动播放、触摸手势支持等功能,则建议引入专门用于处理滚动条逻辑的第三方插件以简化开发过程并提高兼容性和稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值