自动滚动组件

11 阅读1分钟

image.png

<template>
  <div class="page">
    <div class="scroll-view" ref="scrollViewRef">

      <div class="complain-list" ref="listRef"  v-for="(p, n) in 2" :key="n">
        <div class="complain-bg" v-for="(item, index) in data" :key="index">
          <div class="complain-col2">{{item.projectType}}</div>
          <div class="complain-col2">{{item.countType}}</div>
        </div>

      </div>
    </div>
  </div>
</template>
<script setup>
import {ref, onMounted, onBeforeUnmount, nextTick} from "vue";

const props = defineProps({
  data:{
    type:Array,
    default:()=>{
      return []
    }
  }
})
const listRef = ref();
const scrollViewRef = ref();

let intervalId = null;

// 滚动函数:模拟上下滚动效果
const autoScrolling = () => {
  intervalId = setInterval(() => {
    if (scrollViewRef.value.scrollTop < listRef.value[0].clientHeight) {
      scrollViewRef.value.scrollTop += 1;
    } else {
      scrollViewRef.value.scrollTop = 0;
    }
  }, 40);
};

onMounted(() => {
  nextTick(() => {
    autoScrolling(); // 开始自动滚动
  });
});

onBeforeUnmount(() => {
  if (intervalId) {
    clearInterval(intervalId);
  }
});
</script>
<style scoped lang="scss">
.page {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.scroll-view {
  width: 100%;
  height: 100%;
  overflow-y: hidden; /* 隐藏垂直滚动条 */
  position: relative;
}

.complain-text{
  font-size: 16px;
  color: #FFFFFF;
  line-height: 24px;
}

.complain-list {
  width: 100%;
  gap: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  transition: transform 0.5s ease-in-out;
  margin-bottom: 20px;

  .complain-bg {
    background-image: url("@/assets/images/project/projectTypeCount-bg.png");
    background-size: 100% 100%;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    width: 360px;
    height: 44px;
    box-sizing: border-box;
    padding: 0 56px 0 70px;

    .complain-col1 {
      font-size: 14px;
      color: rgba(255,255,255,0.8);
    }

    .complain-col2 {
      font-size: 16px;
      color: #FFFFFF;
    }

    .complain-col {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;

      .complain-row1 {
        font-size: 12px;
        color: rgba(255, 255, 255, 0.5);
      }

      .complain-row2 {
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: center;
        gap: 4px;
      }
    }
  }
}
</style>