Stop scroll on x axis #511
-
Hey guys, Just need some help on figuring out how to disable the scrolling on the X-Axis without the NPM install. Currently just using the plain js file and importing through unpkg.com. Any help would be nice! Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 10 replies
-
Hi check this #481 (comment) window.myScrollbar = Scrollbar.init(document.getElementById('#scrollbar'))
const setMomentum = myScrollbar.prototype.setMomentum;
myScrollbar.prototype.setMomentum = function(x, y) {
setMomentum.call(this, 0, y);
} html {
scroll-behavior: initial;
}
html, body {
height: 100%;
}
body
position: relative;
overflow: hidden;
}
#scrollbar {
width: 100%;
height: 100vh;
height: -webkit-fill-available;
} |
Beta Was this translation helpful? Give feedback.
-
This workaround is based on Cuberto plugins, and should be work class DisableScrollPlugin extends ScrollbarPlugin {
static pluginName = 'disableScroll';
static defaultOptions = {
direction: null;
}
transformDelta(delta) {
if(this.options.direction) {
delta[this.options.direction] = 0;
}
return { ...delta };
}
}
window.myScrollbar = Scrollbar.init(document.getElementById('#scrollbar'), {
damping: 0.2,
plugins: {
disableScroll: { direction: 'x' }
}
}) or you can use another Cuberto plugin called class SoftScrollPlugin extends Scrollbar.ScrollbarPlugin {
static pluginName = 'SoftScroll';
transformDelta(delta, fromEvent) {
const dirX = delta.x > 0 ? 1 : -1;
const dirY = delta.y > 0 ? 1 : -1;
if (dirX === this.lockX || dirY === this.lockY) {
return {x: 0, y: 0};
} else {
this.lockX = null;
this.lockY = null;
}
return delta;
}
onRender(Data2d) {
const {x, y} = Data2d;
// Up
if (y < 0 && !this.lockY && Math.abs(y) >= this.scrollbar.scrollTop) {
this.scrollbar.setMomentum(0, -this.scrollbar.scrollTop);
this.lockY = -1;
}
// Left
if (x < 0 && !this.lockX && Math.abs(x) >= this.scrollbar.scrollLeft) {
this.scrollbar.setMomentum(-this.scrollbar.scrollLeft, 0);
this.lockX = -1;
}
// Right
if (x > 0 && !this.lockX && Math.abs(x) >= (this.scrollbar.limit.x - this.scrollbar.scrollLeft)) {
this.scrollbar.setMomentum((this.scrollbar.limit.x - this.scrollbar.scrollLeft), 0);
this.lockX = 1;
}
// Down
if (y > 0 && !this.lockY && Math.abs(y) >= (this.scrollbar.limit.y - this.scrollbar.scrollTop)) {
this.scrollbar.setMomentum(0, (this.scrollbar.limit.y - this.scrollbar.scrollTop));
this.lockY = 1;
}
if(y === 0) this.lockY = null;
if(x === 0) this.lockX = null;
}
}
Scrollbar.use(SoftScroll)
window.myScrollbar = Scrollbar.init(...) |
Beta Was this translation helpful? Give feedback.
-
I found a temp sloppy fix, you can set scrollbar.limit.x to 0 after initializing the scroll bar, then add a event listener for scroll and set the limit to 0 in that as well. Seems to do the trick, although is definitely sloppy. |
Beta Was this translation helpful? Give feedback.
I found a temp sloppy fix, you can set scrollbar.limit.x to 0 after initializing the scroll bar, then add a event listener for scroll and set the limit to 0 in that as well. Seems to do the trick, although is definitely sloppy.