vue中如何动态切换背景图片

最近想复习一下vue,就准备写一个后台练练手,开始写登陆页面时,感觉单一背景太单调了,就想着写一个动态切换的背景登录页,可是发现虽然变量定义了,效果却没实现,图片不显示,下面展示一下成功,以及错误代码,和正确代码

运行效果

错误代码

<template>
  <div id="login" :style="{backgroundImage:`url(${loginImage})`}">
      <div class="isbtn"  @click="change()">切换主题</div>
  </div>
</template>

<script>
export default {
  name: '',
  data() {
    return {
      imgindex:1,
      loginImage:('../assets/bgimg/5.jpg')
    };
  },
  computed:{
  },
  watch:{
  },
  methods: {
    change(){
       if(this.imgindex<10){
        this.imgindex+=1
        console.log(this.imgindex)
       }else{
        this.imgindex=1
       }
       this.loginImage=(`../assets/bgimg/${this.imgindex}.jpg`)
    }
  },
};
</script>

<style scoped lang="scss">
.isbtn{
  position: fixed;
  top: 50px;
  right: 40px;
  // background: red;
  color: #0aa1ed;
  // background: lightsalmon;
  font-size: 14px;
  padding: 5px;
  opacity: 0.8;
  cursor: pointer;
}

#login {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  position: relative;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-position: center;
  background-size: cover;
  
}


</style>
正确代码 后来查了查发现如果想要动态引入本地图片的话,需要加一个require引入,不然引入路径是一个字符串,不会被识别出来,具体可以再开发者模式看看有啥区别
<template>
  <div id="login" :style="{backgroundImage:`url(${loginImage})`}">
      <div class="isbtn"  @click="change()">切换主题</div>
  </div>
</template>

<script>
export default {
  name: '',
  data() {
    return {
      imgindex:1,
      loginImage:require('../assets/bgimg/5.jpg')
    };
  },
  computed:{
  },
  watch:{
  },
  methods: {
    change(){
       if(this.imgindex<10){
        this.imgindex+=1
        console.log(this.imgindex)
       }else{
        this.imgindex=1
       }
       this.loginImage=require(`../assets/bgimg/${this.imgindex}.jpg`)
    }
  },
};
</script>

<style scoped lang="scss">
.isbtn{
  position: fixed;
  top: 50px;
  right: 40px;
  // background: red;
  color: #0aa1ed;
  // background: lightsalmon;
  font-size: 14px;
  padding: 5px;
  opacity: 0.8;
  cursor: pointer;
}

#login {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  position: relative;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-position: center;
  background-size: cover;
  
}


</style>

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Docker 是一种容器化技术,可以用于生物信息学学习和实践中。生物信息学是研究基因组、蛋白质组、代谢组以及与生物学相关的大量数据的学科。在生物信息学中,研究人员需要使用各种软件工具进行序列分析、结构预测、基因组注释等工作。传统上,这些工具需要手动在本地环境中进行安装和配置,容易出现软件版本依赖和环境冲突等问题。而使用 Docker 可以将这些工具及其依赖项打包到一个容器中,形成一个独立的、可移植的软件环境。这样,研究人员可以在不同的计算机上快速部署、运行这些工具,避免了繁琐的安装和配置过程。 在知乎上,有许多关于 Docker 和生物信息学的学习资源可以获取。在知乎上,有许多生物信息学领域的专家和爱好者分享了他们的经验和知识。他们可以回答关于 Docker 在生物信息学中的应用、使用技巧、最佳实践等问题。通过阅读和参与这些问题和讨论,我们可以了解到 Docker 在生物信息学中的作用和优势,学习如何使用 Docker 来进行生物信息学研究,以及如何构建和共享自己的 Docker 容器。 在知乎上,我们可以找到许多有关 Docker 生物信息学的话题和文章。这些文章介绍了如何使用 Docker 来搭建生物信息学工作环境,如何使用 Docker 来运行常见的生物信息学软件工具,以及 Docker 在高通量数据分析中的应用等。通过阅读这些文章,我们可以深入了解 Docker 在生物信息学学习中的应用场景和实践经验,帮助我们更好地应用 Docker 来解决生物信息学研究中的问题。 总之,Docker 在生物信息学学习中具有重要的作用。通过在知乎上查找相关话题和文章,我们可以学习和分享 Docker 在生物信息学中的应用经验和最佳实践,提高生物信息学研究的效率和准确性。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值