Самый простой подход - дать дополнительное общее имя класса всем элементам, которые вы пытаетесь скользить вверх / вниз, а затем подключить фактические привязки к элементам, используя атрибут href
на привязке.Приведенный ниже фрагмент javascript (с изменениями в HTML) будет выполнять те же функции, что и ваш текущий javascript, а также упростит добавление дополнительных слайдеров:
# the html
<a class='about slide-control' href='#about'>toggle the about section</a>
<a class='info slide-control' href='#info'>toggle the info section</a>
<a class='contact slide-control' href='#contact'>toggle the contact section</a>
<div class='slider' id='about'>
this is the about section
</div>
<div class='slider' id='info'>
this is the info section
</div>
<div class='slider' id='contact'>
this is the contact section
</div>
# the javascript
$('a.slide-control').click(function() {
$('div.slider').slideUp();
var target = $(this).attr('href');
$(target).slideDown('slow');
});
Вы можете просто добавить новый слайдердобавив правильный HTML:
<a class='slide-control' href='#new_section'>Tell me more about this new section!</a>
<div class='slider' id='new_section'>
This is a great paragraph about the new section on our website!
</div>
В качестве альтернативы, вы можете использовать существующие классы и просто добавить вызов метода .slideUp
для других ваших элементов перед .slideDown
:
$('a.about').click(function() {
$("#info").slideUp('slow');
$("#contact").slideUp('slow');
$("#about").slideDown('slow');
});
// repeat for each click handler