这段代码是一个简单的前端页面交互功能,用于在页面A和页面B之间跳转时保存和恢复音乐播放进度。

页面A的代码:

<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <meta http-equiv='X-UA-Compatible' content='ie=edge'>
    <title>页面A</title>
</head>
<body>
    <h1>页面A</h1>
    <button id='goToB'>跳转到页面B</button>

    <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js'></script>
    <script>
        $(document).ready(function () {
            // 跳转到页面B时,保存当前音乐播放进度
            $('#goToB').click(function () {
                sessionStorage.setItem('musicProgress', JSON.stringify(ap.audio.currentTime));
                window.location.href = '页面B的URL';
            });
        });
    </script>
</body>
</html>

页面B的代码:

<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <meta http-equiv='X-UA-Compatible' content='ie=edge'>
    <title>页面B</title>
</head>
<body>
    <h1>页面B</h1>
    <button id='goToA'>跳转到页面A</button>

    <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js'></script>
    <script>
        $(document).ready(function () {
            // 从sessionStorage中获取音乐播放进度
            let musicProgress = JSON.parse(sessionStorage.getItem('musicProgress'));
            if (musicProgress) {
                // 设置音乐播放进度
                ap.audio.currentTime = musicProgress;
                // 继续播放音乐
                ap.play();
            }

            // 跳转到页面A时,保存当前音乐播放进度
            $('#goToA').click(function () {
                sessionStorage.setItem('musicProgress', JSON.stringify(ap.audio.currentTime));
                window.location.href = '页面A的URL';
            });
        });
    </script>
</body>
</html>

代码说明:

  1. 在页面A中,点击按钮跳转到页面B时,使用sessionStorage保存当前音乐播放进度ap.audio.currentTime,然后跳转到页面B。
  2. 页面B加载完成后,从sessionStorage中获取音乐播放进度,并设置ap.audio.currentTime为获取到的值。然后调用ap.play()方法继续播放音乐。
  3. 在页面B中,点击按钮跳转到页面A时,同样保存当前音乐播放进度,并跳转到页面A。

代码使用说明:

  1. 需要将页面A的URL和页面B的URL替换为实际的URL才能正常运行。
  2. 代码中使用了jQuery库来简化DOM操作,需要引入jQuery库才能正常使用。可以通过添加<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js'></script>来引入jQuery库。

总结:

通过以上代码,可以实现在AB两个页面相互跳转时能够继续播放音乐并且歌词从上次播放的位置开始显示。