欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > Vue-样式绑定-class

Vue-样式绑定-class

2025/5/20 23:05:24 来源:https://blog.csdn.net/u012516524/article/details/148064064  浏览:    关键词:Vue-样式绑定-class

样式绑定-class

  • 字符串写法
  • 数组写法
  • 对象写法

字符串写法

使用场景: 样式类名不确定,需要动态绑定

  • 代码
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>样式绑定-class</title><!--  引入Vue  --><script type="text/javascript" src="../js/vue.js"></script><style>.base{padding: 5px;height: 100px;}.gray{background-color: gray;}.blue{background-color: skyblue;}.orange{background-color: orange;}</style></head><body><div id="root"><h1>样式绑定-class</h1><div><h2>样式绑定-class  =>  字符串写法</h2><!-- class样式绑定 -> 字符串写法 场景: 样式类名不确定,需要动态绑定--><div class="base" :class="bgc" @click="changeBgc">{{name}}</div></div></div></body><script type="text/javascript">Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示const vm = new Vue({el: "#root",data: {name: "Vue 扛把子",bgc:'gray'},methods: {changeBgc(){const arr = ['gray','blue','orange']const index = Math.floor(Math.random()*3)this.bgc = arr[index]console.log("背景色变成了:" + this.bgc)}}});</script>
</html>
  • 效果

在这里插入图片描述

数组写法

使用场景:样式个数、名字不确定, 即可操作样式数组实现动态样式

  • 代码
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>样式绑定-class</title><!--  引入Vue  --><script type="text/javascript" src="../js/vue.js"></script><style>.base{padding: 5px;height: 100px;}.gray{background-color: gray;font-size: 40px;}.blue{background-color: skyblue;font-weight: bold;}.orange{background-color: orange;color: blue;}</style></head><body><div id="root"><h1>样式绑定-class</h1><div><h2>样式绑定-class  =>  数组写法</h2><!-- class样式绑定 -> 数组写法 场景: 样式个数、名字不确定,即操作样式数组实现动态样式--><div class="base" :class="bgcArr">{{name}}</div></div></div></body><script type="text/javascript">Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示const vm = new Vue({el: "#root",data: {name: "Vue 扛把子",bgcArr:['gray','blue','orange']}});</script>
</html>
  • 效果

在这里插入图片描述

对象写法

使用场景:样式个数、名字确定,动态决定是否使用

  • 代码
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>样式绑定-class</title><!--  引入Vue  --><script type="text/javascript" src="../js/vue.js"></script><style>.base{padding: 5px;height: 100px;}.gray{background-color: gray;font-size: 40px;}.blue{background-color: skyblue;font-weight: bold;}.orange{background-color: orange;color: blue;}</style></head><body><div id="root"><h1>样式绑定-class</h1><div><h2>样式绑定-class  =>  对象写法</h2><!-- class样式绑定 -> 对象写法 场景: 样式个数、名字确定,动态决定是否使用--><div class="base" :class="bgcObj">{{name}}</div><br><button @click="useGray">gray</button> <button @click="useOrange">orange</button> <button @click="noUse">null</button></div></div></body><script type="text/javascript">Vue.config.productionTip = false; // 阻止vue在启动是生成生产提示const vm = new Vue({el: "#root",data: {name: "Vue 扛把子",bgcObj:{gray:false,orange:false}},methods: {useGray(){this.bgcObj.gray = !this.bgcObj.grayconsole.log(this.bgcObj.gray ? "使用gray" : "不使用gray")},useOrange(){this.bgcObj.orange = !this.bgcObj.orangeconsole.log(this.bgcObj.orange ? "使用orange" : "不使用orange")},noUse(){this.bgcObj.orange = false;this.bgcObj.gray = false;console.log("不使用gray、orange")}},});</script>
</html>
  • 效果

    gray


    在这里插入图片描述
    orange


    在这里插入图片描述
    null


    在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词