【问题标题】:Change arrow colors in ng-bootstrap carousel更改 ng-bootstrap 轮播中的箭头颜色
【发布时间】:2020-03-19 10:04:02
【问题描述】:

我正在使用 ng-bootstrap 创建轮播,我想将箭头颜色或图像从白色更改为黑色,因为我有白色背景图像并且它们是不可见的。我的问题是我无法跨度,我不知道如何在 scss 中调用它。我将 bootstrap 4 和 ng-bootstrap 用于角度 https://ng-bootstrap.github.io/#/components/carousel/examples。当我在控制台中更改图像网址时,它可以工作。我试图直接获得箭头但没有任何反应。

我的代码:

<div class="col-lg-6">
      <div class="imageCarousel">
        <ng-container *ngIf="product.images">
          <ngb-carousel>
            <ng-template id="imageSlide" ngbSlide *ngFor="let image of product.images | slice: 1">
              <img [src]="image">

            </ng-template>

          </ngb-carousel>
        </ng-container>
      </div>
    </div>

scss:

.imageCarousel {
        max-height: 500px;
        text-align: center;
        color: $color2;
        .carousel .slide {
            width: 50px;
            height: 50px;
            background-image: url("https://storage.googleapis.com/staging.pc-shop-260af.appspot.com/ic_keyboard_arrow_right_black_24px.svg");
        }

        img {
            // border: 2px solid blue;
            max-width: 100%;
            max-height: 400px;
        }
    }

【问题讨论】:

    标签: html sass bootstrap-4


    【解决方案1】:

    是的,它已解决,但有时您需要将视图封装设置为 None

    @Component({
      selector: "app-builder",
      templateUrl: "./builder.component.html",
      styleUrls: ["./builder.component.css"],
      encapsulation: ViewEncapsulation.None
    })
    

    【讨论】:

    • 谢谢——这样我就可以让图标的本地 CSS 工作了。
    • 我已经尝试过您的解决方案,现在本地 CSS 可以正常工作,但箭头点击不起作用。
    【解决方案2】:

    ngb-carousel 使用 SVG 图像作为控件,您可以使用自己的 prev 和 next 按钮图像覆盖它:

    .carousel-control-prev-icon{
       background-image: url('https://apufpel.com.br/assets/img/seta_prim.png')
    }
    .carousel-control-next-icon{
      background-image: url('https://apufpel.com.br/assets/img/seta_ult.png')
    }
    

    【讨论】:

    • 是的,问题是我对 scss 不熟悉,我正在尝试,但仍然没有
    • 就像我上面说的,'我试图直接得到箭头'
    • 你能上传一个小提琴吗?
    • 我已将它与 firebase db 连接,不能:/
    • 问题是,我无法在我的组件样式中设置这些箭头的样式。全局 style.scss 工作。无论如何,您的答案有效,但您可以将其更改为使用全局答案,我会接受
    【解决方案3】:

    Eduardo 给出的答案(上图)仅在封装设置为 ViewEncapsulation.None 时才有效,这会对 CSS 的其他方面产生意想不到的副作用。

    而是可以使用 ::ng-deep 伪类来定位相关的特定 CSS。在您的 scss 文件中使用以下内容:

    ::ng-deep .carousel-control-prev-icon{
      background-image: url('your-replacement.svg');
    }
    ::ng-deep .carousel-control-next-icon{
      background-image: url('your-replacement.svg');
    }
    

    【讨论】:

    • 欢迎来到 Stack Overflow。请参观了解 Stack Overflow 的工作原理,并阅读如何提问以了解如何提高问题的质量。
    【解决方案4】:

    首先,我建议升级到 ng-bootstrap v6+,它正确地将 ngb 组件 ViewEncapsulation 设置为 None,因此可以通过全局 CSS 或通过未封装的组件 CSS 进行设置。请参阅 (https://github.com/ng-bootstrap/ng-bootstrap/issues/3479)。

    其次,只需重新设置 SVG 并将 fill 属性更改为您想要的任何颜色,即可覆盖 SVG。

    .carousel-control-next-icon {
       background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='rgb(50, 54, 57)' width='8' height='8' viewBox='0 0 8 8'%3e%3cpath d='M2.75 0l-1.5 1.5L3.75 4l-2.5 2.5L2.75 8l4-4-4-4z'/%3e%3c/svg%3e") !important;
    }
    

    或者,如果您只是想获得更多对比度,也可以反转颜色:

    .carousel-control-next-icon,
    .carousel-control-prev-icon {
       filter: invert(1);
    }
    

    在您的主机组件上设置 encapsulation: ViewEncapsulation.None 并不是最佳做法。更明智的做法是将这些条目更改为您的项目 styles.scss(或在 angular.json 中指定为全局样式文件的任何内容)

    【讨论】:

      【解决方案5】:

      在提供的链接上检查该控件时,您可以看到您的两个按钮都附加了类。

      那些箭头有一个背景图像属性。在您自己的 css 文件中为该属性覆盖该类。

      【讨论】:

      • 我现在从办公桌上起来。你能验证在检查器中更改属性是否有效吗?
      • 是的,我已经检查过了,没有任何效果,我试图找到这个箭头,直接给他们,甚至打电话给他们的父母,但仍然没有
      猜你喜欢
      • 2022-06-16
      • 2019-07-12
      • 1970-01-01
      • 2020-10-08
      • 2018-08-29
      • 2017-12-26
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多