【问题标题】:Bootstrap 4 sass two gradient colors to navbar links hover not workingBootstrap 4 sass 两种渐变颜色到导航栏链接悬停不起作用
【发布时间】:2018-04-14 15:57:18
【问题描述】:

我真的是 sass 的新手,我为此搜索了很多我云没有找到任何解决方案,帮助我

我已经创建了带有渐变背景的 bootstrap 4 导航栏,渐变导航栏完美地链接了它的工作,我再次将渐变字体颜色添加到 a:hover 它的工作但如果我将渐变背景颜色添加到 a:hover 它不起作用,只显示'a' 链接背景颜色渐变,但不是 'a' 字体渐变

任何帮助将不胜感激,并提前感谢您。

Expected result

Actual result

HTML

<nav class="navbar navbar-expand-sm">
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navBarItems" aria-controls="navbarText"
        aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navBarItems">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item">
                <a href="" class="nav-link">Home</a>
            </li>
            <li class="nav-item">
                <a href="" class="nav-link">Books</a>
            </li>
            <li class="nav-item">
                <a href="" class="nav-link">Guest</a>
            </li>
            <li class="nav-item">
                <a href="" class="nav-link active">Contact us</a>
            </li>
        </ul>
    </div>
</nav>

SASS

@mixin gold-gradient(){
background: rgb(249, 206, 112);
background: -moz-linear-gradient(left, rgba(249, 206, 112, 1) 0%, rgba(197, 154, 38, 1) 50%, rgba(249, 206, 112, 1) 100%, rgba(123, 90, 0, 1) 100%);
    background: -webkit-linear-gradient(left, rgba(249, 206, 112, 1) 0%, rgba(197, 154, 38, 1) 50%, rgba(249, 206, 112, 1) 100%, rgba(123, 90, 0, 1) 100%);
    background: linear-gradient(to right, rgba(249, 206, 112, 1) 0%, rgba(197, 154, 38, 1) 50%, rgba(249, 206, 112, 1) 100%, rgba(123, 90, 0, 1) 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f9ce70', endColorstr='#7b5a00', GradientType=1);
}

@mixin red-gradient($dir-1: left,
$dir-2: right) {
    background: rgb(153, 10, 34);
    background: -moz-linear-gradient($dir-1, rgba(153, 10, 34, 1) 8%, rgba(200, 9, 54, 1) 50%, rgba(211, 31, 31, 1) 100%);
    background: -webkit-linear-gradient($dir-1, rgba(153, 10, 34, 1) 8%, rgba(200, 9, 54, 1) 50%, rgba(211, 31, 31, 1) 100%);
    background: linear-gradient(to $dir-2, rgba(153, 10, 34, 1) 8%, rgba(200, 9, 54, 1) 50%, rgba(211, 31, 31, 1) 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#990a22', endColorstr='#d31f1f', GradientType=1);
}

@mixin gold-text-gradient() {
    background: rgb(249, 206, 112);
    background: -moz-linear-gradient(left, rgba(249, 206, 112, 1) 0%, rgba(197, 154, 38, 1) 50%, rgba(249, 206, 112, 1) 100%, rgba(123, 90, 0, 1) 100%);
    background: -webkit-linear-gradient(left, rgba(249, 206, 112, 1) 0%, rgba(197, 154, 38, 1) 50%, rgba(249, 206, 112, 1) 100%, rgba(123, 90, 0, 1) 100%);
    background: linear-gradient(to right, rgba(249, 206, 112, 1) 0%, rgba(197, 154, 38, 1) 50%, rgba(249, 206, 112, 1) 100%, rgba(123, 90, 0, 1) 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f9ce70', endColorstr='#7b5a00', GradientType=1);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

@mixin red-text-gradient() {
    background: rgb(153, 10, 34);
    background: -moz-linear-gradient(left, rgba(153, 10, 34, 1) 8%, rgba(200, 9, 54, 1) 50%, rgba(211, 31, 31, 1) 100%);
    background: -webkit-linear-gradient(left, rgba(153, 10, 34, 1) 8%, rgba(200, 9, 54, 1) 50%, rgba(211, 31, 31, 1) 100%);
    background: linear-gradient(to right, rgba(153, 10, 34, 1) 8%, rgba(200, 9, 54, 1) 50%, rgba(211, 31, 31, 1) 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#990a22', endColorstr='#d31f1f', GradientType=1);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.navbar {
    @include red-gradient();
    .navbar-nav li a {
        @include gold-text-gradient;
    }
    .navbar-nav li a:hover {
        @include red-text-gradient();
        @include gold-gradient();
    }
}

Codepen Demo

【问题讨论】:

    标签: sass hover navbar gradient bootstrap-4


    【解决方案1】:

    试试这个代码,它对我有用。

    .navbar {
       @include red-gradient();
       .navbar-nav li a {
           @include gold-text-gradient;
       }
    
       .navbar-nav{ 
           a:hover {
             @include red-text-gradient();
           }
           li:hover{
             @include gold-gradient();
           } 
        } 
    }
    

    问题是您将此渐变设置为文本(在本例中为&lt;a&gt;)而不是此文本的容器(&lt;li&gt;)。

    【讨论】:

    • 是的,我在发布这个问题之前已经尝试过这个,但是我可以通过正常的 css 方式存档这个,它给出一个:hover{color:#fff;背景:#000} 作为渐变色
    • 我猜你不能,因为在这种情况下你需要访问 a:hover{} 的父级,而我不知道 Sass 中的方式。但我认为我给你的解决方案很有效,而且效果很好。
    • 非常感谢您的帮助,
    猜你喜欢
    • 2018-09-14
    • 2018-12-01
    • 2022-01-08
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2017-09-08
    • 2015-07-29
    相关资源
    最近更新 更多