【发布时间】:2020-10-18 16:34:47
【问题描述】:
我有一个这样定义的细长按钮:
<span class="nav-button" on:click={e => setActive(e.target,"nav-button")}>
...
</span>
当运行下面的 setActive() 函数以应用 .active 类时:
// Toggles the active status of an element
function setActive(e, bubble?:string) {
// Optionally bubbles to spesified parent element
if(bubble) while(!e.classList.contains(bubble)) e = e.parentElement;
(e.classList.contains("active"))? e.classList.remove("active"):e.classList.add("active");
}
它添加了 .active 类,但不添加样式。
.nav-button 在 SCSS 中定义为:
.nav-button {
box-shadow: -7px -7px 20px 0px var(--shadow-primary),
-4px -4px 5px 0px var(--shadow-primary),
7px 7px 20px 0px #0002,
4px 4px 5px 0px #0001,
inset 0px 0px 0px 0px var(--shadow-primary),
inset 0px 0px 0px 0px #0001,
inset 0px 0px 0px 0px var(--shadow-primary),
inset 0px 0px 0px 0px #0001;
transition:box-shadow 0.25s cubic-bezier(.79,.21,.06,.81);
&.active {
box-shadow: 0px 0px 0px 0px var(--shadow-primary),
0px 0px 0px 0px var(--shadow-primary),
0px 0px 0px 0px #0001,
0px 0px 0px 0px #0001,
inset -7px -7px 20px 0px var(--shadow-primary),
inset -4px -4px 5px 0px var(--shadow-primary),
inset 7px 7px 20px 0px #0003,
inset 4px 4px 5px 0px #0001 !important;
}
}
有关视频示例,请参见 here。
如果有人可以帮助我,将不胜感激!
【问题讨论】:
-
您在浏览器中使用 DOM 检查器工具看到了什么? “.active”规则是否被其他规则覆盖?
-
它们不适用于元素;该类已添加到 html 但没有更改 css。
-
对,但是 DOM 检查器应该告诉你为什么。
-
它不会出现在 DOM 检查器的样式列表中。 .active 类未应用,尽管已添加到 html。
-
试试
:global(.nav-button){ .... .... }
标签: javascript html css sass svelte