【发布时间】:2010-05-12 16:13:02
【问题描述】:
我真的不知道怎么问这个,所以我在这里写了脚本:
http://jsbin.com/acaxi/edit
这很简单,我正在尝试创建滑动面板。
我知道有很多脚本做得很好,说实话太多了。
如果有人认为你可以推荐一个插件而不是我的脚本,那么请分享!
【问题讨论】:
-
抱歉,您的问题到底是什么?您的演示似乎运行良好。
标签: jquery dynamic slider panels
我真的不知道怎么问这个,所以我在这里写了脚本:
http://jsbin.com/acaxi/edit
这很简单,我正在尝试创建滑动面板。
我知道有很多脚本做得很好,说实话太多了。
如果有人认为你可以推荐一个插件而不是我的脚本,那么请分享!
【问题讨论】:
标签: jquery dynamic slider 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 });
});
要动态添加提要,您需要将点击功能附加到每个添加的新提要或使用 jQuery .live() 事件处理程序。我选择了以前的方法。这是updated demo,以及代码:
$(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') } );
}
【讨论】: