سؤال
وطيب، والتراسل الفوري تحاول تحريك شيء إلى آفاق مختلفة إذا معرف لديه نخبة فئة مختارة. لدي التعليمة البرمجية التالية.
function myfunction() {
if ($('#homebutton').hasClass("active"));
{
$('#content').animate({height: "240px",opacity: 1}, 100 , hideLoader());
}
if ($('#showbutton').hasClass("active"));
{
$('#content').animate({height: "79px",opacity: 1}, 200 , hideLoader());
}
if ($('#aboutbutton').hasClass("active"));
{
$('#content').animate({height: "527px",opacity: 1}, 300 , hideLoader());}
if ($('#contactbutton').hasClass("active"));
{
$('#content').animate({height: "1040px",opacity: 1}, 400 , hideLoader());
}
}
وهذا هو تسلسل الرسوم المتحركة بعد بعضها البعض بغض النظر عن ما كنت فوق ذلك ينتهي به الأمر 1040px عالية ما أفعل الخطأ ؟؟
وحاول تغييرها إلى
{if ($('#homebutton').hasClass("active"));
$('#content').animate({height: "240px",opacity: 1}, 100 , hideLoader());
}
ومع الإطلاق أي تأثير
المحلول
ولقد فصل إذا كانت تصريحات من التعليمات البرمجية على السطر التالي. الشيء الوحيد الذي قمت بتنفيذ عندما يكون الشرط صحيحا هو بيان فارغ.
تغيير هذا:
if ($('#homebutton').hasClass("active"));
في:
if ($('#homebutton').hasClass("active"))
ووالشيء نفسه بالنسبة للالثلاث الأخرى.
نصائح أخرى
ويمكنك تنظيف ما يصل الكثير من خلال الاستفادة من لغة لطيفة أن جافا سكريبت هي:
$.each({
'#homebutton': { height: '240px', speed: 100 },
'#showbutton': { height: '79px', speed: 200 },
'#aboutbutton': { height: '527px', speed: 300 },
'#contactbutton': { height: '1040px', speed: 400 }
}, function(selector, controls) {
if ($(selector).hasClass('active'))
$('#content').animate({height: controls.height, opacity: 1}, controls.speed, hideLoader());
});
لا تنتمي إلى StackOverflow