Я недавно работал над небольшим блогом и хотел бы, чтобы последовательные записи в блоге были доступны из аккордеона (пример ниже).Таким образом, вы можете быстро просмотреть заголовки сообщений, выбрать интересный и прочитать его.Когда вы закончите, вы можете легко вернуться к скиммингу без ненужной навигации по меню.
Моя проблема в том, что как только вы закончили читать пост и нажали второй, я не могу открыть второй поствверху, с видимым заголовком.Поскольку вы уже прокрутили вниз до первого поста, вы открываете 2/3 второго поста.Это вынуждает пользователя прокручивать все назад до вершины поста, который он еще не прочитал.По какой-то причине я не могу заставить что-либо работать;Любое руководство будет высоко ценится!
ОБНОВЛЕНИЕ: Оказывается, поскольку я использовал тонкую версию jQuery, функции, которые я пытался использовать, были недоступны.Теперь, когда я преодолел это препятствие, все компилируется, но я не могу заставить страницу прокрутиться в нужное место.
Моё самое близкое решение - это прокрутить обратно до первого card-header
при открытии нового раздела (тогда как я хочу card-header
, по которому щелкнули).
$(".card-header").click(function (event) {
$('html, body').animate({
scrollTop: $(".card-header").offset().top
}, 300);
});
То, что я пытаюсь сделать, логически эквивалентно этому, но этот точный код вообще не прокручивается (он компилируется правильно и заменяет $(this)
на $(event.target)
или $(event.target).parent()
тоже не работает).
$(".card-header").click(function(event) {
$('html, body').animate({
scrollTop: $(this).offset().top-60
}, 300);
Вот минимальный рабочий пример для воспроизведения моей борьбы:
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container" id="container">
<div class="card">
<a class="card-header" data-toggle="collapse" href="#sec1">Title1</a>
<div id="sec1" class="collapse" data-parent="#container">
<div class="card-body">
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
</div>
</div>
</div>
<div class="card">
<a class="card-header" data-toggle="collapse" href="#sec2">Title2</a>
<div id="sec2" class="collapse" data-parent="#container">
<div class="card-body">
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
lots and lots of text
</div>
</div>
</div>
</div>