简体   繁体   English

SCSS范围和变量问题

[英]Issues with SCSS scope and variables

I'm attempting to animate a UL popping up. 我正在尝试制作弹出的UL动画。 I want this to happen on a button click event. 我希望在按钮单击事件中发生这种情况。 I have :active and :hover states on my button that work. 我的按钮上有:active:hover状态可以正常工作。 The problem arises when I try to add conditional CSS to the dropdown list upon pressing the button. 当我尝试在按下按钮时将条件CSS添加到下拉列表时,就会出现问题。 My current approach attempts to use variables. 我当前的方法尝试使用变量。 The HTML is: HTML是:

<div class="col-md-6 col-md-offset-3 dropDiv">
    <button class="btn btn-primary dropdown-toggle" type="button" id="actionHeader" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">How can we help you?</button>
    <ul class="dropdown-menu actionDrop text-center" aria-expanded="false">
        <li><a href="/blogs">Software Consulting</a></li>
        <li><a href="">Careers at Bellwether</a></li>
        <li><a href="">Our Team</a></li>
        <li><a href="">Bellwether in the News</a></li>
    </ul>
</div>

An example of the SCSS that animates the button: 为按钮设置动画的SCSS示例:

&[aria-expanded="true"] {
    transition: transform 0.3s;
    transform: rotateX(90deg) translateY(0);
    $isExpanded: true;
}

$isExpanded is a global variable. $isExpanded是全局变量。 In my dropdown UL css, I attempt to test if $isExpanded is true, and apply CSS if it is: 在我的下拉UL CSS中,我尝试测试$isExpanded是否为true,如果是,则应用CSS:

.actionDrop {
    background: $bellTan;
    width: 100%;
    text-align: center;
    overflow: hidden;

    @if $isExpanded {
        color: red;
    }
}

I've also tried: 我也尝试过:

&:@if $isExpanded {
    color: red;
}

But I think that syntax is wrong. 但是我认为语法是错误的。

The color is never applied in either case. 在任何一种情况下都不会应用颜色。 How do I apply CSS to an element based on a global SASS variable set by a different element's class? 如何基于不同元素类设置的全局SASS变量将CSS应用于元素?

Please excuse me if I've made a stupid mistake, I have only been using CSS precompilers for about 2 days now. 如果我犯了一个愚蠢的错误,请原谅,我现在才使用CSS预编译器大约2天。

I think you are mistaking SCSS for a real-time script for CSS, which it is not. 我认为您将SCSS误认为是CSS的实时脚本,事实并非如此。 It is precompiled to standard CSS. 它已预编译为标准CSS。 All your logic is computed at this compilation stage. 您的所有逻辑都是在此编译阶段计算的。

This should achieve what you are looking for: 这应该可以实现您想要的目标:

.dropdown-toggle[aria-expanded="true"] + .actionDrop {
  color: red;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM