dioxus/examples/scroll_to_top.rs

39 lines
1.1 KiB
Rust
Raw Normal View History

//! Scroll elements using their MountedData
//!
//! Dioxus exposes a few helpful APIs around elements (mimicking the DOM APIs) to allow you to interact with elements
//! across the renderers. This includes scrolling, reading dimensions, and more.
//!
//! In this example we demonstrate how to scroll to the top of the page using the `scroll_to` method on the `MountedData`
2023-03-24 11:32:42 -05:00
use dioxus::prelude::*;
fn main() {
launch_desktop(app);
2023-03-24 11:32:42 -05:00
}
fn app() -> Element {
let mut header_element = use_signal(|| None);
2023-03-24 11:32:42 -05:00
2024-01-16 13:18:46 -06:00
rsx! {
2023-03-24 11:32:42 -05:00
div {
h1 {
2024-01-31 14:07:00 -08:00
onmounted: move |cx| header_element.set(Some(cx.data())),
2023-03-24 11:32:42 -05:00
"Scroll to top example"
}
for i in 0..100 {
div { "Item {i}" }
}
button {
2024-01-15 13:06:05 -08:00
onclick: move |_| async move {
if let Some(header) = header_element.cloned() {
2024-01-13 21:12:21 -08:00
let _ = header.scroll_to(ScrollBehavior::Smooth).await;
2023-03-24 11:32:42 -05:00
}
},
"Scroll to top"
}
}
2024-01-13 21:12:21 -08:00
}
2023-03-24 11:32:42 -05:00
}