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 16:32:42 +00:00
use dioxus::prelude::*;
fn main() {
dioxus::launch(app);
2023-03-24 16:32:42 +00:00
}
fn app() -> Element {
let mut header_element = use_signal(|| None);
2023-03-24 16:32:42 +00:00
2024-01-16 19:18:46 +00:00
rsx! {
2023-03-24 16:32:42 +00:00
div {
h1 {
2024-01-31 22:07:00 +00:00
onmounted: move |cx| header_element.set(Some(cx.data())),
2023-03-24 16:32:42 +00:00
"Scroll to top example"
}
for i in 0..100 {
div { "Item {i}" }
}
button {
2024-01-15 21:06:05 +00:00
onclick: move |_| async move {
if let Some(header) = header_element.cloned() {
2024-01-14 05:12:21 +00:00
let _ = header.scroll_to(ScrollBehavior::Smooth).await;
2023-03-24 16:32:42 +00:00
}
},
"Scroll to top"
}
}
2024-01-14 05:12:21 +00:00
}
2023-03-24 16:32:42 +00:00
}