Comments (15)
I have a better idea: polyfill all links with href
starting with #
import smoothscroll from 'smoothscroll-polyfill'
if (CSS.supports('scroll-behavior', 'smooth')) return
smoothscroll.polyfill()
const links = document.querySelectorAll('a[href^="#"]')
links.forEach(link => {
const id = link.attributes['href'].value.slice(1)
const target = document.getElementById(id)
link.addEventListener('click', () => {
target.scrollIntoView({ behavior: 'smooth' })
})
})
from smoothscroll.
For those still stumbling across this issue:
If you just want the scroll-behavior
CSS support to make anchor links pointing at an ID scroll smoothly (as is common for one pager designs), I made a package to help with this some time ago, which can be used in combination with this polyfill. :)
https://github.com/jonaskuske/smoothscroll-anchor-polyfill
It won't automatically add smooth scrolling when using the JS APIs, so you still need to pass { behavior: 'smooth' }
when doing that, but all <a href="#id">
anchors on the page will scroll smoothly. (and navigation between anchors using the browser's backwards/forwards buttons too)
from smoothscroll.
+1 this would be awesome.
from smoothscroll.
This would be great.
You can base this on: https://github.com/philipwalton/polyfill
from smoothscroll.
+1
from smoothscroll.
+1
from smoothscroll.
I think this makes more sense as a PostCSS plugin more as a JavaScript polyfill. Let me know if you agree with me @iamdustan and we close this.
from smoothscroll.
I don’t think this makes sense as a PostCSS plugin. Specifically, PostCSS only handles transforming CSS source to CSS source, but until Houdini starts landing there is nothing that can be done on that front.
I think the only practical way to support this is by:
- require the style to by declared inline
<div style="scroll: smooth" />
- test for that existence when any
scroll
method is called. - take smooth or standard path accordingly.
The reason for requiring it inline is because without it we’d have to capture and parse every CSS file and declaration used on a given webpage to behave correctly (since getComputedStyle(element)
doesn’t include unknown declarations). Whilst that is more correct it also is a large cost to the user just to detect whether the developer wanted to use smooth scrolling.
from smoothscroll.
I was thinking on Houdini being an immediate need for this to actually work too. I think the inline style thing can be easily done, though I don't know if worth since inline is not the way devs prefer to style elements in the major of cases.
from smoothscroll.
On using inline style
: https://github.com/philipwalton/polyfill is designed to handle css parsing for polyfills.
from smoothscroll.
even though that library would make it rather trivial from our perspective, it still has the cost of doing it. I don’t personally think that that is a really good default behavior.
from smoothscroll.
fwiw this isn’t a novel idea either. the pointer-events polyfill takes a similar stance for performance reasons. https://github.com/jquery/PEP#touch-action
from smoothscroll.
How about partially shimming this with a different function: polyfillAll()
This function would run the normal polyfill()
, but add "click" or "hashchange" event listeners for smooth-scrolling all links to on-page anchors.
from smoothscroll.
Any update on this?
from smoothscroll.
I've investigated and there's no way we can detect the scroll-behavior: smooth
property on CSS, since the browser doesn't parse properties it unknows and honestly I don't see a big gain in adding support for inline style
attribute detection.
If someone has an idea on how to properly do this (no hack) please stand up :)
from smoothscroll.
Related Issues (20)
- smoothscroll no working when added @media query to document HOT 1
- Newest version isn't published to NPM because of that Cloudflair CDN can't get newest version HOT 2
- Body can't be used as scroll-container HOT 4
- Smoothscroll Flickering Page Elements and too fast on Safari 13.0.5 HOT 2
- Make scrollIntoView work for Safari and SVGElement HOT 4
- smooth scroll is not working in chrome version>=81 HOT 2
- [Feature Request] Padding option HOT 2
- Doesn't work with Safari CSS Scroll Snap HOT 3
- window.scrollTo with behavior: 'smooth' doesn't scroll to the top of the page with safari
- Check for existence of window to support SSR HOT 3
- null as the first argument causing a TypeError
- smoothscroll from <script> tag not working HOT 1
- Accessibility: respect prefers-reduced-motion
- Not working with window.parent? HOT 1
- Support for `scrollTop` & `scrollLeft` for Elements
- scrollIntoView with arguments is not working HOT 3
- Polyfill not working on Safari 15 (iOS & macOS) HOT 24
- Cannot scroll center for scrollIntoView in iOS HOT 4
- Support scrollBehavior after connecting the polyfill
- undefined is not an object (evaluating 'e.clientHeight')
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from smoothscroll.