-
Environment
Issue SummaryAccesing the webpage with a #link doesn't scroll to the section with that ID Expected BehaviorIf you go to the webpage.com/#someID the page would load and be positioned in the page section where that someID had been set. |
Beta Was this translation helpful? Give feedback.
Replies: 20 comments 1 reply
-
This feature is not suitable for this lib since it has no idea where you initialize scrollbar to. Also it has never been designed for root scrollbar replacement. You should implement this feature yourself. |
Beta Was this translation helpful? Give feedback.
-
Ok, noted😉 |
Beta Was this translation helpful? Give feedback.
-
Hint: this could be done with |
Beta Was this translation helpful? Give feedback.
-
What i did was basically
I prefer to used the scrollTo so i can add some offset in case i needed. |
Beta Was this translation helpful? Give feedback.
-
Actually you can also add offsets while using |
Beta Was this translation helpful? Give feedback.
-
Really ? i didn't see that in the Doc, awesome!!! |
Beta Was this translation helpful? Give feedback.
-
It does! Look at the "Details" section! |
Beta Was this translation helpful? Give feedback.
-
If you are adding anchors support, I'd suggest you also register a listener for |
Beta Was this translation helpful? Give feedback.
-
I didn't know that listener exists ! that gonna simplify the logic a lot ! refactor is coming haha thank you so much. |
Beta Was this translation helpful? Give feedback.
-
Hi again @idiotWu , in the past the behavior of the smooth-scrollbar was ignore the hash # // Scroll to if HASH element
// -------------------------
// where scroller is the smooth-scrollbar and touchDevice is a boolean
// because i use native scroll on touch devices.
const hash = window.location.hash;
if(!!hash) {
const id = hash.substring(1);
const target = document.getElementById(id);
if(!!target) {
const targetPosition = target.getBoundingClientRect();
if(!touchDevice) {
// Smooth-scrollbar scroll
setTimeout(() => {
scroller.scrollTo(0, targetPosition.top, 600);
}, 300);
}
}
} But now with the "smooth-scrollbar": "^8.2.5" version Sometimes the smoot-scrollbar ignore the hash, but others initialized the web with the hash element at the top, but as this element were the first one. web.com#someID Weird point is that it's behave randomly, sometimes ignore the hash ( which is perfect, i manage it later, but sometimes dont) As a temp fix i'm:
But i don't really like this temp fix because it's messing with the browser history init() {
const hash = window.location.hash;
if (!hash) {
window.scrollTo(0, 0);
}
history.pushState(null, null, window.location.pathname);
if (!!scrollbarWrapper) {
this.scroller = Scrollbar.init(scrollbarWrapper, options);
history.pushState(null, null, window.location.pathname + hash);
}
} |
Beta Was this translation helpful? Give feedback.
-
I think this issue is related to initializing orders. What about using plugin system: class AnchorPlugin extends ScrollbarPlugin {
static pluginName = 'anchor';
onInit() {
// go to anchor
}
} P.S. IMO P.P.S. the above const scrollbar = Scrollbar.init(elem, options);
requestAnimationFrame(() => {
// go to anchor
}); see smooth-scrollbar/src/scrollbar.ts Lines 204 to 207 in 8171ddd |
Beta Was this translation helpful? Give feedback.
-
import { ScrollbarPlugin } from 'smooth-scrollbar';
class AnchorPlugin extends ScrollbarPlugin {
static pluginName = 'anchor';
onHashChange = () => {
this.jumpToHash(window.location.hash);
};
onClick = (event) => {
const { target } = event;
if (target.tagName !== 'A') {
return;
}
const hash = target.getAttribute('href');
if (!hash || hash.charAt(0) !== '#') {
return;
}
this.jumpToHash(hash);
};
jumpToHash = (hash) => {
const { scrollbar } = this;
if (!hash) {
return;
}
// reset scrollTop
scrollbar.containerEl.scrollTop = 0;
scrollbar.scrollIntoView(document.querySelector(hash));
};
onInit() {
this.jumpToHash(window.location.hash);
window.addEventListener('hashchange', this.onHashChange);
this.scrollbar.contentEl.addEventListener('click', this.onClick);
}
onDestory() {
window.removeEventListener('hashchange', this.onHashChange);
this.scrollbar.contentEl.removeEventListener('click', this.onClick);
}
}
// usage
Scrollbar.use(AnchorPlugin); Demo: https://codesandbox.io/s/anchor-plugin-u6cy6?file=/src/anchor-plugin.js |
Beta Was this translation helpful? Give feedback.
-
Hey, I think I finally figured out the reason: when an anchor is clicked, So the fix is: // reset scrollTop...
scrollbar.containerEl.scrollTop = 0;
// ...then scroll to the target
scrollbar.scrollIntoView(document.querySelector(hash)); |
Beta Was this translation helpful? Give feedback.
-
Hello! For some reason in firefox, it anchors the anchor to position 0.
|
Beta Was this translation helpful? Give feedback.
-
I need to use the anchor reference method. I have a header and I want to navigate the page by clicking on these links. I have tried the method described above and when I refresh the page it immediately flips to a certain "hash". But how do I execute this event only when I click on the specified link? Tell me please |
Beta Was this translation helpful? Give feedback.
-
could you help with the question above? |
Beta Was this translation helpful? Give feedback.
-
Hi, can you please help me with the anchor links in the smoothscrollbar script? |
Beta Was this translation helpful? Give feedback.
-
Hi, I solved this problem by using the following code. Seems to be fine in smooth-scrollbar@8.5.3. Though if anyone can improve, kindly edit this code! // For newly opened link (e.g in new tab)
const hash = window.location.hash;
const scrollbar = Scrollbar.init(document.body, {
continuousScrolling: false,
alwaysShowTracks: true,
plugins: {}
});
if (hash) {
const target = document.getElementById(hash.substring(1));
if (target) {
scrollbar.scrollIntoView(target, {
offsetTop: -scrollbar.containerEl.scrollTop,
});
}
}
// For opening link in the same page
window.addEventListener('hashchange', function () {
const hash = window.location.hash;
if (hash) {
const target = document.getElementById(hash.substring(1));
if (target) {
scrollbar.scrollIntoView(target, {
offsetTop: -scrollbar.containerEl.scrollTop,
});
}
}
}, false); |
Beta Was this translation helpful? Give feedback.
-
Hi @masnormen, your solution ended my struggle: import {useEffect} from 'react';
import Scrollbar from 'smooth-scrollbar';
import OverscrollPlugin from 'smooth-scrollbar/plugins/overscroll';
let overscrollOptions = {
enable: true,
effect: 'bounce',
damping: 0.15,
maxOverscroll: 150,
};
let options = {
continuousScrolling: false,
alwaysShowTracks: true,
damping: 0.07,
plugins: {
overscroll: {
...overscrollOptions
}
}
};
const SmoothScroll = () => {
useEffect(() => {
Scrollbar.use(OverscrollPlugin);
// For newly opened link (e.g in new tab)
const hash = window.location.hash;
const scrollbar = Scrollbar.init(document.body, options);
if (hash) {
const target = document.getElementById(hash.substring(1));
if (target) {
scrollbar.scrollIntoView(target, {
offsetTop: -scrollbar.containerEl.scrollTop,
});
}
}
// For opening link in the same page
window.addEventListener('hashchange', function () {
const hash = window.location.hash;
if (hash) {
const target = document.getElementById(hash.substring(1));
if (target) {
scrollbar.scrollIntoView(target, {
offsetTop: -scrollbar.containerEl.scrollTop,
});
}
}
}, false);
return () => {
if (Scrollbar) {
Scrollbar.destroy(document.body)
}
};
}, []);
return null;
};
export default SmoothScroll; |
Beta Was this translation helpful? Give feedback.
-
what worked in the sandbox didn't work in my project. (mb because of the -17vs18 version of the react, mb because of gatsby or my crooked hands, or maybe moon in capricorn).
index.jsx:
my package.json
|
Beta Was this translation helpful? Give feedback.
AnchorPlugin
: