所以這是我在 StackOverflow 上的第一個問題。我一直試圖將變數系結到視窗的內部高度。
背景: 我有一個網站,按以下順序由 3 個主要元素組成:
- 標題
- 內容
- 導航欄(固定)
我想要的是找到標題和導航欄之間空間的高度。為此,我試圖將一個變數系結到視窗的內部高度,然后減去已知的標題和導航欄高度。
我覺得奇怪的是,svelte:window 上的其他系結似乎也不起作用。
這是我的代碼示例:
<script>
import { navHeight, headerHeight, contentHeight } from "$lib/stores/dimensions.js"
let innerHeight = 0;
// write the computed height in the store
$: $contentHeight = innerHeight - $navHeight - $headerHeight;
</script>
<!-- Get the window's inner height and bind the innerHeight variable to it -->
<svelte:window bind:innerHeight />
<p>{innerHeight}</p>
uj5u.com熱心網友回復:
所以我找到了解決問題的方法:我試圖將此代碼放在__layout.svelte中。一旦我將此代碼放在索引頁面中,一切都會按預期作業!
顯然布局不能訪問視窗屬性(雖然可以理解,因為它應該用作模板)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/498371.html
