我这里有一个简单的代码
$("#faqs h4").click(function(){
var div=$("div");
var h4=$("h4");
h4.animate({letterSpacing:'0.1em'}, "slow");
});
单击h4标题时,字母将间隔为0.1em。现在,我不知道如何编写另一个代码,以便再次单击该代码时它可以返回到0.0em。救命!谢谢!
这是您可以做到的一种方法:
$("#faqs h4").click(function(){
var div=$("div");
var h4=$("h4");
var theSpacing = h4.css('letterSpacing');
if (theSpacing == '0em') {
h4.animate({letterSpacing:'0.1em'}, "slow");
}
else {
h4.animate({letterSpacing: '0em'}, 'slow');
}
});
或另一种方式
var theSpacing = 0; // set a counter var to use as a toggle method
$("#faqs h4").click(function(){
var div=$("div");
var h4=$("h4");
if (theSpacing == 0) {
h4.animate({letterSpacing:'0.1em'}, "slow");
theSpacing += 1; // sets it not equal to 0 so on next click it will run the else statement
}
else {
h4.animate({letterSpacing: '0em'}, 'slow');
theSpacing = 0; // sets it equal to 0 so on next click will run the code in the if statement
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句