Simplify scroll jump prevention documentation
This commit is contained in:
parent
250a06a032
commit
2935b5f4ba
1 changed files with 3 additions and 7 deletions
|
|
@ -321,24 +321,20 @@ The solution temporarily removes the hash from the URL during theme toggle, then
|
|||
.. code-block:: javascript
|
||||
|
||||
function toggleTheme() {
|
||||
// Save current scroll position and hash
|
||||
const scrollY = window.scrollY;
|
||||
// Save hash and temporarily remove to prevent jump
|
||||
const hash = window.location.hash;
|
||||
|
||||
// Temporarily remove hash to prevent jump
|
||||
if (hash) {
|
||||
history.replaceState(null, null, ' ');
|
||||
}
|
||||
|
||||
// ... perform theme toggle ...
|
||||
|
||||
// Restore scroll position and hash without jumping
|
||||
window.scrollTo(0, scrollY);
|
||||
// Restore hash without jumping
|
||||
if (hash) {
|
||||
history.replaceState(null, null, hash);
|
||||
}
|
||||
}
|
||||
|
||||
This preserves both the scroll position and the URL anchor without triggering the browser's default anchor-jumping behavior.
|
||||
By removing the hash before theme changes, the browser has no anchor to jump to during layout reflow. The hash is restored afterward, preserving the URL without triggering the browser's default anchor-jumping behavior.
|
||||
|
||||
.. contents::
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue