当前位置:首页 > JavaScript

js实现倒计时页面跳转

2026-01-30 20:27:41JavaScript

实现倒计时页面跳转的方法

使用JavaScript实现倒计时并在倒计时结束后自动跳转页面,可以通过以下方法完成:

设置倒计时变量和显示

let countdown = 5; // 倒计时5秒
const countdownElement = document.getElementById('countdown'); // 显示倒计时的元素

function updateCountdown() {
  countdownElement.textContent = countdown;
  countdown--;

  if (countdown < 0) {
    clearInterval(timer);
    window.location.href = 'https://example.com'; // 跳转目标URL
  }
}

const timer = setInterval(updateCountdown, 1000); // 每秒更新一次

HTML部分

js实现倒计时页面跳转

<div>
  页面将在 <span id="countdown">5</span> 秒后自动跳转...
</div>

使用setTimeout实现

另一种更简单的方法是使用setTimeout:

setTimeout(() => {
  window.location.href = 'https://example.com';
}, 5000); // 5000毫秒=5秒后跳转

添加进度条效果

如果需要更直观的显示,可以添加进度条:

js实现倒计时页面跳转

const progressBar = document.getElementById('progress');
let width = 100;
const interval = 50; // 更新频率(毫秒)
const duration = 5000; // 总时长(毫秒)
const increment = (interval / duration) * 100;

const progressTimer = setInterval(() => {
  width -= increment;
  progressBar.style.width = width + '%';

  if (width <= 0) {
    clearInterval(progressTimer);
    window.location.href = 'https://example.com';
  }
}, interval);

对应的HTML

<div class="progress-container">
  <div id="progress" class="progress-bar"></div>
</div>

CSS样式

.progress-container {
  width: 100%;
  height: 5px;
  background: #eee;
}

.progress-bar {
  height: 100%;
  background: #4CAF50;
  width: 100%;
  transition: width 0.05s linear;
}

注意事项

  • 确保跳转URL正确无误
  • 考虑添加取消跳转的按钮
  • 在移动设备上测试效果
  • 如果页面内容重要,建议提供明显的手动跳转链接

完整示例代码

<!DOCTYPE html>
<html>
<head>
  <style>
    .progress-container {
      width: 100%;
      height: 5px;
      background: #eee;
      margin: 10px 0;
    }
    .progress-bar {
      height: 100%;
      background: #4CAF50;
      width: 100%;
    }
    #cancel-btn {
      margin-top: 10px;
      padding: 5px 10px;
      cursor: pointer;
    }
  </style>
</head>
<body>
  <div>
    页面将在 <span id="countdown">5</span> 秒后自动跳转...
  </div>
  <div class="progress-container">
    <div id="progress" class="progress-bar"></div>
  </div>
  <button id="cancel-btn">取消跳转</button>

  <script>
    let countdown = 5;
    const countdownElement = document.getElementById('countdown');
    const progressBar = document.getElementById('progress');
    const cancelBtn = document.getElementById('cancel-btn');
    let width = 100;
    const interval = 50;
    const duration = 5000;
    const increment = (interval / duration) * 100;

    let progressTimer = setInterval(() => {
      width -= increment;
      progressBar.style.width = width + '%';

      if (width <= 0) {
        clearInterval(progressTimer);
        window.location.href = 'https://example.com';
      }
    }, interval);

    let countdownTimer = setInterval(() => {
      countdown--;
      countdownElement.textContent = countdown;

      if (countdown <= 0) {
        clearInterval(countdownTimer);
      }
    }, 1000);

    cancelBtn.addEventListener('click', () => {
      clearInterval(progressTimer);
      clearInterval(countdownTimer);
      countdownElement.textContent = '已取消';
      progressBar.style.width = '100%';
      progressBar.style.backgroundColor = '#ccc';
    });
  </script>
</body>
</html>

标签: 跳转倒计时
分享给朋友:

相关文章

vue实现点击跳转路由

vue实现点击跳转路由

vue实现点击跳转路由的方法 在Vue中实现点击跳转路由,可以通过以下几种方式完成,具体取决于项目使用的路由管理工具(如Vue Router)以及需求场景。 使用router-link组件 rou…

js实现倒计时

js实现倒计时

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

h5实现登录页面跳转页面跳转页面跳转

h5实现登录页面跳转页面跳转页面跳转

使用HTML5实现登录页面跳转 在HTML5中实现登录页面跳转可以通过多种方式完成,包括表单提交、JavaScript跳转和超链接跳转。以下是几种常见的实现方法。 表单提交跳转 使用HTML表单的…

h5实现登录页面跳转

h5实现登录页面跳转

实现登录页面跳转的基本方法 使用HTML5和JavaScript实现登录页面跳转,可以通过表单提交或JavaScript事件触发。以下是几种常见方式: 表单提交跳转 在HTML中创建一个表单,通过a…

vue 实现跳转

vue 实现跳转

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转可以通过多种方式,以下是常见的几种方法: 使用 router-link 组件 router-link 是 Vue Router 提供的组件,用…

vue实现前端跳转

vue实现前端跳转

Vue 实现前端跳转的方法 在 Vue 中实现前端跳转主要通过路由(Vue Router)完成,以下是几种常见的方式: 声明式导航 使用 <router-link> 组件实现跳转,适合模…