Coder Social home page Coder Social logo

Comments (11)

ctf0 avatar ctf0 commented on July 24, 2024 62

here is a solution

let item = // what we want to scroll to
let wrapper = // the wrapper we will scroll inside
let count = item.offsetTop - wrapper.scrollTop - xx // xx = any extra distance from top ex. 60
wrapper.scrollBy({top: count, left: 0, behavior: 'smooth'})

from smoothscroll.

adamthedorsey avatar adamthedorsey commented on July 24, 2024 27

This is an old thread but I just came across it while working out my own solution. Using scroll() instead of scrollBy() seems to make a lot more sense to me.

I simply did...

var offset = 50; // sticky nav height
var el = document.querySelector('.some-element'); // element you are scrolling to
window.scroll({ top: (el.offsetTop - offset), left: 0, behavior: 'smooth' });

from smoothscroll.

13lackNinja avatar 13lackNinja commented on July 24, 2024 18

In case anyone's still searching, I got around this issue with a fixed position header by setting the { block: 'end' } option for my element in element.scrollIntoView(options).

This conveniently lines it up based on the bottom of the element rather than the top, and you can take care of the height of the element yourself by simply subtracting the height of your header or other fixed element.

from smoothscroll.

naumantahir1 avatar naumantahir1 commented on July 24, 2024 6

That helped in my case —


HTML

<div class='stick-to-top'></>

Javascript

document.getElementsByClassName('stick-to-top')[0].scrollIntoView({behavior: "smooth", block: "center"});

CSS

.stick-to-top {
 padding-bottom: 400px;
}

from smoothscroll.

joshuatuscan avatar joshuatuscan commented on July 24, 2024 1

I'd also love this, or some way of having a callback when animation is finished to be able to re-adjust the element.

from smoothscroll.

jeremenichelli avatar jeremenichelli commented on July 24, 2024 1

If there are some element floating that will imply the scrolling need to be readjust you need to manually get the y position of the elements and use window.scroll with the resulting metrics gathered.

from smoothscroll.

themaxsandelin avatar themaxsandelin commented on July 24, 2024

On @joshuatuscan comment, I would absolutely love a callback for when the scroll movement is completely done. 👌🏻

from smoothscroll.

jeremenichelli avatar jeremenichelli commented on July 24, 2024

People, remember this is a polyfill and not a library. Unless a callback is added to the spec is not likely to make it in the code.

The best option is to listen to the scroll event and check for the final y position to execute some call and later remove the listener until a new smooth scroll ocurrence.

from smoothscroll.

themaxsandelin avatar themaxsandelin commented on July 24, 2024

It's a good suggestion but does not apply to any sort of website with fixed height content as this would break when tested on larger devices with smaller scrolling distance.

from smoothscroll.

oscarhandsome avatar oscarhandsome commented on July 24, 2024

somebody know how to do? or need use offset and animation to move from top?

from smoothscroll.

jeremenichelli avatar jeremenichelli commented on July 24, 2024

Closing since the feature requested is not included in the official specs. If you find a use case that works on native implementations and not with the polyfill feel free to report it. Thanks!

from smoothscroll.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.