Skip to content

Instantly share code, notes, and snippets.

@if0rest
Created September 24, 2021 13:36
Show Gist options
  • Save if0rest/27cdf02946ec9a0a9d0e6ec7c7b2c765 to your computer and use it in GitHub Desktop.
Save if0rest/27cdf02946ec9a0a9d0e6ec7c7b2c765 to your computer and use it in GitHub Desktop.
Универсальный jQuery-скрипт для блоков с вкладками
<style>
.tabs__content {
display: none;
}
.tabs__content.active {
display: block;
}
</style>
<div class="tabs">
<ul class="tabs__caption">
<li class="active">1-я вкладка</li>
<li>2-я вкладка</li>
</ul>
<div class="tabs__content active">
Содержимое первого блока
</div>
<div class="tabs__content">
Содержимое второго блока
</div>
</div><!-- .tabs -->
<script>
// v1
(function($) {
$(function() {
$('ul.tabs').each(function() {
$(this).find('li').each(function(i) {
$(this).click(function(){
$(this).addClass('active').siblings().removeClass('active')
.parents('div.tabs').find('div.tabs__content').eq(i).fadeIn(150).siblings('div.tabs__content').hide();
});
});
});
})
})(jQuery)
// v2 (need jQuery > 1.7)
(function($) {
$(function() {
$('ul.tabs__caption').on('click', 'li:not(.active)', function() {
$(this).addClass('active').siblings().removeClass('active')
.closest('div.tabs').find('div.tabs__content').removeClass('active').eq($(this).index()).addClass('active');
})
})
})(jQuery)
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment