Position fixed in multiple scroll areas #502
Xavier4022
started this conversation in
General
Replies: 1 comment 1 reply
-
3
for <body>
<section> <!-- wrap main-scrollbar -->
<div class="main-scrollbar-fixed"> <!-- fixed element -->
Normal CSS fixed property
</div>
<main id="main-scrollbar" data-scrollbar>
<div id="leftscroll" data-scrollbar>
Nested leftscroll SmoothScrollbar
<div class="leftscroll-fixed">...</div> <!-- fixed element -->
</div>
<div id="rightscroll" data-scrollbar>
Nested rightscroll SmoothScrollbar
<div class="rightscroll-fixed">...</div> <!-- fixed element -->
</div>
</main>
</section>
</body> .main-scrollbar-fixed, .leftscroll-fixed, .rightscroll-fixed {
position: fixed;
}
/* more info in js section (below) */
#leftscroll {
--leftscroll-position-top: 0px;
--leftscroll-position-left: 0px;
}
.letscroll-fixed {
top: var(--leftscroll-position-top);
left: var(--leftscroll-position-left)
} If you init scrollbar just like code you share you have to do like this: var mainElem = document.getElementById("main-scrollbar");
var innerElem1 = document.getElementById("leftscroll");
var innerElem2 = document.getElementById("rightscroll");
var innerElemOneFixed = document.querySelector(".letscroll-fixed");
Scrollbar.use(OverscrollPlugin);
const options = {
damping: 0.05,
renderByPixels: !('ontouchstart' in document),
};
const overscrollOptions = {
enable: false,
effect: navigator.userAgent.match(/Android/) ? 'glow' : 'bounce',
damping: 0.05,
maxOverscroll: navigator.userAgent.match(/Android/) ? 150 : 100,
glowColor: mainElem.dataset.glowColor,
};
const scrollbar = [
Scrollbar.init(mainElem, {
...options,
delegateTo: document,
plugins: {
overscroll: { ...overscrollOptions },
},
}),
Scrollbar.init(innerElem1, {
...options,
plugins: {
overscroll: { ...overscrollOptions },
},
}),
Scrollbar.init(innerElem2, {
...options,
plugins: {
overscroll: { ...overscrollOptions },
},
}),
];
// scrollbar[0] => main-scrollbar instance
scrollbar[1].addListener(function(status) { // => leftscroll instance
var offset = status.offset;
// fixed specific element
innerElemOneFixed .style.top = offset.y + 'px';
innerElemOneFixed .style.left = offset.x + 'px';
// if you want fixed multiple element use CSS variable instead
innerElem1.style.setProperty('--leftscroll-position-top', offset.y + 'px');
innerElem1.style.setProperty('--leftscroll-position-left', offset.x + 'px');
});
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Pardon this stupid question. I'm not a developer and don't know any Javascript.
I'm trying to add some position fixed elements to multiple scrollable areas. Found a working solution here, but I have no clue how to add this to my javascript. It would be great, if anyone could give me some advice.
This is the code to get the elements fixed:
And here is how my scrollbar settings look like:
(I grabbed this code from here)
How can I join these two scripts?
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions