Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Created November 27, 2024 13:49
Show Gist options
  • Select an option

  • Save igorbenic/8d3f250803fae6ae7096cdf4c182943d to your computer and use it in GitHub Desktop.

Select an option

Save igorbenic/8d3f250803fae6ae7096cdf4c182943d to your computer and use it in GitHub Desktop.
Horizontal Scroll Native
function nativeHorizontalScroll( id ) {
var elID = id;
var element = document.getElementById( id );
var scrolling = false;
var initialX = 0;
function getParent( node, id ) {
if ( node.id === id ) {
return node;
}
if ( ! node.parentElement ) {
return null;
}
return getParent( node.parentElement, id );
}
function scroll( initial, changed ) {
var diff = (-1.25) * ( changed - initial );
var currentScroll = element.scrollLeft;
var maxScroll = element.scrollWidth - element.clientWidth;
if ( currentScroll === 0 && diff < 0 ) {
return;
}
currentScroll = currentScroll + diff;
if ( currentScroll < 0 ) {
currentScroll = 0;
}
if ( currentScroll > maxScroll ) {
currentScroll = maxScroll;
}
initialX = changed;
element.scroll( currentScroll, element.scrollTop );
}
function disableSelect(event) {
event.preventDefault();
}
addEventListener("mousemove", (event) => {
if ( ! scrolling ) {
return;
}
window.addEventListener('selectstart', disableSelect);
scroll( initialX, event.clientX );
});
addEventListener("mouseup", (event) => {
if ( scrolling ) {
window.removeEventListener('selectstart', disableSelect);
scrolling = false;
}
})
addEventListener("mousedown", (event) => {
var target = event.target || event.originalTarget;
if ( ! getParent( target, elID ) ) {
return;
}
scrolling = true;
initialX = event.clientX;
});
}
@huria1057-jpg
Copy link
Copy Markdown

It’s interesting how code snippets like this highlight the importance of clean structure and efficiency when building or modifying functionality.
Even small tweaks in logic can significantly improve usability and overall experience for users interacting with a system.AD786 Free Game is a reliable real earning app for users in Pakistan who want more than just casual gaming, bringing together engaging gameplay with a focus on simplicity, transparency, and a localized experience for users.

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