English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
まず效果图を紹介します。興味を持った方は実装コードを参照してください
以下に核心コードを示します:
$.ajax({ url: "/music/music.txt", type: "get", success: function(data) { data = jQuery.parseJSON(data); var length = data.length; var now=0; for (i = 0; i < length; i++) { $("#musicText li").eq(i).after("<li>" + data[i].text + "</li>") } var player = { playButton: $(".play"), songText: $(".musicText"), state: 0, //0再生,1一時停止 audio: $("#audio").get(0), bind: function() { //ボタン結合 //再生または一時停止 console.log($.type(this)) console.log($.type(this)) var obj = this; this.playButton.click(function() { obj.changeState(obj.state ? 0 : 1); }); //音声設定 $("#voice").click(function(ex) { var percent = (ex.clientX - $(this).offset().left) / $(this).width(); obj.setVoice(percent); }); //デフォルトの音声 0.8 obj.setVoice(1.0); //無音 $("#voiceOP").click(function() { if (obj.muted) { $(this).removeClass("muted"); obj.audio.muted = false; obj.muted = false; } else { $(this).addClass("muted"); obj.audio.muted = true; obj.muted = true; } }); //進行度設定 $("#MusicProgress").click(function(ex) { var percent = (ex.clientX - $(this).offset().left) / $(this).width(); obj.setProgress(percent, false); }); //前の曲 $("#prev").click(function() { obj.nowIndex--; if (obj.nowIndex < 0) obj.nowIndex = obj.list.length - 1; obj.playSing(obj.nowIndex); }); //次の曲 $("#next").click(function() { obj.nowIndex++; if (obj.nowIndex >= obj.list.length) obj.nowIndex = 0; obj.playSing(obj.nowIndex); player.audio.play(); }); //イベントバインド - 再生時間変更 this.audio.ontimeupdate = function() { obj.timeChange(); } //再生終了 this.audio.onended = function() { obj.singEnd(); } }, //再生状態切替 changeState: function(_state) { this.state = _state; if (!this.state) { this.playButton.removeClass("pause").addClass("play"); this.pause(); } else { this.playButton.removeClass("play").addClass("pause"); this.play(); } }, //再生 play: function() { this.audio.play(); }, //一時停止 pause: function() { this.audio.pause(); }, timeChange: function() { var nowSec = Math.floor(this.audio.currentTime); console.log(nowSec) console.log(data[now].time) if(nowSec>data[now].time){ now = now + 1; console.log(now) $("#musicText li").eq(now).addClass("active").siblings("li").removeClass("active"); $("#musicText").css("top",-(24*now)+138) } var totalSec = Math.floor(this.audio.duration); //現在進行度表示 var secTip = secFormat(nowSec) + "/" + secFormat(totalSec); if (secTip.length == 11) $("#secTip").html(secTip); this.setProgress(nowSec / totalSec, true); }, setVoice: function(percent) { $("#voice").children(".bar").css("width", percent * 100 + "%"); $("#voice").children("a").css("left", percent * 100 + "%"); this.audio.volume = percent; }, setProgress: function(percent, justCss) { $("#MusicProgress").children(".bar").css("width", percent * 100 + "%"); $("#MusicProgress").children("a").css("left", percent * 100 + "%"); if (!justCss) this.audio.currentTime = this.audio.duration * percent; }, singEnd: function() { if (this.style == 0) { this.nowIndex++; if (this.nowIndex >= this.list.length) this.nowIndex = 0; this.playSing(this.nowIndex); } else { var index = Math.floor(Math.random() * (this.list.length + 1)) - 1; index = index < 0 &63; 0 : index; index = index >= this.list.length &63; (this.list.length - 1) : index; this.playSing(index); this.nowIndex = index; } }, }; player.bind(); function secFormat(num) { var m = Math.floor(num / 60); var s = Math.floor(num % 60); return makeFormat(m) + ":" + makeFormat(s); function makeFormat(n) { if (n >= 10) return n; else { return "0" + n; } } } } )
そして、このコードは alpha0.0 です。1のバージョンは、常にアップデートされています。
以上は、jQueryを使用して実現した歌詞スライド版音楽プレイヤーのコードについて紹介しました。皆様に役立つことを願っています。何かご不明な点がございましたら、コメントを残してください。編集者はすぐに回答します。
声明:本稿の内容はインターネットから収集され、著作権者による所有物であり、インターネットユーザーが自発的に貢献し、自己でアップロードしたものであり、本サイトは所有権を持ちません。また、人工的な編集は行われていません。著作権侵害を疑う場合は、メールを notice#w までお送りください。3codebox.com(メールを送信する際は、#を@に置き換えてください。侵权を報告する場合は、関連する証拠を提供し、一旦確認がついたら、本サイトは直ちに侵害を疑われるコンテンツを削除します。)