diff --git a/examples/ui/scroll.rs b/examples/ui/scroll.rs index c087dfc6bdfee..14329cc45fb10 100644 --- a/examples/ui/scroll.rs +++ b/examples/ui/scroll.rs @@ -78,7 +78,8 @@ fn on_scroll_handler( }; if !max { - scroll_position.x += delta.x; + // Update the x portion of the scroll position with the scroll delta and clamp the value between 0.0 and the max x offset + scroll_position.x = (scroll_position.x + delta.x).max(0.0).min(max_offset.x); // Consume the X portion of the scroll delta. delta.x = 0.; } @@ -93,7 +94,8 @@ fn on_scroll_handler( }; if !max { - scroll_position.y += delta.y; + // Update the y portion of the scroll position with the scroll delta and clamp the value between 0.0 and the max y offset + scroll_position.y = (scroll_position.y + delta.y).max(0.0).min(max_offset.y); // Consume the Y portion of the scroll delta. delta.y = 0.; }