Vue.js标签v-bind和v-on初探实例

Vue初探使用标签进行图片以及背景切换

本来这次作业还是想水一下饿,但是要是写太少的字数对曝光量不易,所以还是有水字数的必要,虽然说真的很语无伦次,不知所言,但是还是有必要说明情况,具体什么情况,且听接下来的分析。

script代码

html 复制代码
<script>
  const app=new Vue({
      el:"#zjw",
      data:{
          filename:"./img/李四1.jpg",
          flag:1,
          color:"background-color:red"
      },
      methods:{
          change:function (){
              if(this.flag==1){
                  this.filename="./img/李四1.jpg"
                  this.color="background-color:red"
                  this.flag=2
              }else if(this.flag==2){
                  this.filename = "./img/李四2.jpg"
                  this.color="background-color:blue"
                  this.flag=3
              }else{
                  this.filename="./img/李四3.jpg"
                  this.color="background-color:skyblue"
                  this.flag=1
              }
          }
      }
  })
</script>

body

html 复制代码
<body>
<div id="zjw" v-bind:style="color" style="width:700px;height: 500px">

    <img v-bind:src="filename" alt="李四" style="width: 380px;height:500px">
<button @click="change()">切换</button>
</div>
</body>

简单分析

v-bind绑定stylev-bind:style="color",我这里写的color,要做切换颜色的处理。而对于imgv-bind:src="filename",要进行切换照片的处理。

切换按钮<button @click="change()">切换</button>

@click就等于v-on

data

html 复制代码
data:{
          filename:"./img/李四1.jpg",
          flag:1,
          color:"background-color:red"
      },

methods

通过if ,else if ,else来进行3者的转换,这样在点击切换时就能进行3个图片以及div背景的转换了。

html 复制代码
methods:{
          change:function (){
              if(this.flag==1){
                  this.filename="./img/李四1.jpg"
                  this.color="background-color:red"
                  this.flag=2
              }else if(this.flag==2){
                  this.filename = "./img/李四2.jpg"
                  this.color="background-color:blue"
                  this.flag=3
              }else{
                  this.filename="./img/李四3.jpg"
                  this.color="background-color:skyblue"
                  this.flag=1
              }
          }
      }

结果示例



相关推荐
程序员大阳2 分钟前
副队长CSS教程(30)–CSS Grid 实战:实现复杂自适应表单布局
前端·css·自适应·grid·表单
Csvn7 分钟前
微前端实现
前端
Chrischaan1 小时前
做 AI 塔罗小程序时,我为什么不让大模型自己抽牌:一次上线复盘
前端
骑着蜗牛撵大象3271 小时前
LangChain 工具调用从声明到闭环:给智能体接上真实的手脚
前端
数据掘金1 小时前
百度统计后台页面数据异常?从手动调用时机开始排查
前端
hunteritself1 小时前
10 层 4K 实时剪辑,国产工作站进入广电制作核心
大数据·前端·人工智能·深度学习·transformer
踩着两条虫1 小时前
我在浏览器里“盖”了一个村:Vue + Three.js 大屏开发踩坑记
前端·vue.js·three.js
默_笙2 小时前
🧀 让大模型的答案"流"出来:NestJS AI 模块 + SSE + RxJS 实战
javascript·人工智能
mbshqqb2 小时前
TypeScript基础教程之同步生成器
前端·javascript·typescript
嘟嘟同学和妮妮同学2 小时前
【升级】一个中国历史可视化站:皇帝时间线梳理、古代思想流派、诗词鉴赏 (2)
前端·html