【问题标题】:Cannot render images dynamically to carousel Vuejs无法将图像动态渲染到轮播 Vuejs
【发布时间】:2019-09-22 14:35:08
【问题描述】:

我已经使用 bootstrap-vue 设置了一个轮播。我使用了一个 for 循环和一个对象数组来动态创建组件。在每张幻灯片中,我将包含一个文本和一个背景图像。我能够动态呈现文本。但是,背景图像只出现在第一张幻灯片上。对于后续的幻灯片,图像将不再呈现。似乎是什么问题?下面是我的代码和结果截图

更新:我仍然无法让它工作。谁能帮我?

<template>
  <div>
    <b-carousel
      id="carousel-1"
      v-model="slide"
      :interval="4000"
      controls
      indicators
      background="#ababab"
      style="text-shadow: 1px 1px 2px #333;"
      @sliding-start="onSlideStart"
      @sliding-end="onSlideEnd"
    >
      <!-- Text slides with image -->
      <b-carousel-slide
        v-for="item in carouselItems"
        :key="item.id"
        :text="item.text"
        :style="{ 'background-image' : 'url(\'' + item.image + '\')' }"
      ></b-carousel-slide>
    </b-carousel>
    <ProductList/>
  </div>
</template>

<script>
// 1. Loading images asychronously later on from S3
// https://stackoverflow.com/questions/50659676/how-to-load-image-src-url-in-vuejs-asyncronously
import ProductList from "@/components/product/ProductList.vue";

export default {
  components: {
    ProductList
  },
  data() {
    return {
      carouselItems: [
        {
          id: 1,
          image: "https://source.unsplash.com/LAaSoL0LrYs/1920x1080",
          text: "Nulla vitae elit libero, a pharetra augue mollis interdum. (1)"
        },
        {
          id: 2,
          image: "https://source.unsplash.com/LAaSoL0LrYs/1920x1080",
          text: "Nulla vitae elit libero, a pharetra augue mollis interdum. (2)"
        },
        {
          id: 3,
          image: "https://source.unsplash.com/LAaSoL0LrYs/1920x1080",
          text: "Nulla vitae elit libero, a pharetra augue mollis interdum. (3)"
        }
      ],
      slide: 0,
      sliding: null
    };
  },
  methods: {
    onSlideStart(slide) {
      this.sliding = true;
    },
    onSlideEnd(slide) {
      this.sliding = false;
    }
  }
};
</script>

<style scoped>
.carousel-item {
  height: 100vh;
  min-height: 350px;
  background: no-repeat center center scroll;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}
</style>

第一次渲染时的轮播

随后的幻灯片不显示图像

返回第一张幻灯片,背景图片消失了

【问题讨论】:

  • 我认为问题在于您如何在 b-carousel-slide 中编写 v-bind:style。试试这个::style="{ backgroundImage : url(item.image) }"
  • 您好,它不起作用。轮播甚至没有与您的代码一起出现

标签: twitter-bootstrap vue.js bootstrap-4 carousel bootstrap-vue


【解决方案1】:

当您使用 bootstrap-vue 库时,您可以使用“caption”属性而不是“text”和“img-src”而不是 style:background。因为b-carousel-slide 组件覆盖了样式标签。

<b-carousel-slide
        v-for="item in carouselItems"
        :key="item.id"
        :caption="item.text"
        :img-src="item.image"
      ></b-carousel-slide>

如果你想为你的 crosal 设置一个自定义样式,你可以为你的标签设置一个额外的 class attr,比如:

<b-carousel-slide
      class="MyCustomClass"
      ....
      ></b-carousel-slide>
<style>
    .MyCustomClass {
        width: 100%;    
        object-fit: cover; 
    }
</style>

但请注意,您使用的轮播要么是响应式的,要么是全角的。我认为你的问题是别的。

【讨论】:

  • 您好,感谢您的回复!我能够动态渲染图像。但是,我使用 style:background 的原因是为了使背景图像响应移动设备。现在已删除,图像不再响应。我该怎么办?
  • ajafari 进一步解释,我希望我的图像适合所有设备的屏幕。我怎样才能做到这一点?
  • 我建议您为 分配一个类,然后添加或覆盖自定义样式项以实现您想要的样式。虽然b-carousel-style 内部有 w-100 类。
  • 嗨 ajafari,但我想要实现这样的目标 stackoverflow.com/questions/55965592/…
  • 哦!我知道你想要高度适合的图像。你没有尝试使用object-fit: cover;吗?
猜你喜欢
  • 2018-11-18
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 2021-07-15
  • 1970-01-01
  • 2016-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多