2024-02-14 21:48:58 +00:00
|
|
|
//! 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() {
|
2024-10-10 23:00:58 +00:00
|
|
|
dioxus::launch(app);
|
2023-03-24 16:32:42 +00:00
|
|
|
}
|
|
|
|
|
2024-01-14 04:51:37 +00:00
|
|
|
fn app() -> Element {
|
2024-01-16 07:24:59 +00:00
|
|
|
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 {
|
2024-01-16 05:12:44 +00:00
|
|
|
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
|
|
|
}
|