Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pause and resume methods #58

Open
ghost opened this issue May 18, 2017 · 11 comments
Open

pause and resume methods #58

ghost opened this issue May 18, 2017 · 11 comments

Comments

@ghost
Copy link

ghost commented May 18, 2017

Maybe you could add pause and resume methods. This could already do the trick:

    self.resume = function () {
        pause = false;  
    };

    self.pause = function () {
        pause = true;
    };
@codymx
Copy link

codymx commented May 19, 2017

I agree!

Right now I destroy the rellax element on smaller viewports and it'd be great to have a vanilla way of recreating the rellax when the browser viewport sizes back up.

I've tried a few things, but binding new Rellax('.rellax', {}); to a $(window).resize function causes it to keep creating instances every time the viewport is resized, pushing my elements up and down the page more and more as you resize.

@ghost
Copy link
Author

ghost commented Oct 25, 2017

@c0mrx May I know why you destroy rellax on smaller viewports?

@codymx
Copy link

codymx commented Oct 25, 2017

@Glumanda99 The elements were moving too much and covering more important information, so for UX reasons I've disabled it on smaller screens.

@ghost
Copy link
Author

ghost commented Oct 25, 2017

@c0mrx Thanks for your fast response! I'm not sure if I'm gonna have the same issues while designing, but I could imagine to remove the rellax class on smaller viewports and add them back on all data-rellax attributes when appropriate. Need to try this..

@ghost
Copy link
Author

ghost commented Oct 25, 2017

I think I also need to reset translate3d every time I do this.

@codymx
Copy link

codymx commented Oct 25, 2017

@Glumanda99 I ended up manipulating the speed attribute. It's not the cleanest way to do it, but it does work.

  // Init Rellax.js after all elements have fully loaded so that all heights of DOMs are calculated
  window.onload = function() {
    var rellax = new Rellax('.rellax', {
      center: true
    });
    // 'Disable' rellax on mobile
    function checkWidth() {
      var windowSize = $(window).innerWidth();
      if (windowSize <= 960) {
        $('.rellax').attr('data-rellax-speed', '0');
        rellax.refresh();
      } else {
        rellax.refresh();
        $('.even.rellax').attr('data-rellax-speed', '-1');
        $('.odd.rellax').attr('data-rellax-speed', '1');
      }
    }
    // Execute on load
    checkWidth();
    // Refresh whenever window is resized.
    $(window).resize(function() {
      checkWidth();
    });
  };

@captainfromspace
Copy link

captainfromspace commented Jan 22, 2018

@c0mrx, in your code example above you are calling rellax.refresh(), is that a custom method you've written? I need to refresh the rellax after an AJAX-call that adds content, but destroying and re-initializing it is not working that good[1].

[1] - #91

@codymx
Copy link

codymx commented Jan 22, 2018

@captainfromspace I believe it may be in one of the forks of this plugin. Either way, the function was added before line 284 of the plugin and is as follows:

self.refresh = function() {
      self.destroy();
      screenY = window.innerHeight;
      blocks = [];
      cacheBlocks();
      animate();
      pause = false;
    };

@sergeymorkovkin
Copy link

sergeymorkovkin commented Feb 21, 2019

I'm trying to use Rellax with the OnScreen. This is mainly required to achieve acceptable page performance. Therefore, I want to pause/suspend all rellax instances except for those that are visible in the viewport.

Unfortunately, I couldn't achieve this without copying rellax src code and patching it.

My recommendation is implementing two methods: attach() and detach(). All it should do is:

  1. Stop requestAnimationFrame loop;
  2. Detach window.onresize handler;

All this should be done WITHOUT reverting initial element styles. Otherwise it'll all jump-flicker.

In my implementation it's as simple as:

`Rellax.prototype.attach = function() {
if (this.pause) {
this.pause = false;
window.addEventListener('resize', this.initHandler);
this.update();
}
};

Rellax.prototype.detach = function() {
if (!this.pause) {
window.removeEventListener('resize', this.initHandler);
window.cancelAnimationFrame(this.loopId);
this.pause = true;
this.loopId = null;
}
};`

@zachtownsend
Copy link

Here's what I've used to make it run responsively:

const getRellaxSpeed = () => $window.width() < 768 ? 0 : -7;
const rellax = new Rellax('.rellax', {
    speed: getRellaxSpeed(),
});

let throttle = null;

$window.resize(() => {
    clearTimeout(throttle);

    throttle = setTimeout(() => {
        rellax.options.speed = getRellaxSpeed();
        rellax.refresh();
    }, 400);
});

(The throttle thing is not necessary, but you get the idea)

@sergeymorkovkin
Copy link

Here's what I've used to make it run responsively:

Yeah, that looks good for your goals. In my situation, however, I need to reduce the use of animation frames. Otherwise it makes smooth scrolling jerky. So, I still need detach/attach. Preferrably, persisting Rellax instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants