Vue之class的用法


Vue中class的使用總結如下:

使用形式v-bind:class  簡寫:class

1.在數組中使用一個class

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title>VUE</title>  
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">  
    <meta name="apple-mobile-web-app-capable" content="yes">  
    <meta name="apple-mobile-web-app-status-bar-style" content="black">  
    <style>  
        .red{  
            color: red;  
        }  
        .blue{  
            background: blue;  
        }  
    </style>  
    <script src="vue.js"></script>  
    <script>  
        window.onload=function(){  
            new Vue({  
                el:'#box',  
                data:{  
                    color:'blue'  
                },  
                methods:{  
                }  
            });  
        };  
    </script>  
</head>  
<body>  
    <div id="box">  
        <strong :class="[color]">文字...</strong>  
    </div>  
</body>  
</html>  

描述:

 

1)

 a) <strong :class="[color]">文字...</strong>

 b) data:{
                 color:'blue'
     }

兩者的color名稱是對應一致的

 

    2)

a) data:{
             color:'blue'
   },

b) .blue{
            background: blue;
        }

兩者的blue名稱是對應一致的。

 

 

 

2.在數組中使用多個class

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title></title>  
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">  
    <meta name="apple-mobile-web-app-capable" content="yes">  
    <meta name="apple-mobile-web-app-status-bar-style" content="black">  
    <style>  
        .red{  
            color: red;  
        }  
        .blue{  
            background: blue;  
        }  
    </style>  
    <script src="vue.js"></script>  
    <script>  
        window.onload=function(){  
            new Vue({  
                el:'#box',  
                data:{  
                    red:'red',  
                    b:'blue'  
                },  
                methods:{  
                }  
            });  
        };  
    </script>  
</head>  
<body>  
    <div id="box">  
        <strong :class="[red,b]">文字...</strong>  
    </div>  
</body>  
</html>  

描述:

原理和上述使用一個數組一樣

 

3.使用對象形式

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title></title>  
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">  
    <meta name="apple-mobile-web-app-capable" content="yes">  
    <meta name="apple-mobile-web-app-status-bar-style" content="black">  
    <style>  
        .red{  
            color: red;  
        }  
        .blue{  
            background: blue;  
        }  
    </style>  
    <script src="vue.js"></script>  
    <script>  
        window.onload=function(){  
            new Vue({  
                el:'#box',  
                data:{  
                },  
                methods:{  
                }  
            });  
        };  
    </script>  
</head>  
<body>  
    <div id="box">  
        <strong :class="{red:true,blue:true}">文字...</strong>  
    </div>  
</body>  
</html>  

描述:

使用對象的形勢的時候,<strong :class="{red:true,blue:true}">文字...</strong>,其中的red,blue都是class的名字,

和數組形勢的時候不一樣。當為true的時候,顯示對應的樣式,為false的時候,隱藏對應的樣式。現在都是true,

所以文字顏色是紅色。背景是藍色

 

4.使用對象形式。不同於上一個。

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title></title>  
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">  
    <meta name="apple-mobile-web-app-capable" content="yes">  
    <meta name="apple-mobile-web-app-status-bar-style" content="black">  
    <style>  
        .red{  
            color: red;  
        }  
        .blue{  
            background: blue;  
        }  
    </style>  
    <script src="vue.js"></script>  
    <script>  
        window.onload=function(){  
            new Vue({  
                el:'#box',  
                data:{  
                    a:true,  
                    b:false  
                },  
                methods:{  
                }  
            });  
        };  
    </script>  
</head>  
<body>  
    <div id="box">  
        <strong :class="{red:a,blue:b}">文字...</strong>  
    </div>  
</body>  
</html>  

描述:

 

和第三種不同的是,使用了a,b兩個變量,然后在data中設置a,b的布爾值,從而實現class的顯示和隱藏。true

顯示,false隱藏。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM