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
              }
          }
      }

结果示例



相关推荐
Revol_C9 分钟前
开箱即用!轻量级轮询方案,支持同步获取轮询结果!
前端·javascript·设计模式
38242782722 分钟前
python:正则表达式
前端·python·正则表达式
用户479492835691529 分钟前
我是怎么把模型回复用tts播放的更自然的
前端
JS_GGbond29 分钟前
前端崩溃监控:给网页戴上“生命体征监测仪”
前端
俊劫31 分钟前
AI 编码技巧篇(内部分享)
前端·javascript·ai编程
Maxkim31 分钟前
一文读懂 Chrome CRX📦:你需要了解的核心知识点
前端·前端工程化
JackJiang33 分钟前
AI大模型爆火的SSE技术到底是什么?万字长文,一篇读懂SSE!
前端·websocket
Mr_chiu34 分钟前
数据可视化大屏模板:前端开发的效率革命与架构艺术
前端
进击的野人34 分钟前
一个基于 Vue 的 GitHub 用户搜索案例
前端·vue.js·前端框架
ZsTs11934 分钟前
《2025 AI 自动化新高度:一套代码搞定 iOS、Android 双端,全平台 AutoGLM 部署实战》
前端·人工智能·全栈