vue轮播+拖动横向滚动图

横向滚动轮播,鼠标滑动拖动和点击跳转链接。
效果图:
http://www.hnbwzn.com/
在这里插入图片描述

<template>
  <a-row class="project-case">
    <a-col :span="24" class="title">
      <router-link to="/customerCase" tag="span">经典案例</router-link>
    </a-col>
    <div class="content">
      <div
        class="content-list"
        ref="contentList"
        @mousedown.prevent="contentListMouseDown"
      >
        <a-col
          class="content-item"
          :span="4"
          v-for="(item, index) in dataList"
          :key="index"
        >
          <!-- <router-link :to="item.path"> -->
            <img @mousedown.prevent="" @click="routerLink(item)" :src="item.url" alt="" />
          <!-- </router-link> -->
        </a-col>
      </div>
    </div>
  </a-row>
</template>
<script>
export default {
  name: "BottomScrollImg",
  data() {
    return {
      dataList: [
        // 开头填充
        { url: "/img-static/customer-project-case/01.png", path: "/softwareProduct?id=0" },
        { url: "/img-static/customer-project-case/02.png", path: "/softwareProduct?id=1" },
        { url: "/img-static/customer-project-case/03.png", path: "/softwareProduct?id=2" },
        { url: "/img-static/customer-project-case/04.png", path: "/caseDetail?id=0" },
        { url: "/img-static/customer-project-case/05.png", path: "/caseDetail?id=1" },
        { url: "/img-static/customer-project-case/06.png", path: "/caseDetail?id=2" },

        { url: "/img-static/customer-project-case/01.png", path: "/softwareProduct?id=0" },
        { url: "/img-static/customer-project-case/02.png", path: "/softwareProduct?id=1" },
        { url: "/img-static/customer-project-case/03.png", path: "/softwareProduct?id=2" },
        { url: "/img-static/customer-project-case/04.png", path: "/caseDetail?id=0" },
        { url: "/img-static/customer-project-case/05.png", path: "/caseDetail?id=1" },
        { url: "/img-static/customer-project-case/06.png", path: "/caseDetail?id=2" },

        // 主体数据开始
        { url: "/img-static/customer-project-case/01.png", path: "/softwareProduct?id=0" },
        { url: "/img-static/customer-project-case/02.png", path: "/softwareProduct?id=1" },
        { url: "/img-static/customer-project-case/03.png", path: "/softwareProduct?id=2" },
        { url: "/img-static/customer-project-case/04.png", path: "/caseDetail?id=0" },
        { url: "/img-static/customer-project-case/05.png", path: "/caseDetail?id=1" },
        { url: "/img-static/customer-project-case/06.png", path: "/caseDetail?id=2" },
         // 主体数据结束

        //  末尾填充
        { url: "/img-static/customer-project-case/01.png", path: "/softwareProduct?id=0" },
        { url: "/img-static/customer-project-case/02.png", path: "/softwareProduct?id=1" },
        { url: "/img-static/customer-project-case/03.png", path: "/softwareProduct?id=2" },
        { url: "/img-static/customer-project-case/04.png", path: "/caseDetail?id=0" },
        { url: "/img-static/customer-project-case/05.png", path: "/caseDetail?id=1" },
        { url: "/img-static/customer-project-case/06.png", path: "/caseDetail?id=2" },

        { url: "/img-static/customer-project-case/01.png", path: "/softwareProduct?id=0" },
        { url: "/img-static/customer-project-case/02.png", path: "/softwareProduct?id=1" },
        { url: "/img-static/customer-project-case/03.png", path: "/softwareProduct?id=2" },
        { url: "/img-static/customer-project-case/04.png", path: "/caseDetail?id=0" },
        { url: "/img-static/customer-project-case/05.png", path: "/caseDetail?id=1" },
        { url: "/img-static/customer-project-case/06.png", path: "/caseDetail?id=2" },
      ],
      timerId: "",
      count: 0,
      stopFlag: false,
      mousePosition_down: "",//鼠标点击时的位置
      contentListWidth:'',//list的宽度
      listLength:2,//要显示的图片占据的list
      moveFlag:false,//鼠标拖动时,阻止路由跳转
      // mousePosition_up: "",
    };
  },
  methods: {
    routerLink(item){
      if(this.moveFlag) return
      this.$router.push(item.path)
    },
    // 鼠标按下时,添加move 和up事件
    contentListMouseDown(e) {
      // console.log("down", e.offsetX);
      this.mousePosition_down = e.offsetX;
      this.$refs.contentList.addEventListener("mousemove", this.mouseMove);
      this.$refs.contentList.addEventListener("mouseup", this.mouseUp);
    },
    // 鼠标up时,移除move和up事件
    mouseUp() {
      setTimeout(() => {
        this.moveFlag = false
      }, 0);
      // console.log("up", e.offsetX);
      this.removeListener()
      // 拖动到列表最前面时
      if(this.count < this.contentListWidth){
        console.log('小')
        this.count = parseInt(this.count) + this.contentListWidth * this.listLength
        this.$refs.contentList.style.transform = `translateX(-${this.count}px)`;
      }
      // 拖动到列表最后面时
      if(this.count > this.contentListWidth * (this.listLength + 1)){
        console.log(this.count / this.contentListWidth)
        console.log('最大')
        this.count = parseInt(this.count) - this.listLength * this.contentListWidth
        this.$refs.contentList.style.transform = `translateX(-${this.count}px)`;
      }
    },
    // 鼠标move时,list跟随移动
    mouseMove(e) {
      this.moveFlag = true
      // console.log("move", e.offsetX);
      let mousePosition_move = e.offsetX;
      this.count = parseInt(this.mousePosition_down) - mousePosition_move + parseInt(this.count);
      // console.log(this.count);
      this.$refs.contentList.style.transform = `translateX(-${this.count}px)`;
    },
    // 移除contentlist的mousemove 和 mouseup;在鼠标up、鼠标移出此元素、时。
    removeListener(){
      this.$refs.contentList.removeEventListener("mousemove", this.mouseMove);
      this.$refs.contentList.removeEventListener("mouseup", this.mouseUp);
    }
  },
  mounted() {
    // 定时器滚动
    this.$refs.contentList.addEventListener("mouseenter", () => {
      this.stopFlag = true;
    });
    this.$refs.contentList.addEventListener("mouseleave", () => {
      this.stopFlag = false;
      // 在mouseleave时也移除move和up事件
      this.mouseUp()
    });
    this.contentListWidth = parseInt(getComputedStyle(this.$refs.contentList).width);
    this.count = parseInt(getComputedStyle(this.$refs.contentList).width);
    // console.log(this.count);
    this.timerId = setInterval(() => {
      if (this.stopFlag) return;
      this.count++;
      if (
        this.count == this.contentListWidth * this.listLength
      ) {
        this.count = this.contentListWidth
      }
      this.$refs.contentList.style.transform = `translateX(-${this.count}px)`;
    }, 15);
  },
  beforeDestroy(){
    clearInterval(this.timerId)
  }
};
</script>
<style lang="less" scoped>
.project-case {
  padding-bottom: 3rem;
  overflow: hidden;
  .title {
    font-size: 3.6rem;
    padding: 5rem 0;
    span {
      cursor: pointer;
    }
  }
  .content {
    .content-list {
      user-select: none;
      transform: translateX(-33.33%);
      display: flex;
      flex-wrap: nowrap;
      width: 100%;
      .content-item {
        padding-right: 5rem;
        img {
          transition: all 0.5s;
          width: 100%;
          cursor: pointer;
          &:hover {
            transform: scale(1.3);
          }
        }
      }
    }
  }
}
</style>
  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以使用Vue 3和Element Plus来实现一个滚动通知栏。以下是一个简单的示例: 首先,安装Element Plus和Vue Router(如果还没有安装): ```bash npm install element-plus vue-router ``` 然后,在你的Vue文件中,引入所需的组件和样式: ```vue <template> <div class="scroll-notice"> <el-scrollbar ref="scrollbar" :wrap-style="{ height: '40px' }"> <el-scrollbar-wrap> <div class="notice-content" :style="{ top: top + 'px' }"> <!-- 这里放置通知内容 --> <div v-for="(notice, index) in notices" :key="index" class="notice-item"> {{ notice }} </div> </div> </el-scrollbar-wrap> </el-scrollbar> </div> </template> <script> import { ref, reactive, watch } from 'vue'; export default { name: 'ScrollNotice', data() { return { top: 0, notices: [ '通知1', '通知2', '通知3', // 更多通知... ], }; }, mounted() { this.startScroll(); }, methods: { startScroll() { const scrollHeight = this.$refs.scrollbar.$el.clientHeight; const contentHeight = this.$refs.scrollbar.scrollContent.offsetHeight; if (contentHeight > scrollHeight) { // 开始滚动 const updateTop = () => { this.top -= 1; if (this.top <= -contentHeight) { this.top = scrollHeight; } }; this.timer = setInterval(updateTop, 20); } }, }, beforeUnmount() { clearInterval(this.timer); }, }; </script> <style> .scroll-notice { width: 100%; height: 40px; } .notice-content { position: relative; } .notice-item { line-height: 40px; } </style> ``` 在上面的示例中,我们使用了`el-scrollbar`组件来创建一个带有滚动条的容器。通知内容放置在一个具有绝对定位的容器内,并通过改变容器的`top`值来实现滚动效果。在`mounted`钩子中,我们调用`startScroll`方法来启动滚动。 当通知内容的高度大于容器的高度时,我们会通过定时器不断更新`top`值,从而实现滚动效果。当滚动到底部时,我们将`top`值重置为容器的高度,以实现循环滚动。 请注意,这只是一个简单的示例,你可以根据实际需求进行修改和扩展。你可能需要根据具体情况进行样式调整和数据绑定。 希望能对你有所帮助!
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值