当前位置:首页 > JavaScript

js实现下拉刷新

2026-01-15 15:02:03JavaScript

监听触摸事件

通过监听 touchstarttouchmovetouchend 事件来检测用户下拉手势。记录触摸起始位置和移动距离。

let startY = 0;
let currentY = 0;

document.addEventListener('touchstart', (e) => {
  startY = e.touches[0].clientY;
});

document.addEventListener('touchmove', (e) => {
  currentY = e.touches[0].clientY;
  const distance = currentY - startY;

  if (distance > 0 && window.scrollY === 0) {
    e.preventDefault();
    updateRefreshUI(distance);
  }
});

document.addEventListener('touchend', (e) => {
  const distance = currentY - startY;
  if (distance > 100) { // 触发刷新的阈值
    triggerRefresh();
  } else {
    resetRefreshUI();
  }
});

更新下拉刷新UI

根据下拉距离动态更新UI,如下拉提示文本或加载动画。

function updateRefreshUI(distance) {
  const refreshElement = document.getElementById('refresh-element');
  refreshElement.style.transform = `translateY(${distance}px)`;

  if (distance > 100) {
    refreshElement.textContent = '释放刷新';
  } else {
    refreshElement.textContent = '下拉刷新';
  }
}

触发刷新动作

当用户下拉超过阈值时执行数据刷新操作,通常是一个异步请求。

function triggerRefresh() {
  const refreshElement = document.getElementById('refresh-element');
  refreshElement.textContent = '加载中...';

  fetchNewData().then(() => {
    resetRefreshUI();
  });
}

async function fetchNewData() {
  // 模拟异步请求
  return new Promise(resolve => {
    setTimeout(() => {
      console.log('数据已更新');
      resolve();
    }, 1500);
  });
}

重置UI状态

刷新完成后或取消下拉时恢复初始状态。

function resetRefreshUI() {
  const refreshElement = document.getElementById('refresh-element');
  refreshElement.style.transform = 'translateY(0)';
  refreshElement.textContent = '下拉刷新';
  startY = 0;
  currentY = 0;
}

完整HTML结构示例

<!DOCTYPE html>
<html>
<head>
  <style>
    #refresh-element {
      position: fixed;
      top: -50px;
      width: 100%;
      text-align: center;
      padding: 15px 0;
      background: #f5f5f5;
      transition: transform 0.2s;
    }
    .content {
      padding: 20px;
      height: 200vh;
    }
  </style>
</head>
<body>
  <div id="refresh-element">下拉刷新</div>
  <div class="content">页面内容区域</div>
  <script>
    // 上述JavaScript代码
  </script>
</body>
</html>

注意事项

  • 确保只在页面顶部(scrollY === 0)时触发下拉刷新
  • 添加 e.preventDefault() 防止页面整体滚动
  • 合理设置触发刷新的阈值距离(通常100-150px)
  • 考虑添加加载动画提升用户体验
  • 在移动端开发时注意300ms点击延迟问题

js实现下拉刷新

标签: js
分享给朋友:

相关文章

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 或直接使用 window.location 实现页面跳转,适用于普通跳转或带参数的 URL。 // 方…

vue实现js休眠

vue实现js休眠

Vue 中实现 JavaScript 休眠 在 Vue 中实现 JavaScript 休眠通常需要使用异步方式,以避免阻塞主线程。以下是几种常见方法: 使用 setTimeout 和 Pro…

css3结合js制作

css3结合js制作

CSS3 结合 JavaScript 制作动态效果 CSS3 和 JavaScript 结合可以实现丰富的动态效果,包括动画、交互和响应式设计。以下是几种常见的实现方式: CSS3 动画与 Java…

js实现跳转

js实现跳转

使用 window.location 跳转 通过修改 window.location.href 实现页面跳转,这是最常用的方法。 window.location.href = 'https:/…

js实现动画

js实现动画

使用 CSS 动画与 JavaScript 控制 通过 JavaScript 动态添加或移除 CSS 类来触发动画。CSS 定义关键帧(@keyframes),JavaScript 通过 classL…

js实现倒计时

js实现倒计时

使用 setInterval 实现倒计时 通过 setInterval 定时器每秒更新剩余时间,适用于简单倒计时场景。 function countdown(seconds, callback)…