我有一个可点击的栏,一旦点击就会使用Jquery的.animate功能移动三个单独的栏。
再次点击时,我需要相同的按钮来反转动画。我仍然是Jquery的菜鸟,所以我确定这是一个简单的范围问题,但我无法弄明白。
Jquery的:
// Show More Copy
$('.copy-more').click(function(){
var _toggle = 0;
var _header;
var _sum;
var _full;
var _opacity;
var _text;
if ( _toggle === 0) {
_header = 120;
_sum = 150;
_full = 220;
_opacity = 1;
_text = "Read Less";
_toggle = 1;
}
else {
_header = 310;
_sum = 345;
_full = 420;
_opacity = 0;
_text = "Read More";
_toggle = 0;
}
$('.slide-header').animate({'top': _header}, 150);
$('.copy-sum').animate({'top': _sum}, 250);
$('.copy-full').animate({'top': _full, 'opacity': _opacity}, 500);
$('.copy-more').animate({'opacity': 0}, 500, function () {
$(this).text(_text);
}).animate({'opacity': 1}, 500);
});
答案 0 :(得分:1)
_toggle
在.click
范围内定义。需要进一步确定这一点才能实现:
// Move _toggle to here
var _toggle = 0;
$('.copy-more').click(function(){
var _header;
var _sum;
var _full;
var _opacity;
var _text;
if ( _toggle === 0) {
_header = 120;
_sum = 150;
_full = 220;
_opacity = 1;
_text = "Read Less";
_toggle = 1;
}
else {
_header = 310;
_sum = 345;
_full = 420;
_opacity = 0;
_text = "Read More";
_toggle = 0;
}
$('.slide-header').animate({'top': _header}, 150);
$('.copy-sum').animate({'top': _sum}, 250);
$('.copy-full').animate({'top': _full, 'opacity': _opacity}, 500);
$('.copy-more').animate({'opacity': 0}, 500, function () {
$(this).text(_text);
}).animate({'opacity': 1}, 500);
});
但是,您应该考虑.toggleClass和CSS3 animations - 更清洁
答案 1 :(得分:0)