Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit 1430757

Browse files
committed
feat: scroll sections list if the active item is ouf of view
Signed-off-by: Sekwah <[email protected]>
1 parent 59bc608 commit 1430757

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

theme/_layouts/examples.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,25 @@
6363
{% endif %}
6464
{% include site-footer.html %}
6565
</div><!-- /.js-footer-area -->
66+
<!-- Custom ScrollSpy Logic -->
67+
<script>
68+
$(document).ready(function() {
69+
$('[data-spy="scroll"]').on('activate.bs.scrollspy', function (event) {
70+
const highlightedElement = $(event.target);
71+
72+
if (highlightedElement.length) {
73+
const elementRect = highlightedElement[0].getBoundingClientRect();
74+
const container = highlightedElement.parent().parent()[0];
75+
const containerRect = container.getBoundingClientRect();
76+
77+
const isOutOfView = elementRect.top < containerRect.top || elementRect.bottom > containerRect.bottom;
78+
79+
if (isOutOfView) {
80+
highlightedElement[0].scrollIntoView();
81+
}
82+
}
83+
});
84+
});
85+
</script>
6686
</body>
6787
</html>

0 commit comments

Comments
 (0)