Если вас интересует немного продвинутая реализация, взгляните на эти коды.
- Он поддерживает добавление статей во время выполнения. Просто добавьте к нему какую-нибудь статью, и она будет составлять из них группу
- Вы можете настроить некоторые параметры, такие как продолжительность анимации, количество элементов в каждой группе, имя тега статьи или имя класса (необходимо добавить. Вручную),. ..
С некоторыми изменениями вы можете сделать из него плагин jQuery. Также используется анимация затухания jQuery.
// This is a closure
(function($) {
'use strict';
$(function() {
var
articles_parent,
articles,
//-----
btn_next,
btn_prev,
btn_more,
//-----
inside_group_tag_or_class,
//-----
group_class,
active_group_class,
animation_time;
var
group_count,
group_length,
group_counter;
var
in_repair,
i;
var click_timeout;
// assign value to variable(s)
articles_parent = $('#myList');
//-----
inside_group_tag_or_class = 'article';
//-----
articles = articles_parent.find('> ' + inside_group_tag_or_class);
group_class = 'each-group';
active_group_class = 'active-group';
//-----
animation_time = 260;
//-----
btn_next = $('#loadMore');
btn_prev = $('#showLess');
btn_more = $('#moreArticles');
//-----
in_repair = false;
//-----
group_counter = 1;
// calculate group count and length
group_count = 5;
group_length = Math.ceil(articles.length / group_count);
// wrap group x to a new group
function slicer(x) {
var
wrapper_element,
current_group,
//-----
from,
to;
// assign value to variable(s)
wrapper_element = '<div class="' + group_class + '" data-group="' + group_counter++ + '" />';
//-----
from = x * group_count;
to = from + group_count;
//-----
current_group = articles.slice(from, to);
current_group.wrapAll(wrapper_element);
}
function updateSlicer() {
articles = articles_parent.find('> ' + inside_group_tag_or_class);
group_length = Math.ceil(articles.length / group_count);
// call slicer function to group each n article
for (i = 0; i < group_length; i++) {
slicer(i);
}
}
function showGroup(action, repair) {
animateGroupItems('show', action, repair);
}
function hideAllGroups() {
animateGroupItems('hide');
}
function animateGroupItems(action, extra, repair) {
repair = repair === false;
var
all_groups,
active_group,
active_group_items;
//-----
var idx;
//-----
all_groups = getAllGroups();
active_group = getActiveGroup(repair);
active_group_items = active_group.find('> ' + inside_group_tag_or_class);
if (action === 'show') {
var
show_action,
active_group_idx,
first_groups_index,
last_groups_index;
//-----
show_action = extra;
active_group_idx = active_group.index();
first_groups_index = 0;
last_groups_index = all_groups.last().index();
// check show action
if (show_action === 'next') {
if (active_group_idx !== last_groups_index) {
idx = active_group_idx + 1;
} else {
resetClick();
return;
}
hideAllGroups();
} else if (show_action === 'prev') {
if (active_group_idx !== first_groups_index) {
idx = active_group_idx - 1;
} else {
resetClick();
return;
}
hideAllGroups();
} else {
idx = first_groups_index;
hideAllGroups();
}
setTimeout(function() {
// main show action
active_group.removeClass(active_group_class);
all_groups.eq(idx).addClass(active_group_class);
active_group = getActiveGroup(true);
active_group_items = active_group.find('> ' + inside_group_tag_or_class);
//-----
active_group_items.show();
active_group.stop().fadeIn(animation_time, function() {
can_click = true;
});
}, 2 * animation_time);
} else if (action === 'hide') {
all_groups.stop().fadeOut(animation_time);
}
}
function getActiveGroup(repair) {
return checkActiveGroup(repair);
}
function getAllGroups() {
return articles_parent.find('.' + group_class);
}
function checkActiveGroup(repair) {
repair = repair === true;
var
all_groups,
active_group,
active_group_length;
all_groups = getAllGroups();
active_group = articles_parent.find('.' + group_class + '.' + active_group_class);
active_group_length = active_group.length;
// if we don't have any active group, select first one
if (!active_group_length) {
all_groups.eq(0).addClass(active_group_class);
if (repair && !in_repair) {
in_repair = true;
repairGroups();
}
}
// if we have more than one active group, remove active class from all groups but first one
if (active_group_length > 1) {
active_group.not(active_group.first()).removeClass(active_group_class);
}
active_group = articles_parent.find('.' + group_class + '.' + active_group_class);
return active_group;
}
function repairGroups() {
var all_groups;
all_groups = getAllGroups();
articles.stop().fadeOut(animation_time, function() {
all_groups.stop().fadeOut(animation_time);
});
showGroup();
in_repair = false;
}
function resetClick() {
clearTimeout(click_timeout);
can_click = true;
}
// call slicer function to group each n article
for (i = 0; i < group_length; i++) {
slicer(i);
}
// show first group
showGroup(false);
var can_click = true;
// show next group
btn_next.on('click', function() {
if (can_click) {
can_click = false;
showGroup('next');
}
});
// show previous group
btn_prev.on('click', function() {
if (can_click) {
can_click = false;
showGroup('prev');
}
});
// insert more article
var counter = 1;
btn_more.on('click', function() {
for (var j = 0; j < 5; j++) {
articles_parent.append($('<article/>').text('New article number ' + counter++));
}
updateSlicer();
});
});
})(jQuery);
#myList {
min-height: 75px;
}
#myList article {
display: none;
}
#loadMore {
color: green;
cursor: pointer;
}
#showLess {
color: red;
cursor: pointer;
}
#moreArticles {
color: blue;
cursor: pointer;
}
#loadMore:hover,
#showLess:hover,
#moreArticles:hover {
color: black;
}
.each-group {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="myList">
<article>One</article>
<article>Two</article>
<article>Three</article>
<article>Four</article>
<article>Five</article>
<article>Six</article>
<article>Seven</article>
<article>Eight</article>
<article>Nine</article>
<article>Ten</article>
<article>Eleven</article>
<article>Twelve</article>
<article>Thirteen</article>
<article>Fourteen</article>
<article>Fifteen</article>
<article>Sixteen</article>
<article>Seventeen</article>
<article>Eighteen</article>
<article>Nineteen</article>
<article>Twenty</article>
</div>
<div id="loadMore">Load more</div>
<div id="showLess">Show less</div>
<div id="moreArticles">Add more article</div>