使用 TailwindCSS 和 Flowbite 将轮播添加到我的页面。我还添加了一个切换开关元素,基本上允许我在轮播中的两个项目之间循环(选中 = 幻灯片 2,未选中 = 幻灯片 1)。
这个可行 - 我陷入困境的是尝试根据 localStorage 中该切换的存储值将其设置为加载时的“幻灯片 2”。
我已验证我的 localStorage 值是否已正确保存/检索(作为“是”和“否”字符串)。然后,我尝试在加载时将轮播设置为幻灯片 1(“否”值)或幻灯片 2(“是”值),但无论我尝试什么,它始终显示幻灯片 1。
我尝试了“slideTo()”和“next()”方法,但似乎都不起作用(它们在更改事件中起作用,但在我的加载函数中不起作用)。
控制台中没有错误。就像我说的,切换在页面加载后工作正常,并在幻灯片之间正确循环,它只是没有在页面加载时设置正确的幻灯片(因此显示的幻灯片与切换的准确设置不匹配)。
Flowbite 轮播文档:https://flowbite.com/docs/components/carousel/
这是我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
您正在以编程方式更改选中的属性,这不会触发更改事件。由于未触发更改事件,因此不会调用事件侦听器。
触发更改事件的一种方法:
1
2
constchangeEvent =newEvent("change")exclude_suspended_toggle.dispatchEvent(changeEvent)因此,在您的代码中,它将变为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
constchangeEvent =newEvent("change")// Determine default check state of toggle and slide from user's preferencesif(('metrics-exclude-suspended'in localStorage) && localStorage.getItem('metrics-exclude-suspended') ==='yes') {console.log("enabled from storage...");// Make toggle checkedexclude_suspended_toggle.checked = true;exclude_suspended_toggle.dispatchEvent(changeEvent)}else{console.log("disabled from storage...");// Uncheck toggleexclude_suspended_toggle.checked = false;exclude_suspended_toggle.dispatchEvent(changeEvent)}