Reputation: 2929
What I trying to achieve is moving div positions according to an array of positions. Number of objects is unknown could be between 4 to 20.
Only 4 elements shown at once.
To be clear, I've 4 item different positions and each time (like every 4 seconds) items will change position and then leave the scene. (below, numbers are represents items and letters are positions)
Items: 1 2 3 4 5 6 7 8 9 10
Posi.: a
Items: 1 2 3 4 5 6 7 8 9 10
Posi.: b a
Items: 1 2 3 4 5 6 7 8 9 10
Posi.: c b a
Items: 1 2 3 4 5 6 7 8 9 10
Posi.: d c b a
Items: 1 2 3 4 5 6 7 8 9 10
Posi.: - d c b a
Items: 1 2 3 4 5 6 7 8 9 10
Posi.: - - d c b a
these are the positions;
var positions = [
{ top: 0, left: 1100, zoom:70 },//(out of scene)
{ top: 0, left: 420, zoom:70 },
{ top: 185, left: 217, zoom:80 },
{ top: 310, left: 0, zoom:100},
{ top: 646, left: 210, zoom:80 },
{ top: 800, left: 210, zoom:80 } //(out of scene)
];
This is where am I so far. I believe I'm stuck. Seems working but it is broken.
var lastItemNo = -1;
var lastAnimation = 0;
var visibleItems = Array();
function tick(){
visibleItems[visibleItems.length] = (lastItemNo+1);
if((lastItemNo+1) == items.length){
lastItemNo = -1;
}else{
lastItemNo++;
}
if(visibleItems.length>6){
var removedItem = visibleItems.shift();
$(".left .holder > .tweet").eq(removedItem).attr("data-pos",0).css({
top:positions[0].top,
left:positions[0].left,
zoom:positions[0].zoom+"%",
});
}
for(var i=0;i<visibleItems.length;i++){
// console.log($(".left .holder > .tweet").eq(i).attr("data-pos"));
}
$.each(visibleItems,function(i,el){
var currentItem = $(".left .holder > .tweet").eq(el);
console.log(el);
console.log(parseInt(currentItem.attr("data-pos"))+i);
currentItem.animate({
top:positions[parseInt(currentItem.attr("data-pos"))].top,
left:positions[parseInt(currentItem.attr("data-pos"))].left,
zoom:positions[parseInt(currentItem.attr("data-pos"))].zoom+"%",
});
currentItem.attr("data-pos", parseInt(currentItem.attr("data-pos"))+1 )
});
clog("tick");
}
html is;
<div class="left">
<div class="holder">
<div class="tweet" data-pos="0">
<div class="avatar">
<img class="user" src="https://pbs.twimg.com/profile_images/478523550886658048/rLUgKkv7_normal.png" boder="0">
<div class="mask"><img src="/theme/images/avatar_mask.png" boder="0"></div>
</div>
<div class="text">
<div class="username">Software AG Tu00fcrkiye <small>@SoftwareAGTR</small></div>
<div class="tweet">TEXT</div>
</div>
</div>
<div class="tweet" data-pos="0">
<div class="avatar">
<img class="user" src="https://pbs.twimg.com/profile_images/478523550886658048/rLUgKkv7_normal.png" boder="0">
<div class="mask"><img src="/theme/images/avatar_mask.png" boder="0"></div>
</div>
<div class="text">
<div class="username">Software AG Tu00fcrkiye <small>@SoftwareAGTR</small></div>
<div class="tweet">TEXT!</div>
</div>
</div>
<div class="tweet" data-pos="0">
<div class="avatar">
<img class="user" src="https://pbs.twimg.com/profile_images/528538190798675969/La7toYrv_normal.jpeg" boder="0">
<div class="mask"><img src="/theme/images/avatar_mask.png" boder="0"></div>
</div>
<div class="text">
<div class="username">Ece Vahapoglu <small>@ecevahapoglu</small></div>
<div class="tweet">text https://t.co/JDfP6ATMWk</div>
</div>
</div>
<div class="tweet" data-pos="0">
<div class="avatar">
<img class="user" src="https://pbs.twimg.com/profile_images/581787648584511488/Kxy-mZGu_normal.jpg" boder="0">
<div class="mask"><img src="/theme/images/avatar_mask.png" boder="0"></div>
</div>
<div class="text">
<div class="username">Fatih Mert Esmer <small>@mertesmer</small></div>
<div class="tweet">#DASummit15 http://t.co/mpnBIh8zJK</div>
</div>
</div>
<div class="tweet" data-pos="0">
<div class="avatar">
<img class="user" src="https://pbs.twimg.com/profile_images/475291049724620800/TmAbgWKF_normal.jpeg" boder="0">
<div class="mask"><img src="/theme/images/avatar_mask.png" boder="0"></div>
</div>
<div class="text">
<div class="username">Agah Alptekin <small>@AgahAlptekin</small></div>
<div class="tweet">RT @digitalage: TEXT ://t.…</div>
</div>
</div>
</div>
</div>
Upvotes: 0
Views: 191
Reputation: 21492
Using the same positions
array as defined in the question:
var unusedItems = $('.left > .holder > .tweet').get(),
usedItems = [];
// Initially hide all the items.
$(unusedItems).css({ top: positions[0].top, left: positions[0].left, zoom: positions[0].zoom + "%" });
function tick() {
// If the usedItems array is full, remove the lead item and add it back
// to the unusedItems array (as long as the item isn't null).
if (usedItems.length == positions.length) {
var item = usedItems.shift();
if (item) {
unusedItems.push(item);
}
}
// Add an unused item to the usedItems array if there is one.
// Otherwise add null as a placeholder.
usedItems.push(unusedItems.shift() || null);
// Loop through the usedItems array and animate the items to their
// new positions, skipping the null placeholders.
$.each(usedItems, function(i, item) {
if (item) {
var $item = $(item),
position = positions[usedItems.length - 1 - i];
$item.animate({ top: position.top, left: position.left, zoom: position.zoom + "%" });
}
});
}
There won't be any null
placeholders if the number of items is equal to or greater than the number of positions.
The null
placeholders and the following line of code are what associate the positions with the items:
position = positions[usedItems.length - 1 - i];
Upvotes: 1