h5跑马灯效果

无缝滚动效果 ,外加 手指的移动效果

思路:

1.无缝滚动1个内容区域,复制一下这个内容,追加到改内容的后面,形成无缝滚动的效果

2.手指左滑右滑进行x轴方向的加减

代码如下:

<template>
  <div class="scroll-container" ref="container">
    <div
      class="scroll-content"
      ref="content"
      @touchstart.stop="handlerTouchStart"
      @touchmove.stop="handlerTouchMove"
      @touchend.stop="handlerTouchEnd"
    >
      <!-- 滚动内容放在这里 -->
      <div class="list">
        <div v-for="(item, index) in list" :key="index">
          <img :src="item" alt="" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import a from "../assets/imgs/1.jpg";
import b from "../assets/imgs/2.jpg";
import c from "../assets/imgs/3.jpg";
import d from "../assets/imgs/4.jpg";
import e from "../assets/imgs/5.jpg";
import f from "../assets/imgs/6.jpg";
import g from "../assets/imgs/7.jpg";
import h from "../assets/imgs/8.jpg";
import i from "../assets/imgs/9.jpg";
import j from "../assets/imgs/10.jpg";

export default {
  data() {
    return {
      list: [a, b, c, d, e, f, g, h, i, j],
      startLocation: 0, //手指起始位置
      step: 2, //移动步长
      offsetX: 0, //水平方向移动的距离
      timerid: null, //任务id
      moveDistance: 0, //手指滑动的距离
      contentWidth: 0 //一个容器的宽度
    };
  },

  mounted() {
    this.$nextTick(() => {
      // 获取滚动内容元素和容器元素
      const content = this.$refs.content;
      const container = this.$refs.container;

      // 获取滚动内容的宽度
      this.contentWidth = content.offsetWidth;

      // 复制滚动内容并追加到原来的内容后面
      const clone = content.cloneNode(true);
      content.parentNode.appendChild(clone);

      // 设置滚动内容的总宽度,使其可以无限循环滚动
      container.style.width = this.contentWidth * 2 + "px";
      // 开启动画
      this.startAnimation();
    });
  },
  methods: {
    startAnimation() {
      this.offsetX += this.step;
      //   if (this.offsetX <= 0 || this.offsetX >= this.contentWidth) {
      //     this.offsetX = 0;
      //   }
      //   if (this.offsetX % this.contentWidth == 0) this.offsetX = 0;
      this.limitX();
      this.updateAnimate(this.offsetX);
      this.timerid = requestAnimationFrame(this.startAnimation);
    },
    updateAnimate(x) {
      this.$refs.container.setAttribute(
        "style",
        `width:${2 * this.contentWidth}px;transform: translateX(${-x}px)`
      );
    },
    handlerTouchStart(e) {
      cancelAnimationFrame(this.timerid);
      this.startLocation = e.touches[0].pageX;
    },
    handlerTouchMove(e) {
      const current = e.touches[0].pageX;
      //   手指滑动距离
      this.moveDistance = Math.floor(e.touches[0].pageX - this.startLocation);
      if (current > this.startLocation) {
        // 往右划
        this.offsetX -= Math.abs(this.moveDistance);
      } else {
        // 往左滑
        this.offsetX += Math.abs(this.moveDistance);
      }
      //   判断边界距离
      this.limitX();
      //   if (this.offsetX <= 0 || this.offsetX >= 1000) {
      //     this.offsetX = 0;
      //   }
      //   if (this.offsetX % this.contentWidth == 0) this.offsetX = 0;
      this.updateAnimate(this.offsetX);
    },
    handlerTouchEnd() {
      this.startAnimation();
    },
    limitX() {
      if (this.offsetX <= 0 || this.offsetX >= this.contentWidth) {
        this.offsetX = 0;
      }
      if (this.offsetX % this.contentWidth == 0) this.offsetX = 0;
    }
  }
};
</script>
<style scoped lang="less">
.list {
  height: 100px;
  //   background: papayawhip;
  display: flex;
  div {
    width: 100px;
    height: 100px;
    img {
      width: 100%;
      height: 100%;
    }
  }
}
.scroll-container {
  overflow-x: scroll;
  white-space: nowrap;
}

.scroll-content {
  display: inline-block;
}
.scroll-content {
  //   animation: scroll 20s linear infinite;
}

// @keyframes scroll {
//   0% {
//     transform: translateX(0);
//   }
//   100% {
//     transform: translateX(-100%);
//   }
// }
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
H5翻书效果源码是一段用于实现网页翻书效果的代码。H5是指HTML5,是一种用于构建和展示网页的技术标准。翻书效果是指在网页中实现书本翻页的动画效果。 以下是一个简单的H5翻书效果源码示例: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> /* 设置书籍容器的样式 */ .book { width: 400px; height: 300px; perspective: 1000px; margin: 100px auto; } /* 设置书页的样式 */ .page { width: 100%; height: 100%; position: absolute; transform-style: preserve-3d; transition: transform 1s; } /* 设置左右两页的样式 */ .page-left { transform-origin: right center; } .page-right { transform-origin: left center; } /* 设置翻书动画的样式 */ .book:hover .page-left { transform: rotateY(-180deg); } .book:hover .page-right { transform: rotateY(180deg); } </style> </head> <body> <div class="book"> <div class="page page-left"> <!-- 左边页的内容 --> <h1>左页</h1> </div> <div class="page page-right"> <!-- 右边页的内容 --> <h1>右页</h1> </div> </div> </body> </html> ``` 以上代码使用HTML和CSS来实现翻书效果。通过设置书籍容器的样式和两个页的样式,利用CSS的`transform`属性和`transition`属性来实现页面的旋转动画效果。用户在鼠标悬停在书籍容器上时,左页和右页会分别翻转,实现翻书效果。 需要注意的是,以上只是一个简单的示例代码,实际使用时还需根据具体需求进行适当的修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值