You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[feature] Scroll speed according to distance from edge #1907
Currently scroll speed is either 0 or the specified scrollSpeed if dragged item is near to edge.
Requested feature is that scroll speed will increase gradually according to the distance to the edge. If it's closer, it will be faster.
This can be done probably with scrollFn property but it would be much better if there were some built in functions like 'linear', 'ease-in', 'ease-out' etc.
@gap777 Well, I made something work-around implementation that scroll speed increases gradually. It also considers the mouse position outside of window. If u need it, just adjust some codes below
constonStart=(e)=>{startScrollFn(e);};constonEnd=(e)=>{initScrollVars();};typeDirection="up"|"down"|"right"|"left";letpointerId: number;lettargetScrollEl: HTMLElement|null;letlastPointerEvent: PointerEvent;letinterval;letscrollSpeed=3;constspeedIncreaseFactor=0.001;letlastMoveDirection: Direction;constinitScrollSpeed=()=>(scrollSpeed=3);constonMouseMove=(event: PointerEvent)=>{if(targetScrollEl){lastPointerEvent=event;}};conststartScrollFn=(e)=>{constcontext=e.fromasHTMLDivElement;pointerId=e.originalEvent.pointerId;targetScrollEl=getClosestScrollableEl(context);if(targetScrollEl){targetScrollEl.setPointerCapture(pointerId);targetScrollEl.addEventListener("pointermove",onMouseMove);clearInterval(interval);interval=setInterval(()=>{scrollFn(lastPointerEvent,targetScrollEl);});}};constinitScrollVars=()=>{if(targetScrollEl?.hasPointerCapture(pointerId)){targetScrollEl?.releasePointerCapture(pointerId);}targetScrollEl?.removeEventListener("pointermove",onMouseMove);targetScrollEl=null;initScrollSpeed();clearInterval(interval);};constscrollFn=(e: PointerEvent,container: HTMLElement|null)=>{if(!container||!e)returnfalse;targetScrollEl=container;const{ moveDirection, x, y }=getScrollResult(container,e);container.scrollBy(x,y);if(e!==lastPointerEvent){scrollSpeed=round(scrollSpeed+speedIncreaseFactor,3);}if(moveDirection!==lastMoveDirection){initScrollSpeed();}if(moveDirection)lastMoveDirection=moveDirection;returnfalse;};constgetScrollResult=(container: HTMLElement,e: PointerEvent)=>{letmoveDirection: Direction|null=null;letx=0;lety=0;constscrollSensitivity=props.option?.scrollSensitivity||50;const{ top, bottom, left, right }=container.getBoundingClientRect();consttopDistance=e.clientY-top;constbottomDistance=bottom-e.clientY;constleftDistance=e.clientX-left;constrightDistance=right-e.clientX;if(topDistance<scrollSensitivity){y=(-(scrollSensitivity-topDistance)/scrollSensitivity)*scrollSpeed;moveDirection="up";}elseif(bottomDistance<scrollSensitivity){y=((scrollSensitivity-bottomDistance)/scrollSensitivity)*scrollSpeed;moveDirection="down";}if(leftDistance<scrollSensitivity){x=(-(scrollSensitivity-leftDistance)/scrollSensitivity)*scrollSpeed;moveDirection="left";}elseif(rightDistance<scrollSensitivity){x=((scrollSensitivity-rightDistance)/scrollSensitivity)*scrollSpeed;moveDirection="right";}return{ moveDirection, x, y };};
FYI, It's actually for Vue components and used customized Sortable so some function's parameters might be different from original. You also have to call the method scrollFn in above code snippet when Sortable calls original scrollFn too.
Activity
gap777 commentedon Mar 23, 2023
@koraykural Can you post your work-around implementation of
scrollFn?koraykural commentedon Mar 23, 2023
Sorry, I could not find it. It was an old project.
jongmin4943 commentedon Apr 4, 2023
@gap777 Well, I made something work-around implementation that scroll speed increases gradually. It also considers the mouse position outside of window. If u need it, just adjust some codes below
FYI, It's actually for Vue components and used customized Sortable so some function's parameters might be different from original. You also have to call the method
scrollFnin above code snippet when Sortable calls original scrollFn too.something like this