سؤال

لديّ برنامج نصي للرسوم المتحركة jQuery يعمل بشكل صحيح ، ومع ذلك ، أشعر أن هناك طريقة لتقليل النفقات العامة الكلية للنص. فيما يلي رابط لصفحة التطوير:

http://dev.abinstallations.com

هناك جزأين للرسوم المتحركة ، وتطبق هذه الرسوم المتحركة على ستة عناصر ديف فردية. يتم تطبيق نص منفصل على كل عنصر. فمثلا:

//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.
  });
});

... وهلم جرا للعناصر الأربعة المتبقية. هل هناك طريقة مكثفة لتحقيق ذلك؟

هل كانت مفيدة؟

المحلول

يمكنك سحبها كدالة:

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.
      });
    });
}

أو حتى جعل الوظيفة تأخذ عدة معلمات وحلقة عليها.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top