如何在 jQuery 中实现可排序的播放列表

发布于 2024-11-15 18:00:06 字数 313 浏览 3 评论 0原文

我正在制作一个简单的音乐在线播放应用程序。我想制作一个歌曲播放列表,并且我希望播放列表中的歌曲可以排序。我计划使用 jQuery“可排序”插件。但是,我想跟踪歌曲的顺序,以便当一首歌曲结束时,我知道下一首开始哪首歌。我当前的解决方案不是创建一个在用户更改歌曲顺序时执行的事件处理程序,并实际跟踪歌曲的顺序,而是这样:

  1. 跟踪当前播放的歌曲元素的
  2. id歌曲结束,使用 jQuery 查找具有当前 id 的元素
  3. 使用 .next() 获取下一首要播放的歌曲

此实现有什么错误吗?不断跟踪歌曲的顺序会更好吗?

I am making a simple music playing online app. I would like to make a song playlist, and I would like the songs in the playlist to be sortable. I plan to use the jQuery "sortable" plugin. However, I would like to keep track of what order the songs are in, so that when one song finishes, I know which song to start next. Instead of creating an event handler to be executed when the user changes the order of the songs, and actually keeping track of what order songs are in, my current solution is simply this:

  1. Keep track of the id of the currently playing song element
  2. When the song finishes, use jQuery to find the element with the current id
  3. Use .next() to get the next song to play

Is there anything wrong with this implementation? Would it be better to constantly keep track of the order of songs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不是你的备胎 2024-11-22 18:00:06

看起来完全合适,实际上我在一个完全不相关的网站上使用了完全相同的方法。这是我获取下一个 id 的方法,我知道你说过你已经有一些工作了,但我想我还是会分享,以防有帮助:

current_id = window.my_id;
next_id = $('#' + current_id).next().attr('id');

if (next_id == undefined) {
    return; // no more siblings after it
}

// do what you need to do here with next_id to actually load the next content
// in my web apps case, it removes some elements and just changes the hash
// which another function handles

current_id = next_id;
window.my_id = current_id;

Seems entirely appropriate and I actually use that exact same approach on a completely unrelated website. Here is what I do to get the next id, I know you said you already had something working but I thought I'd share anyways in case it helps:

current_id = window.my_id;
next_id = $('#' + current_id).next().attr('id');

if (next_id == undefined) {
    return; // no more siblings after it
}

// do what you need to do here with next_id to actually load the next content
// in my web apps case, it removes some elements and just changes the hash
// which another function handles

current_id = next_id;
window.my_id = current_id;
°如果伤别离去 2024-11-22 18:00:06

这确实没有什么问题。它不必保留订单的Array,每次重新排序都需要更新该订单,而不是仅在需要时(在前一曲目末尾)读取下一首曲目。

There isn't really anything wrong with that. It saves having to keep an Array of the order, which would need to be updated per reorder as opposed to just reading the next track only when required (at end of previous one).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文