(Created page with "$(document).ready(function() { // Select elements with the scroll-trigger class $('.sidebar-heading.one').click(function() { // Find the scrollable container var container = $('#topbar'); // Scroll to the top of the container (position 0) container.animate({ scrollTop: 0 }, 300); // 300ms animation duration }); }); $(document).ready(function() { // Select elements with the scroll-trigger class $('.sidebar-heading.two').click(func...") |
No edit summary |
||
| Line 1: | Line 1: | ||
$(document).ready(function() { | $(document).ready(function() { | ||
// Get element's position relative to the viewport | |||
function getElementTopPosition(selector) { | |||
var element = $(selector); | |||
if (element.length) { | |||
var elementPosition = element.offset().top; | |||
var scrollTop = $(window).scrollTop(); | |||
var positionInViewport = elementPosition - scrollTop; | |||
// Store the position for later use | |||
var storedPosition = elementPosition; | |||
console.log('Element position stored:', storedPosition); | |||
return storedPosition; | |||
} | |||
return 0; | |||
} | |||
// Store positions when page loads | |||
var onePosition = getElementTopPosition('.sidebar-heading.one'); | |||
var twoPosition = getElementTopPosition('.sidebar-heading.two'); | |||
var threePosition = getElementTopPosition('.sidebar-heading.three'); | |||
// Select elements with the scroll-trigger class | // Select elements with the scroll-trigger class | ||
$('.sidebar-heading.one').click(function() { | $('.sidebar-heading.one').click(function() { | ||
| Line 7: | Line 36: | ||
// Scroll to the top of the container (position 0) | // Scroll to the top of the container (position 0) | ||
container.animate({ | container.animate({ | ||
scrollTop: | scrollTop: onePosition - 202 | ||
}, 300); // 300ms animation duration | }, 300); // 300ms animation duration | ||
}); | }); | ||
$('.sidebar-heading.two').click(function() { | |||
// Find the scrollable container | // Find the scrollable container | ||
var container = $('#topbar'); | var container = $('#topbar'); | ||
| Line 20: | Line 46: | ||
// Scroll to the top of the container (position 0) | // Scroll to the top of the container (position 0) | ||
container.animate({ | container.animate({ | ||
scrollTop: | scrollTop: twoPosition - 202 | ||
}, 300); // 300ms animation duration | }, 300); // 300ms animation duration | ||
}); | }); | ||
// Select elements with the scroll-trigger class | |||
$('.sidebar-heading.three').click(function() { | $('.sidebar-heading.three').click(function() { | ||
// Find the scrollable container | // Find the scrollable container | ||
| Line 33: | Line 57: | ||
// Scroll to the top of the container (position 0) | // Scroll to the top of the container (position 0) | ||
container.animate({ | container.animate({ | ||
scrollTop: | scrollTop: threePosition - 202 | ||
}, 300); // 300ms animation duration | }, 300); // 300ms animation duration | ||
}); | }); | ||
}); | }); | ||
Revision as of 17:35, 28 February 2025
$(document).ready(function() {
// Get element's position relative to the viewport
function getElementTopPosition(selector) {
var element = $(selector);
if (element.length) {
var elementPosition = element.offset().top;
var scrollTop = $(window).scrollTop();
var positionInViewport = elementPosition - scrollTop;
// Store the position for later use
var storedPosition = elementPosition;
console.log('Element position stored:', storedPosition);
return storedPosition;
}
return 0;
}
// Store positions when page loads
var onePosition = getElementTopPosition('.sidebar-heading.one');
var twoPosition = getElementTopPosition('.sidebar-heading.two');
var threePosition = getElementTopPosition('.sidebar-heading.three');
// Select elements with the scroll-trigger class
$('.sidebar-heading.one').click(function() {
// Find the scrollable container
var container = $('#topbar');
// Scroll to the top of the container (position 0)
container.animate({
scrollTop: onePosition - 202
}, 300); // 300ms animation duration
});
$('.sidebar-heading.two').click(function() {
// Find the scrollable container
var container = $('#topbar');
// Scroll to the top of the container (position 0)
container.animate({
scrollTop: twoPosition - 202
}, 300); // 300ms animation duration
});
// Select elements with the scroll-trigger class
$('.sidebar-heading.three').click(function() {
// Find the scrollable container
var container = $('#topbar');
// Scroll to the top of the container (position 0)
container.animate({
scrollTop: threePosition - 202
}, 300); // 300ms animation duration
});
});
