JavaScript - window.scroll({ behavior: 'smooth' }) not working in Safari
Behavior options aren't fully supported in IE/Edge/Safari, so you'd have to implement something on your own. I believe jQuery has something already, but if you're not using jQuery, here's a pure JavaScript implementation:
function SmoothVerticalScrolling(e, time, where) {
var eTop = e.getBoundingClientRect().top;
var eAmt = eTop / 100;
var curTime = 0;
while (curTime <= time) {
window.setTimeout(SVS_B, curTime, eAmt, where);
curTime += time / 100;
}
}
function SVS_B(eAmt, where) {
if(where == "center" || where == "")
window.scrollBy(0, eAmt / 2);
if (where == "top")
window.scrollBy(0, eAmt);
}
And if you need horizontal scrolling:
function SmoothHorizontalScrolling(e, time, amount, start) {
var eAmt = amount / 100;
var curTime = 0;
var scrollCounter = 0;
while (curTime <= time) {
window.setTimeout(SHS_B, curTime, e, scrollCounter, eAmt, start);
curTime += time / 100;
scrollCounter++;
}
}
function SHS_B(e, sc, eAmt, start) {
e.scrollLeft = (eAmt * sc) + start;
}
And an example call is:
SmoothVerticalScrolling(myelement, 275, "center");
Is there a Safari equivalent for scroll-behavior: smooth;?
Safari does not support scroll-behavior: smooth
, you'll need some custom javascript to achieve the same effect. See this: Javascript - window.scroll({ behavior: 'smooth' }) not working in Safari
element.scrollTo smooth scrolling not working in Safari in snappable scrollable container
This behavior is apparently possible, but behind a flag. I'm confused though by what is mentioned at Can I Use about scrollTo.
This is the info I found at WebKit
http://bugs.webkit.org/show_bug.cgi?id=188043
In the end, I used the 'smoothscroll-polyfill' polyfill that I dynamically import. (https://github.com/iamdustan/smoothscroll) :(
if (!('scrollBehavior' in document.documentElement.style)) {
import('smoothscroll-polyfill').then((module) => {
module.polyfill();
}).then(() => {
...
});
}
How can I smooth scroll on Safari when <a> clicked?
Safari does not support scroll-behavior: smooth;
To implement smooth scrolling for internal anchor links, add the following slice of vanilla JavaScript (no jQuery required!):
(function() {
scrollTo();
})();
function scrollTo() {
const links = document.querySelectorAll('.scroll');
links.forEach(each => (each.onclick = scrollAnchors));
}
function scrollAnchors(e, respond = null) {
const distanceToTop = el => Math.floor(el.getBoundingClientRect().top);
e.preventDefault();
var targetID = (respond) ? respond.getAttribute('href') : this.getAttribute('href');
const targetAnchor = document.querySelector(targetID);
if (!targetAnchor) return;
const originalTop = distanceToTop(targetAnchor);
window.scrollBy({ top: originalTop, left: 0, behavior: 'smooth' });
const checkIfDone = setInterval(function() {
const atBottom = window.innerHeight + window.pageYOffset >= document.body.offsetHeight - 2;
if (distanceToTop(targetAnchor) === 0 || atBottom) {
targetAnchor.tabIndex = '-1';
targetAnchor.focus();
window.history.pushState('', '', targetID);
clearInterval(checkIfDone);
}
}, 100);
}
After including that code snippet, any anchor link with a class of .scroll
will scroll to the target. No modifications are required, just drop it in.
Since you have used the class .nav-link
for <a>
tag you can replace scroll in the first line of the scrollTo()
function to your anchor class name which is .nav-link
.
Official source of answer
also do check whether you href
is working fine?
How to enable smooth scrolling on mobile iOs?
Unfortunately Smooth Scrolling is not possible in Safari on OSX and iOS natively. But there are some JavaScript polyfills for that. For example: https://github.com/iamdustan/smoothscroll
You have to initialize it with JavaScript, not with CSS.
Related Topics
Validation Using Yup to Check String or Number Length
Scrollable Table With Fixed Headers and Fixed First Column
How to Iterate Through Json Nested Objects With Ngfor
Converting Text to Unicode in JavaScript
Remove Double Quotes from Array in Angularjs
React: How to Inject React Component into Body
Use Localstorage Across Subdomains
How to Set Top and Bottom Margin in Addhtml
Angular 4 | Window.Scrollto(); Is Not Working Properly
Background-Image in React Component
Eslint: Disable Warning - 'Defined But Never Used' for Specific Function
Discord Bot Not Getting All Users
How to Make an Empty String Array in Typescript
Setting the Value of a Checkbox Input If Not Checked
How to Increment the Elements of an Array by a Desired Value in JavaScript
How to Create a Dynamic Jquery Data Table With Json Data and Ajax