baptistebriel / smooth-scrolling

smooth scrolling and parallax effects on scroll
MIT License
612 stars 75 forks source link

Hiding the scroll thumb when content doesn't extend beyond viewport. #91

Closed 23d1 closed 3 years ago

23d1 commented 6 years ago

Is there a way to hide the scrollbar/thumb when the content/scroll-helper-div doesn't expand beyond the viewport (non-native of course)?

I feel really thick asking all these questions, which probably shouldn't be posted as issues on Github. Sorry! :)

baptistebriel commented 6 years ago

Ah — that's indeed a use case I didn't think of. I guess on your resize function you could check smooth.vars.bounding, and if this value is < than smooth.vars.height (equals window.innerHeight) then you could hide the scrollbar using visibility: hidden or display: none

Hope it helps!

23d1 commented 6 years ago

Thanks! Would be great with the .on() and .off() as well (perhaps just adding a class when it meets those different criteria).

Definitely a feature request.

I should probably open another ticket, but I feel like I'm already spamming. ;) Adding a Math.round enable option for the scroll values would be rad, like { fullpixels: true } or something.

baptistebriel commented 6 years ago

Hey @23d1,

Yup, that's definitely something we could add to the on/off methods.

As for rounding the pixels upon transformation, that's something I currently do most of the times now. I still use sub-pixels during transform to make sure the scroll is as smooth as possible, but usually rounds up the value when the user stops scrolling to avoid sub-pixel images and stuff.

If you're extending smooth and applying your own transform, you could use something like this: this.getTransform(-this.vars.current.toFixed()). It currently use a value of 2 if you're using the default script without extending it.

I'll think about adding this feature when I get some time to do it. There's actually a lot of stuff I'd like to add to a new version, I just didn't had time to re-factor it and push a new version with up-to-date documentation.

weotch commented 5 years ago

In case it helps anyone, here's my subclass to add toggling of the scrollbar when it's not needed:

class SmoothCustom extends Smooth {

  // Keep the original behavior that gets turned off when you extend
  constructor(opt) {
    super(opt);
    this.extends = false;
  }

  // Toggle display of scrollbar depending on whether content is > viewport
  resize() {
    super.resize();
    if (this.vars.bounding <= 0) {
      return this.dom.scrollbar.el.classList.add('hide');
    } else {
      return this.dom.scrollbar.el.classList.remove('hide');
    }
  }

};