我真的不知道怎么问这个,所以我在这里写了脚本:http:
//jsbin.com/acaxi/edit
这很简单,我正在尝试创建滑动面板。
我知道有很多脚本做得很好,说实话有太多了。
如果有人认为有一个插件可以推荐而不是我的脚本,那么请分享!
我真的不知道怎么问这个,所以我在这里写了脚本:http:
//jsbin.com/acaxi/edit
这很简单,我正在尝试创建滑动面板。
我知道有很多脚本做得很好,说实话有太多了。
如果有人认为有一个插件可以推荐而不是我的脚本,那么请分享!
我仍然不确定您的问题是什么,但我对您的代码进行了一些修改,以使其适用于任意数量的提要面板(更新的演示)。
$(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 });
});
要动态添加提要,您需要将点击功能附加到每个添加的新提要或使用 jQuery .live() 事件处理程序。我选择了以前的方法。这是更新的演示和代码:
$(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') } );
}