直接设置背景图片

1、在data中声明背景图片相关配置

export default {
  data () {
    return {
      // 顶部导航背景图片配置
      background: {
      	// 背景图片地址
        backgroundImage: 'url(' + require('../../../static/img/person/temp1.png') + ')',
        // 背景图片是否重复
        backgroundRepeat: 'no-repeat',
        // 背景图片大小
        backgroundSize: 'cover',
        // 背景颜色
        backgroundColor: '#000',
        // 背景图片位置
        backgroundPosition: 'center top'
      }
    }
  }
}

2、在vue中引用

 <div :style="background" class="bgBackground">
 </div>

3、设置背景图片的宽高

.bgBackground{
width:100%;
height:150;
}

动态设置背景图片

1、在data中声明背景图片相关配置和切换背景图片的条件

<script>
export default {
  data () {
    return {
      lessonActive: true, // 我的课程激活
      lessonDeactive: false, // 我的课程未被激活
      initLesson: { // 我的课程未被激活
        backgroundImage: 'url(' + require('../../../static/img/person/play.png') + ')'
      },
      activeLesson: { // 我的课程激活
        backgroundImage: 'url(' + require('../../../static/img/person/play1.png') + ')'
      }
  }
}
</script>

2、在vue中引用
判断lessonActive是否为true,为true则显示激活条件下的图片,反之显示未激活下的图片

  <i :style="{backgroundImage:(lessonActive?activeLesson.backgroundImage:initLesson.backgroundImage)}"></i>
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐