scroll_to_offset.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //! Scroll elements using their MountedData
  2. //!
  3. //! Dioxus exposes a few helpful APIs around elements (mimicking the DOM APIs) to allow you to interact with elements
  4. //! across the renderers. This includes scrolling, reading dimensions, and more.
  5. //!
  6. //! In this example we demonstrate how to scroll to a given y offset of the scrollable parent using the `scroll` method on the `MountedData`
  7. use dioxus::html::geometry::PixelsVector2D;
  8. use dioxus::prelude::*;
  9. fn main() {
  10. dioxus::launch(app);
  11. }
  12. fn app() -> Element {
  13. rsx! {
  14. ScrollToCoordinates {}
  15. ScrollToCoordinates {}
  16. }
  17. }
  18. #[component]
  19. fn ScrollToCoordinates() -> Element {
  20. let mut element = use_signal(|| None);
  21. rsx! {
  22. div { border: "1px solid black", position: "relative",
  23. div {
  24. height: "300px",
  25. overflow_y: "auto",
  26. onmounted: move |event| element.set(Some(event.data())),
  27. for i in 0..100 {
  28. div { height: "20px", "Item {i}" }
  29. }
  30. }
  31. div { position: "absolute", top: 0, right: 0,
  32. input {
  33. r#type: "number",
  34. min: "0",
  35. max: "99",
  36. oninput: move |event| async move {
  37. if let Some(ul) = element.cloned() {
  38. let data = event.data();
  39. if let Ok(value) = data.parsed::<f64>() {
  40. ul.scroll(PixelsVector2D::new(0.0, 20.0 * value), ScrollBehavior::Smooth)
  41. .await
  42. .unwrap();
  43. }
  44. }
  45. },
  46. }
  47. }
  48. }
  49. }
  50. }