【发布时间】:2014-08-26 23:24:51
【问题描述】:
我有两个类似的功能如下;
对于最后一个元素函数;
function last () {
var $this = $(this);
$this.animate({
left:'-1200',
opacity:0
},500, function(){
$this
.addClass('hide')
.next()
.removeClass('hide').animate({
left:'0',
opacity:1
},500)
});
}
对于第一个元素函数;
function first () {
var $this = $(this);
$this.animate({
left:'1200',
opacity:0
},500, function(){
$this
.addClass('hide')
.prev()
.removeClass('hide').animate({
left:'0',
opacity:1
},500)
});
}
它们很相似,我想将它们组合为一个功能,如下所示;
function steps (pos) { /* -- Dif-1 -- */
var $this = $(this);
$this.animate({
left:pos==='next'&& '-'+'1200', /* -- Dif-2 -- */
opacity:0
},500, function(){
$this
.addClass('hide')
pos==='next' ? next() : prev() /* -- Dif-3 -- */
.removeClass('hide').animate({
left:'0',
opacity:1
},500)
});
}
我想根据 pos 变量(pos==='next' ? next() : prev() )确定函数 next() 或 prev() 。我该怎么做?
【问题讨论】:
标签: javascript jquery conditional chaining