简体   繁体   English

更改 ng-bootstrap carousel 中的箭头颜色

[英]Change arrow colors in ng-bootstrap carousel

I'm creating carousel with ng-bootstrap, I want to change arrow colors or image from white to black cause i have white background images and they are invisible.我正在使用 ng-bootstrap 创建轮播,我想将箭头颜色或图像从白色更改为黑色,因为我有白色背景图像并且它们不可见。 My problem is that i can't get span, i don't know how to call this in scss.我的问题是我无法获得 span,我不知道如何在 scss 中调用它。 I'm using bootstrap 4 and ng-bootstrap for angular https://ng-bootstrap.github.io/#/components/carousel/examples .我正在将 bootstrap 4 和 ng-bootstrap 用于 angular https://ng-bootstrap.github.io/#/components/carousel/examples When i change image url in console it works.当我在控制台中更改图像 url 时,它可以工作。 I was trying to get arrows directly but nothing happens.我试图直接获得箭头,但没有任何反应。

My code:我的代码:

<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: 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;
        }
    }

Yeap it's solved but you sometimes it's necessary to set the view encapsulation to None是的,它已经解决了,但有时需要将视图封装设置为None

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

ngb-carousel use SVG images as controls, you can override that with your own image of prev and next button: ngb-carousel 使用 SVG 图像作为控件,您可以使用自己的上一个和下一个按钮图像覆盖它:

.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')
}

The answer given by Eduardo (above) only works if encapsulation is set to ViewEncapsulation.None which can have unintended side effects on other aspects of your CSS. Eduardo(以上)给出的答案仅在封装设置为 ViewEncapsulation.None 时才有效,这可能会对 CSS 的其他方面产生意想不到的副作用。

Instead the ::ng-deep pseudo-class can be used to target the specific CSS in question.相反,::ng-deep 伪类可用于针对特定的 CSS。 In your scss file use the following:在您的 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');
}

First I would suggest upgrading to ng-bootstrap v6+ which correctly makes the ngb component ViewEncapsulation as None so it can be set through global CSS or via unencapsulated component CSS.首先,我建议升级到 ng-bootstrap v6+,它正确地使 ngb 组件 ViewEncapsulation 为 None,因此可以通过全局 CSS 或未封装的组件 CSS 设置它。 See ( https://github.com/ng-bootstrap/ng-bootstrap/issues/3479 ).请参阅( https://github.com/ng-bootstrap/ng-bootstrap/issues/3479 )。

Second, override the SVG by simply setting it again & changing the fill property to whatever color you desire.其次,通过简单地再次设置并将填充属性更改为您想要的任何颜色来覆盖 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;
}

or alternatively you can invert the colors if you're just looking for more contrast:或者,如果您只是在寻找更多对比度,则可以反转颜色:

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

Setting encapsulation: ViewEncapsulation.None on your host component isn't a best practice.设置封装:主机组件上的ViewEncapsulation.None不是最佳实践。 The smarter thing to do is to make these entry changes to your projects styles.scss (or whatever is specified in angular.json as the global styles file)更聪明的做法是将这些条目更改到您的项目 style.scss(或在 angular.json 中指定为全局样式文件的任何内容)

When inspecting that control on the link provided you can see your two buttons have classes attached to them.在提供的链接上检查该控件时,您可以看到您的两个按钮附加了类。

检查

Those arrows have a background-image attribute.这些箭头具有 background-image 属性。 Override that class for that attribute in your own css file.在您自己的 css 文件中为该属性覆盖该类。

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

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