只是一點點免責聲明:我是jquery/javascript編程的總新手,並且只知道html/css設計的基礎知識。是否可以使用.each()數組來定位多個div?
我一直試圖在頁面上隨機定位一些soundcloud小部件。我似乎設法實現了這一點 - 但爲此我做了:在css中創建一個新的div id,將每個小部件分配給一個單獨的div id名稱,然後編寫一個函數將每個div移動個別。我嘗試使用.each(),但從我可以在其他問題中收集,這不能運行通過divs,只有div.class名稱...從這裏開始我很困惑嘗試.each(); .parent();類名等等等等測試..
這是JS,我必須每次( 'scTrack' 後僅僅改變數字)複製:
(function() {
var docHeight = $(document).height(),
docWidth = $(document).width(),
$div = $('#scTrack1'),
divWidth = $div.width(),
divHeight = $div.height(),
heightMax = docHeight - divHeight,
widthMax = docWidth - divWidth;
$div.css({
left: Math.floor(Math.random() * widthMax),
top: Math.floor(Math.random() * heightMax)
});
});
CSS:
#scTrack1 {
position:absolute;
height:70px;
width: 200px;
}
HTML
<div id="scTrack1">
<object height="81" width="100%"> <param name="movie" value="https://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F67155680&show_comments=false&auto_play=false&color=000000"></param> <param name="allowscriptaccess" value="always"></param> <embed allowscriptaccess="always" height="81" src="https://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F67155680&show_comments=false&auto_play=false&color=000000" type="application/x-shockwave-flash" width="100%"></embed> </object>
</div>
這裏是我的的jsfiddle例如:http://jsfiddle.net/antrgor_reiz/scVRS/
我想實現的是在css中定義一個div - 用於所有不同的soundcloud小部件 - 以及一些js,它將通過該div的每個實例循環並運行該函數以重新定位div。
這是可能的嗎?
非常感謝!
p.S.我通常不喜歡創建僅使用一次的變量。這是上面的一個較短版本,不會打擾創建'divWidth,divHeight,heightMax或widthMax':http://jsfiddle.net/scVRS/65/(我在上面的回答中留下了它們,以便您可以看到我的解決方案與您的原始代碼有多相似)。 – nnnnnn