vue div backgroundSize 不起作用的解决办法

原文链接: vue div backgroundSize 不起作用的解决办法

上一篇: webpack assets static 目录 区别

下一篇: Python 统计 博客 信息

在使用绑定样式的功能时,发现对div的backgroundSize属性设置不起作用,于是只能 使用一个比较笨拙的办法,解决这个问题

初始加载是生效的,但是之后当点击按钮改变图片地址后,无法填充背景图片

185633_H8xP_2856757.png

改变背景之后

185740_oMxp_2856757.png

第一个使用的是计算属性,绑定style,使用background-size和backgSize都无效

185816_30eo_2856757.png

下面的是使用原生js设置样式

185847_5qTV_2856757.png

第一个

<template>
  <div>
    <div class="img" :style="style"></div>
    <button @click="prev">prev</button>
    <button @click="next">next</button>
  </div>
</template>

<script>
  export default {
    name: "swiper",
    data() {
      return {
        cur_index: 0,
        imgs: [
          'https://i1.mifile.cn/a4/xmad_15195327867488_jlLnp.jpg',
          'https://i1.mifile.cn/a4/xmad_15185161540821_qPMoX.jpg',
          'https://i1.mifile.cn/a4/xmad_15193829171444_CQnuo.jpg',
          'https://i1.mifile.cn/a4/xmad_15192945916761_ormJz.jpg',
        ],
      }
    },
    computed: {
      style() {
        return {
          background: `url('${this.imgs[this.cur_index]}') no-repeat`,
          'backgroundSize': 'contain'
        }
      }
    },
    methods: {
      prev() {
        this.cur_index = (this.cur_index + this.imgs.length - 1) % this.imgs.length
      },
      next() {
        this.cur_index = (this.cur_index + 1) % this.imgs.length
      }
    }
  }
</script>

<style scoped>
  .img {
    width: 400px;
    height: 300px;
    border: 1px solid black;
  }
</style>

第二个

<template>
  <div>
    <div class="img" :style="style" ref="swiper"></div>
    <button @click="prev">prev</button>
    <button @click="next">next</button>
  </div>
</template>

<script>
  export default {
    name: "swiper",
    data() {
      return {
        cur_index: 0,
        imgs: [
          'https://i1.mifile.cn/a4/xmad_15195327867488_jlLnp.jpg',
          'https://i1.mifile.cn/a4/xmad_15185161540821_qPMoX.jpg',
          'https://i1.mifile.cn/a4/xmad_15193829171444_CQnuo.jpg',
          'https://i1.mifile.cn/a4/xmad_15192945916761_ormJz.jpg',
        ],
      }
    },
    watch: {
      cur_index() {
        this.refresh()
      }
    },
    methods: {
      refresh() {
        this.$refs.swiper.style.background = `url('${this.imgs[this.cur_index]}') no-repeat`
        this.$refs.swiper.style.backgroundSize = 'contain'
      },
      prev() {
        this.cur_index = (this.cur_index + this.imgs.length - 1) % this.imgs.length
      },
      next() {
        this.cur_index = (this.cur_index + 1) % this.imgs.length
      }
    },
    mounted() {
      this.refresh()
    }
  }
</script>

<style scoped>
  .img {
    width: 400px;
    height: 300px;
    border: 1px solid black;
    /*background-size: contain;*/
  }
</style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
回答: 在Vue中使用CSSbackground属性时,可以通过style绑定来设置background-image。例如,你可以在div元素上使用style绑定来设置background-image属性,如下所示: ```html <div class="login-container" :style="{ 'background-image': 'url(./登录页bg.png)' }"></div> ``` 这样就可以将指定的图片作为背景图像显示在div元素中了。[1] 另外,background属性是background-color、background-image、background-repeat、background-attachment、background-position和background-size等属性的缩写形式。你可以根据需要在style绑定中设置这些属性,如下所示: ```html <div :style="{ 'background-color': 'red', 'background-image': 'url(./image.jpg)', 'background-repeat': 'no-repeat', 'background-position': 'center', 'background-size': 'cover' }"></div> ``` 这样就可以设置div元素的背景颜色、背景图像、背景重复方式、背景位置和背景大小了。[2] 此外,你还可以根据条件计算来设置background属性的值。例如,你可以在Vue组件中使用计算属性来动态设置background属性,如下所示: ```html <template> <div :style="{ '--lineheight': lineheight }" class="text"> <div class="container"></div> </div> </template> <script> export default { name: 'QTest', props: { lineheight: { type: String, default: '200px' } }, } </script> <style lang="scss" scoped> .text { width: 100px; height: 400px; overflow: hidden; .container { height: calc(100% - var(--lineheight)); background-color: red; } } </style> ``` 在上述示例中,通过计算属性lineheight来动态设置div元素的高度,并将其作为背景属性中的变量(--lineheight)使用。这样可以根据条件计算来设置background属性的值。[3]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值