views:

28

answers:

1

I don't really know how to ask this, so I wrote up the script here: http://jsbin.com/acaxi/edit
It's pretty straight forward, I'm trying to create sliding panels.

I know there's alot of scripts that does that good, to be honest there are too many.

If anyone thinks there is a plugin you could recommend instead of my script then please do share!

+1  A: 

I'm still not sure what your question is, but I reworked your code a bit to make it work with any number of feed panels (updated demo).

$(document).ready(function(){

  var feeds = $('#feeds div'),
      numFeeds = feeds.length;

  feeds
    .click(function(){
      $(this)
        .animate({"margin-left": "-200px", opacity: 0}, "fast")
        .animate({"margin-left": "200px"}, "fast");
      // add 2 since the id isn't zero based
      var next = ( $(this).index() + 2 > numFeeds ) ? 1 : $(this).index() + 2;
      $('div#feed' + next).animate({"margin-left": 0, opacity: 1}, "fast")
    })
    .nextAll().css({ 'margin-left' : '200px', opacity : 0 });

});

To add feeds dynamically you'll need to either attach a click function to each new feed added or use the jQuery .live() event handler. I opted for the previous method. Here is the updated demo, and the code:

$(document).ready(function(){

  var feeds = $('#feeds .feeds'),
      numFeeds = feeds.length;

  // initialize
  feeds
   .click(function(){ animateFeed(this, numFeeds); })
   .nextAll().css({ 'margin-left' : '200px', opacity : 0 });

  // add another feed
  $('.addFeed').click(function(){
   $('<div id="feed' + ( numFeeds++ +1 ) + '" class="feeds">' + numFeeds +'</div>')
    .click(function(){ animateFeed(this, numFeeds); })
    .css({ 'margin-left' : '200px', opacity : 0 })
    .appendTo(feeds.parent());
   $('#num').text(numFeeds);
  })

});

// animate feed panel
function animateFeed(el, num){
 var indx = $(el).index(),
     next = ( indx + 1 ) % num;
 $('.feeds').removeClass('active');
 $(el)
  .animate({ marginLeft : '-200px', opacity: 0}, 'fast')
  .animate({ marginLeft : '200px'}, 'fast' );
 $('.feeds:eq(' + next + ')').animate({ marginLeft : 0, opacity : 1}, 'fast', function(){ $(this).addClass('active') } );
}
fudgey
That's exactly what I was looking for, my question wasn't written in the best way sorry. I want to be able to add as many feed divs as possible without having to make a click function manually for every new one. So if I have a button on the page that creates a new div called feed(n) then it would automatically make that a part of the sliding function. Don't know if that makes any more sense?
Noor
I've updated my answer for you, I hope that helps :)
fudgey