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

Apply CSS transform using direct mode (without CSS vars) #25

Closed frankharrison-wj closed 6 years ago

frankharrison-wj commented 6 years ago

Is it possible to apply a CSS transform using direct mode? I want to add a CSS translate directly to an element, and could obviously do that with CSS variables, but since I'm adding it directly (not globally) it'd be nice to do it the old-fashioned way (style="transform: translateY(10rem)") to get IE11 support.

I tried:

direct: true,
props: {
    'translateY': {
        from: '0',
        to: '10rem'
    }
}

and

direct: true,
props: {
    'transform': {
        from: 'translateY(0)',
        to: 'translateY(10rem)'
    }
}

... but neither of those seem to work.

electerious commented 6 years ago

You can use the inside callback to apply styles manually:

direct: true,
props: {
    'value': {
        from: '0',
        to: '10rem'
    }
},
inside: (instance, percentage, props) => {
    console.log(props.value) // 0rem - 10rem
}

Explanation why the two tests above don't work:

direct: true,
props: {
    'translateY': {
        from: '0',
        to: '10rem'
    }
}

This will generate CSS like: transformY: 2rem

direct: true,
props: {
    'transform': {
        from: 'translateY(0)',
        to: 'translateY(10rem)'
    }
}

This won't work because translateY(0) is not a value basicScroll can interpolate.