想要压缩我的 Jquery 动画脚本

发布于 2024-09-30 07:24:09 字数 1139 浏览 0 评论 0原文

我有一个运行正常的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心 2024-10-07 07:24:09

您可以将其作为函数取出:

Hook("first");
Hook("second");

function Hook( id )
{
    $("#" + id + "_flip").hide();
    $("#" + id + "_button_show").click(function(){
      $("#" + id "_flip").animate({
         opacity: 'toggle',
         top: '+=524',
         height: 'toggle'}, 600, function() {
           // Animation complete.
      });
    });
    $("#" + id + "_button_hide").click(function(){
      $("#" + id + "_flip").animate({
         opacity: 'toggle',
         top: '-=524',
         height: 'toggle'}, 400, function() {
           // Animation complete.
      });
    });
}

或者甚至使该函数接受多个参数并循环遍历它们。

You could pull it out as a function:

Hook("first");
Hook("second");

function Hook( id )
{
    $("#" + id + "_flip").hide();
    $("#" + id + "_button_show").click(function(){
      $("#" + id "_flip").animate({
         opacity: 'toggle',
         top: '+=524',
         height: 'toggle'}, 600, function() {
           // Animation complete.
      });
    });
    $("#" + id + "_button_hide").click(function(){
      $("#" + id + "_flip").animate({
         opacity: 'toggle',
         top: '-=524',
         height: 'toggle'}, 400, function() {
           // Animation complete.
      });
    });
}

Or even make the function take several parameters and loop over them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文