【问题标题】:Change Bootstrap 4 Carousel Control Colors更改 Bootstrap 4 轮播控件颜色
【发布时间】:2018-08-29 16:24:24
【问题描述】:

我正在使用 Bootstrap 4.0 为 Carousel 使用白色背景,并且想更改控件的颜色。似乎 bootstrap 现在使用 SVG 作为其轮播图标。这意味着直接更改属性不起作用。

我目前也在网站上的其他元素中使用 Font Awesome,所以如果有一种方法可以使用 fa-chevrons 并对其进行格式化,并且它在调整大小和格式化方面仍然表现相同,那可能是有效的解决方案。

这是我当前的控制元素代码:

<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
</a>

我发现了一个类似的问题here,但无法理解那里提供的答案。

我还在 GitHub 上找到了 this 页面,但也无法让那里的任何答案对我有用。

【问题讨论】:

  • fa-chevrons 和默认的 Bootstrap 控件有什么区别?我看不出有什么实质性的区别。
  • 在视觉上它是一样的,但是理论上我可以直接改变颜色。由于引导控件使用 SVG,我无法直接使用 css 更改颜色。我不确定是否有一种简单可靠的方法可以在不改变一些隐藏行为的情况下用 fa-chevron 替换引导控件,因为我对引导没有太多经验。

标签: html css twitter-bootstrap bootstrap-4 carousel


【解决方案1】:

只需在 carousel-control-nextcarousel-control-prev 类中插入您自己的图标。例如,我想用 Font-Awesome 图标更改上一个和下一个图标。我可以这样做:

<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
  <!-- INSERT MY OWN PREV ICON USING FONT AWESOME  -->
  <i style="font-size: 60px" class="fas fa-arrow-alt-circle-left"></i>
  <span class="sr-only">Previous</span>
</a>

<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
  <!-- INSERT MY OWN NEXT ICON USING FONT AWESOME  -->
  <i style="font-size: 60px" class="fas fa-arrow-alt-circle-right"></i>
  <span class="sr-only">Next</span>
</a>

希望它能解决你的问题

【讨论】:

    【解决方案2】:

    不需要任何不必要的 css hack。

    如果您想修改任何 Bootstrap css(或特别是轮播控件颜色),您可以轻松做到。

    以下是控制轮播控件颜色的规则:

    .carousel-control-prev-icon {
        background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' viewBox='0 0 8 8'%3E%3Cpath d='M5.25 0l-4 4 4 4 1.5-1.5-2.5-2.5 2.5-2.5-1.5-1.5z'/%3E%3C/svg%3E");
    }
    
    .carousel-control-next-icon {
        background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' viewBox='0 0 8 8'%3E%3Cpath d='M2.75 0l-1.5 1.5 2.5 2.5-2.5 2.5 1.5 1.5 4-4-4-4z'/%3E%3C/svg%3E");
    }
    

    fill='%23fff' 部分中的fff 替换为所需颜色的十六进制代码。

    这是一个工作代码 sn-p,其中 fill='%23fff' 已被 fill='%23f00' 替换为红色而不是白色:

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
        
    <style>
    .carousel-control-prev-icon {
        background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23f00' viewBox='0 0 8 8'%3E%3Cpath d='M5.25 0l-4 4 4 4 1.5-1.5-2.5-2.5 2.5-2.5-1.5-1.5z'/%3E%3C/svg%3E");
    }
    
    .carousel-control-next-icon {
        background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23f00' viewBox='0 0 8 8'%3E%3Cpath d='M2.75 0l-1.5 1.5 2.5 2.5-2.5 2.5 1.5 1.5 4-4-4-4z'/%3E%3C/svg%3E");
    }
    </style>
    
    <div class="container">
        <div class="row">
            <div class="col">
                <div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
                    <div class="carousel-inner">
                        <div class="carousel-item active">
                            <img class="d-block w-100" src="https://placeimg.com/800/400/animals" alt="First slide">
                        </div>
                        <div class="carousel-item">
                            <img class="d-block w-100" src="https://placeimg.com/800/400/arch" alt="Second slide">
                        </div>
                        <div class="carousel-item">
                            <img class="d-block w-100" src="https://placeimg.com/800/400/nature" alt="Third slide">
                        </div>
                    </div>
                    <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
                        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                        <span class="sr-only">Previous</span>
                    </a>
                    <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
                        <span class="carousel-control-next-icon" aria-hidden="true"></span>
                        <span class="sr-only">Next</span>
                    </a>
                </div>
            </div>
        </div>
    </div>

    【讨论】:

    • 我已经尝试将您的代码放入我的自定义 css 并将颜色修改为#ccc。当那失败时,我包括 !important 给予它优先权。在检查器中,颜色显示为#ccc,但是在 bootstrap 4 轮播中,这些指示器不会随着颜色变量而改变颜色。正如我从链接资源中了解到的,这是由于这些图标更改为 SVG。那就是说我感谢您的反馈。谢谢
    • 刚刚用工作代码 sn-p 更新了我的答案。我最初的回答是错误的。
    • 它现在可以工作了,谢谢!除了您的回答之外,我还需要在样式表中的每个背景图像中添加 !important 。否则从导入的样式表中覆盖该值
    • 为什么background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%6db3b7' viewBox='0 0 8 8'%3E%3Cpath d='M5.25 0l-4 4 4 4 1.5-1.5-2.5-2.5 2.5-2.5-1.5-1.5z'/%3E%3C/svg%3E") !important; 不把它变成蓝色?你的回答很好。
    • @Rick 抱歉回复晚了。它在您的示例中失败的原因是因为您需要在 fill='__' 部分中颜色的十六进制值之前有那个 %23 位。 %23 是 URI 编码的主题标签 (#),需要以十六进制颜色值存在,即如果颜色的十六进制值是 #6db3b7,则其 URI 编码值是 %236db3b7。因此,如果您将 fill='%6db3b7' 更改为 fill='%236db3b7',它应该会按预期工作。有关 URI 编码/解码的更多信息:en.wikipedia.org/wiki/Percent-encodingurl-encode-decode.com
    猜你喜欢
    • 2018-05-04
    • 2018-04-17
    • 1970-01-01
    • 2019-06-11
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    相关资源
    最近更新 更多