2012-12-11 70 views
4

以下是顯示Spotify播放按鈕的jQuery代碼,當我點擊「Rock the house!」鏈接時當我點擊一個觸發它顯示的鏈接時,如何觸發Spotify Play按鈕播放?

$(function() { 
$('#partystart').toggle(function() { 
    $(".fadeIn").addClass("party"); 
    $("#musicbox").slideDown("300"); 
    $("#partystart").html("<a id='partystart'>Take a break</a>"); 
}, function() { 
    $(".fadeIn").removeClass("party"); 
    $(".fadeIn").addClass("fullopacity"); 
    $("#musicbox").slideUp("300"); 
    $("#partystart").html("<a id='partystart'>&#9650; Rock the house!</a>"); 
}); 
}); 

下面是HTML:

<div id="partybox" > 
    <iframe id="musicbox" style="margin-top: -2px; display: none;" src="https://embed.spotify.com/?uri=spotify:track:3QMLpta0AmeJLpWNmeyC6B#0:12" width="250" height="80" frameborder="0" allowtransparency="true"></iframe> 
    <a id="partystart">&#9650; Rock the house!</a> 
</div> 

下面是一個例子,可以讓你點擊玩了一圈軌道,而不是實際的Spotify裏面播放按鈕播放按鈕:http://spotifymusic.tumblr.com/

另外,我把#0:12放在歌曲URI的末尾,讓歌曲在0:12秒開始播放,但似乎不起作用。我在那裏做錯了什麼?

謝謝!

+0

順便說一下,我添加了HREF觸發軌跡如下: ▲ Rock the house! 的問題是,它只是觸發的時候,我沒有jquery的涉案 –

回答

1

我沒有Spotify帳戶,也不打算創建一個帳戶,但下面的解決方案應該可以工作,您只需要進行一些調查即可。

Spotify iFrame有一個點擊事件附加到.play-pause-btn(我認爲)。您需要激活點擊事件才能開始播放。這將是這樣的:

$("#musicbox").contents().find("div.play-pause-btn").click(); 

與您的代碼的其餘部分添加它:

$('#partystart').toggle(function() { 
    $(".fadeIn").addClass("party"); 
    $("#musicbox").slideDown("300"); 
    $("#partystart").html("<a id='partystart'>Take a break</a>"); 
    $("#musicbox").contents().find("div.play-pause-btn").click(); 
}, function() { 
    $(".fadeIn").removeClass("party"); 
    $(".fadeIn").addClass("fullopacity"); 
    $("#musicbox").slideUp("300"); 
    $("#partystart").html("<a id='partystart'>&#9650; Rock the house!</a>"); 
}); 
}); 

如果它不工作,你需要確定哪個項目對click事件它。

+0

有沒有人曾經得到這個工作,發揮軌道? – Garuuk