dixonandmoe / rellax

Lightweight, vanilla javascript parallax library
https://dixonandmoe.com/rellax/
MIT License
7.04k stars 880 forks source link

defining multiple rellax classes via js #218

Open joehdevwp opened 3 years ago

joehdevwp commented 3 years ago

I am trying to globally define many different rellax classes rather than use html data attributes, so that I can change them globally. However due to the need to destroy for mobile and refresh, I am hoping to use one variable and create an array of many classes each with different speed settings, like this:

let rellax = [new Rellax ('.one.has-parallax', { speed: 7, center: true, wrapper: null, round: true, vertical: true, horizontal: false, })], [new Rellax ('.two.has-parallax', { speed: 2, center: true, wrapper: null, round: true, vertical: true, horizontal: false, })];

But this breaks it. Any help? thank you.

Or maybe someone can suggest a smarter way to do this.

zangab commented 3 years ago

if you would do it with the html data attributes, you would save some bytes because you don't bloat the js unneccessarily. but i guess i would define a config array with objects and then loop over all:

const relaxConfigs = [
  {
    selector: '.one.has-parallax',
    options: {
      speed: 7,
      center: true,
      ...
    },
  },
  {
    selector: '.two.has-parallax',
    options: {
      speed: 2,
      center: true,
      ...
    },
];

relaxConfigs.forEach(config => new Rellax(config.selector, config.options));
joehdevwp commented 3 years ago

thank you @zangab