electerious / basicScroll

Standalone parallax scrolling for mobile and desktop with CSS variables.
https://basicscroll.electerious.com
MIT License
3.63k stars 148 forks source link

Programmatically update to/from values #41

Closed JiveDig closed 4 years ago

JiveDig commented 5 years ago

I have an animation that starts near the top of the window and works great. However, if a user loads the page already scrolled down (via an anchor link or page refresh or something) I want the animation to start lower the first time, but after some other interaction I want to change the to/from values to the original location.

Pardon my terrible scenario explanation.

Here's some code that shows a little clearer what I hope to do:

// Create the instance.
var instance = basicScroll.create({
    elem: someElement,
    from: 'top-top',
    to: 'bottom-bottom',
    props: { some props here },
});

// If loading with window at the top, start it up.
if ( document.documentElement.scrollTop <= 0 ) {
    instance.start();
}

// some other event happens here.
element.addEventListener( 'some-event', function() {
    // Change the from/to.
    instance.from = '200px';
    instance.to   = '400px';
    instance.calculate();
    instance.update();
    instance.start();
});
electerious commented 5 years ago

It's not possible to update the values of an instance. You could try to create two instances: One starting at the original location and one at the scroll location. Then activate or deactivate them using .start() or .stop().

You could also try to track another element using direct: document.querySelector('.someElem'). Then modify the position of this element and call .calculate() and .update() to recalculate and update the instance.