vue2.0 typescript requestAniframe 实现无缝滚动的图片列表

游走在城市的鱼IP属地: 湖北
0.102字数 31
<template>
  <section-container class="home-mineral-3d" title="无缝滚动">
    <span class="minerals-list-arrow__left">
      <i class="el-icon-arrow-left"></i>
    </span>
    <span class="minerals-list-arrow__right">
      <i class="el-icon-arrow-right"></i>
    </span>
    <div class="minerals-list" @mouseenter="animation = false" @mouseleave="animation = true">
      <div
        class="minerals-wrapper"
        :style="{
          '--translateX1': translateX1 + 'px',
          '--translateX2': translateX2 + 'px'
        }"
      >
        <div ref="imageList1" class="image-list1">
          <image-show v-for="(item, index) in 4" align="left" :title="'图片标题' + index" subTitle="副标题" :key="index" class="mr-12" width="189" height="182" @click="handleClick" />
        </div>
        <div ref="imageList2" class="image-list2">
          <image-show v-for="(item, index) in 4" align="left" :title="'图片标题' + index + 101" subTitle="副标题" :key="index + 101" class="mr-12" width="189" height="182" @click="handleClick" />
        </div>
      </div>
    </div>
  </section-container>
</template>

<script lang="ts">
import { Vue, Component, Ref } from 'vue-property-decorator'
import ImageShow from '@/components/image-show.vue' // ImageShow组件改成image元素即可,需要设置图片宽高
import SectionContainer from '@/components/section-container.vue' // SectionContainer组件改成div元素即可
@Component({
  components: { ImageShow, SectionContainer }
})
export default class Mineral3d extends Vue {
  private translateX1: number = 0
  private translateX2: number = 0
  @Ref('imageList1') readonly imageList1!: HTMLDivElement
  @Ref('imageList2') readonly imageList2!: HTMLDivElement
  private animation: boolean = true
  private listWidth: number = 0

  private mounted() {
    this.listWidth = this.imageList1.clientWidth
    this.doAnimation()
  }

  private doAnimation() {
    const loop = () => {
      if (this.animation) {
        // 控制第一个列表imageList1滚动轨迹
        this.translateX1 -= 1
        if (this.translateX1 <= -this.listWidth) {
          this.translateX1 = this.listWidth
        }
        // 控制第二个列表imageList2滚动轨迹
        this.translateX2 -= 1
        if (this.translateX2 <= -this.listWidth * 2) {
          this.translateX2 = 0
        }
      }
      window.requestAnimationFrame(loop)
    }
    window.requestAnimationFrame(loop)
  }

  private handleClick(value: string) {
    console.log(value)
  }
}
</script>

<style lang="scss" scoped>
.home-mineral-3d {
  position: relative;
  .minerals-list-arrow__left,
  .minerals-list-arrow__right {
    display: inline-block;
    height: 30px;
    width: 30px;
    line-height: 30px;
    text-align: center;
    background: rgba(0, 0, 0, 0.1);
    border-radius: 50%;
    position: absolute;
    top: 50%;
    left: 5px;
  }
  .minerals-list-arrow__right {
    left: unset;
    right: 5px;
  }
  .minerals-list {
    overflow-x: auto;
    margin: 0 20px;
    position: relative;
    /* 隐藏滚动条 */
    &::-webkit-scrollbar {
      display: none;
      /*background-color:transparent;*/
    }

    .minerals-wrapper {
      display: flex;
      .image-list1 {
        display: inline-flex;
        transform: translateX(var(--translateX1));
      }
      .image-list2 {
        display: inline-flex;
        transform: translateX(var(--translateX2));
      }
    }
  }
}
</style>

利用两个一样的列表的位置移动变换实现无缝滚动,结合requestAniframe优化性能。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
1人点赞
总资产0.837共写了2950字获得14个赞共1个粉丝