js实现dom元素横向、纵向滚动的动画

10 篇文章 1 订阅

通过settimeout实现的滚动动画,支持反复点击变快

支持水平滚动和竖直滚动,快速点击会将上次未滚动完的距离叠加,滚动的时间不变,滚动的速度会变快

使用方式

1.复制下方代码;
2.导出对应的方法 movingColumn - 竖直滚动       moving--水平滚动
3.函数接收3个参数 dom: 要滑动的元素   space: 点击一次要滚动的距离  istop/isLeft 是否向上/左滚动


功能修改

const hz = 60  在规定时间分几次滚动到目标位置 60是人肉眼可识别的刷新率
每次滚动的时间为 settime里的1ms * hz  = 60ms
let timer:any = null // 定时器
let TargetLocation = -1 // 上一次点击应该滚动到的目标位置
let toltalSpace = 0 // 本次要滚动的距离

/**
 * @info 竖直滚动
 * @info 滚动动画 hz 刷新率 可以修改滚动的速度
 * @params dom:要滚动的元素; space 要滚动的距离; istop 滚动的方向;
*/
const movingColumn = (dom:HTMLDivElement, space: number, istop:boolean) => {

  // 用户快速点击 则把上次未滚动的距离与本次滚动结合再一起
  if (timer && TargetLocation !== -1) {
    toltalSpace += TargetLocation - dom.scrollTop
    // 计算本次的目标距离
    if(istop) {
      TargetLocation = dom.scrollTop + toltalSpace + space
    } else {
      TargetLocation = dom.scrollTop + toltalSpace - space
    }
  } else if (!timer) {
    toltalSpace = 0
    TargetLocation = -1
  }

  if (istop) {
    toltalSpace -= space
  } else {
    toltalSpace += space
  }

  // 获取本次的目标位置
  const position = dom.scrollTop
  TargetLocation = position + toltalSpace

  clearInterval(timer)
  timer = null
  const hz = 60
  let i = 1
  timer = setInterval(() => {
    dom.scrollTop = position + i * toltalSpace / hz
    ++i
    if (i >= hz) {
      clearInterval(timer)
      timer = null
      dom.scrollTop = TargetLocation // 位置修正
      toltalSpace = 0
      TargetLocation = -1
    }
  }, 1)
}


/**
 * @info 水平滚动
 * @info 滚动动画 hz 刷新率 可以修改滚动的速度
 * @params dom:要滚动的元素; space 要滚动的距离; isLeft 滚动的方向;
*/
const moving = (dom:HTMLDivElement, space: number, isLeft:boolean) => {

  // 用户快速点击 则把上次未滚动的距离与本次滚动结合再一起
  if (timer && TargetLocation !== -1) {
    toltalSpace += TargetLocation - dom.scrollLeft
    // 计算本次的目标距离
    if(isLeft) {
      TargetLocation = dom.scrollLeft + toltalSpace + space
    } else {
      TargetLocation = dom.scrollLeft + toltalSpace - space
    }
  } else if (!timer) {
    toltalSpace = 0
    TargetLocation = -1
  }

  if (isLeft) {
    toltalSpace -= space
  } else {
    toltalSpace += space
  }

  // 获取本次的目标位置
  const position = dom.scrollLeft
  TargetLocation = position + toltalSpace

  clearInterval(timer)
  timer = null
  const hz = 60
  let i = 1
  timer = setInterval(() => {
    dom.scrollLeft = position + i * toltalSpace / hz
    ++i
    if (i >= hz) {
      clearInterval(timer)
      timer = null
      dom.scrollLeft = TargetLocation // 位置修正
      toltalSpace = 0
      TargetLocation = -1
    }
  }, 1)
}

export {
  moving,
  movingColumn
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现横排多个dom自动横向轮播滚动的方法比较多,以下是其中一种实现方法: HTML结构: ```html <div class="container"> <div class="wrapper"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> <div class="item">4</div> <div class="item">5</div> </div> </div> ``` CSS样式: ```css .container { width: 100%; overflow: hidden; } .wrapper { display: flex; transition: transform 0.3s ease-in-out; } .item { flex: 0 0 20%; margin-right: 20px; } ``` JS实现: ```js // 获取容器和包裹元素 const container = document.querySelector('.container'); const wrapper = document.querySelector('.wrapper'); const items = document.querySelectorAll('.item'); // 计算每个item的宽度和margin-right的值 const itemWidth = items[0].offsetWidth + parseInt(getComputedStyle(items[0]).marginRight); const wrapperWidth = itemWidth * items.length; // 设置包裹元素的宽度 wrapper.style.width = `${wrapperWidth}px`; // 自动轮播 let intervalId; function startAutoPlay() { intervalId = setInterval(() => { const currentX = Math.abs(parseInt(wrapper.style.transform) || 0); const nextX = currentX + itemWidth; wrapper.style.transform = `translateX(-${nextX}px)`; }, 2000); } startAutoPlay(); // 手动控制滚动距离 let currentX = 0; container.addEventListener('click', e => { const target = e.target; if (target.classList.contains('item')) { clearInterval(intervalId); const index = Array.from(items).indexOf(target); currentX = itemWidth * index; wrapper.style.transform = `translateX(-${currentX}px)`; startAutoPlay(); } }); ``` 这段代码实现了自动轮播和手动控制单个滚动距离的功能。其中,自动轮播使用了定时器 setInterval() 实现,每隔 2 秒向左移动一个 item 的宽度;手动控制滚动距离使用了事件委托和事件冒泡的原理,点击 item 时会清除定时器,计算当前的滚动距离并设置包裹元素的 transform 属性,最后再次启动定时器实现自动轮播。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值