翼度科技»论坛 编程开发 JavaScript 查看内容

Vue分别运用class绑定和style绑定通过点击实现样式切换

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
Vue官方文档

https://cn.vuejs.org/v2/guide...
  1. <div v-bind:class="[activeClass, errorClass]"></div>
  2. 可以简写成
  3. <div :class="[activeClass, errorClass]"></div>
复制代码
class绑定

  1. <!--
  2. * @Author: [you name]
  3. * @Date: 2021-10-08 15:15:52
  4. * @LastEditors: [you name]
  5. * @LastEditTime: 2021-10-08 22:46:18
  6. * @Description:
  7. -->
  8. <!DOCTYPE html>
  9. <html lang="en">
  10. <head>
  11.     <meta charset="UTF-8">
  12.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  13.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  14.     <title>Document</title>
  15.     <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.13/vue.js"></script>
  16.     <style>
  17.         /* 点击前的样式 */
  18.         .class1 {
  19.             background-color: #fff;
  20.             color: #333;
  21.         }
  22.         /* 点击之后的样式 */
  23.         .class2 {
  24.             background-color: #f52819;
  25.             color: #fff;
  26.         }
  27.         /* 给按钮设置样式 */
  28.         button {
  29.             width: 80px;
  30.             height: 40px;
  31.             border-radius: 5px;
  32.             border: 2px solid rgb(179, 167, 167);
  33.             background-color: #fff;
  34.         }
  35.     </style>
  36. </head>
  37. <body>
  38.     <div id="app">
  39.         <!-- 分别给按钮设置点击事件 -->
  40.         <button @click='handler1' :class="[isYes1? 'class1' : 'class2']">按钮1</button>
  41.         <button @click='handler2' :class="[isYes2? 'class1' : 'class2']">按钮2</button>
  42.         <button @click='handler3' :class="[isYes3? 'class1' : 'class2']">按钮3</button>
  43.     </div>
  44.     <script>
  45.         // 第二种方法
  46.         let vm = new Vue({
  47.             el:'#app',
  48.             data:{
  49.                 isYes1:true,
  50.                 isYes2:true,
  51.                 isYes3:true,
  52.             },
  53.             methods:{
  54.                 handler1(){
  55.                     this.isYes1 = false,
  56.                     this.isYes2 = true,
  57.                     this.isYes3 = true,
  58.                     console.log('第一个点击事件');
  59.                 },
  60.                 handler2(){
  61.                     this.isYes2 = false,
  62.                     this.isYes1 = true,
  63.                     this.isYes3 = true,
  64.                     console.log('第二个点击事件');
  65.                 },
  66.                 handler3(){
  67.                     this.isYes3 = false,
  68.                     this.isYes2 = true,
  69.                     this.isYes1 = true,
  70.                     console.log('第三个点击事件');
  71.                 },
  72.             }
  73.         })
  74.     </script>
  75. </body>
  76. </html>
复制代码
style绑定

  1. <!--
  2. * @Author: [you name]
  3. * @Date: 2021-10-08 15:15:52
  4. * @LastEditors: [you name]
  5. * @LastEditTime: 2021-10-08 22:54:40
  6. * @Description:
  7. -->
  8. <!DOCTYPE html>
  9. <html lang="en">
  10. <head>
  11.     <meta charset="UTF-8">
  12.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  13.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  14.     <title>Document</title>
  15.     <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.13/vue.js"></script>
  16.     <style>
  17.         /* 给按钮设置样式 */
  18.         button {
  19.             width: 80px;
  20.             height: 40px;
  21.             border-radius: 5px;
  22.             border: 2px solid rgb(179, 167, 167);
  23.             background-color: #fff;
  24.         }
  25.     </style>
  26. </head>
  27. <body>
  28.     <div id="app">
  29.         <!-- style绑定,这里是表达式结果类型为字符串,为展示点击按钮改变样式,使用的是三目运算,
  30.             在第一步中设置了一个可用于判断的数据,如果该数据值和按钮内容一样的话,则会触发点击事件,
  31.             该style样式设置为要改变的样式,即data中设置的styCss样式 -->
  32.         <button :style='isActive =="按钮1" ? styCss : ""' @click='changeHandler'>按钮1</button>
  33.         <button :style='isActive =="按钮2" ? styCss : ""' @click='changeHandler'>按钮2</button>
  34.         <button :style='isActive =="按钮3" ? styCss : ""' @click='changeHandler'>按钮3</button>
  35.     </div>
  36.     <script>
  37.         let vm = new Vue({
  38.             el: '#app',
  39.             data: {
  40.                 // 设置一个数据来进行判断,其初始值设为空字符串,就会显示原始样式
  41.                 isActive: '',
  42.                 // 在数据模型中设置经点击后要变换的样式,这里声明一个对象,用在按钮的绑定上,点击后切换的样式
  43.                 styCss: {
  44.                     background: 'red',
  45.                     color: 'white'
  46.                 }
  47.             },
  48.             methods: {
  49.                 // 为点击事件实现三按钮之间的互斥效果,即点击一个按钮,该按钮的样式改变,
  50.                 //其他的不变,点击另一个时,前一个按钮的样式还原,当前按钮样式改变,
  51.                 //那么就需要在点击方法中添加将目标源元素的文本值赋予需要进行判断的数据时,
  52.                 //当点击的按钮的内容和判断的条件一样时,成功触发该点击事件,实现切换并且改变样式的效果。
  53.                 changeHandler(event) {
  54.                     this.isActive = event.target.innerText
  55.                 }
  56.             }
  57.         })
  58.     </script>
  59. </body>
  60. </html>
复制代码
以上就是Vue--分别运用class绑定和style绑定,通过点击实现样式的切换的详细内容,更多关于Vue-运用class style绑定点击样式切换的资料请关注脚本之家其它相关文章!

来源:https://www.jb51.net/javascript/2920020iy.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具