想要压缩我的 Jquery 动画脚本
我有一个运行正常的 jquery 动画脚本,但是,我觉得有一种方法可以减少脚本的总体开销。以下是开发页面的链接:
http://dev.abinstallations.com
动画分为两部分,这些动画适用于六个单独的 div 元素。每个元素都应用一个单独的脚本。例如:
//First
$("#first_flip").hide();
$("#first_button_show").click(function(){
$('#first_flip').animate({
opacity: 'toggle',
top: '+=524',
height: 'toggle'}, 600, function() {
// Animation complete.
});
});
$("#first_button_hide").click(function(){
$('#first_flip').animate({
opacity: 'toggle',
top: '-=524',
height: 'toggle'}, 400, function() {
// Animation complete.
});
});
//Second
$("#second_flip").hide();
$("#second_button_show").click(function(){
$('#second_flip').animate({
opacity: 'toggle',
top: '+=524',
height: 'toggle'}, 600, function() {
// Animation complete.
});
});
$("#second_button_hide").click(function(){
$('#second_flip').animate({
opacity: 'toggle',
top: '-=524',
height: 'toggle'}, 400, function() {
// Animation complete.
});
});
...其余四个元素依此类推。有没有一种简洁的方法来实现这一目标?
I have a jquery animation script that is working properly, however, I feel like there is a way to reduce the overall overhead for the script. Here is a link to the development page:
http://dev.abinstallations.com
There are two parts to the animation, and these animations apply to six individual div elements. A separate script is applied to each element. For example:
//First
$("#first_flip").hide();
$("#first_button_show").click(function(){
$('#first_flip').animate({
opacity: 'toggle',
top: '+=524',
height: 'toggle'}, 600, function() {
// Animation complete.
});
});
$("#first_button_hide").click(function(){
$('#first_flip').animate({
opacity: 'toggle',
top: '-=524',
height: 'toggle'}, 400, function() {
// Animation complete.
});
});
//Second
$("#second_flip").hide();
$("#second_button_show").click(function(){
$('#second_flip').animate({
opacity: 'toggle',
top: '+=524',
height: 'toggle'}, 600, function() {
// Animation complete.
});
});
$("#second_button_hide").click(function(){
$('#second_flip').animate({
opacity: 'toggle',
top: '-=524',
height: 'toggle'}, 400, function() {
// Animation complete.
});
});
...and so on for the remaining four elements. Is there a condensed way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将其作为函数取出:
或者甚至使该函数接受多个参数并循环遍历它们。
You could pull it out as a function:
Or even make the function take several parameters and loop over them.