绑定样式.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>绑定样式</title>
  8. <!-- 引入vue -->
  9. <script type="text/javascript" src="../js/vue.js"></script>
  10. <style>
  11. .basic{
  12. width: 400px;
  13. height: 100px;
  14. border: 1px solid black;
  15. }
  16. .happy{
  17. border:4px solid red;
  18. background-color: rgba(255, 255, 0, 0.644);
  19. background: linear-gradient(30deg,yellow,pink,orange,yellow);
  20. }
  21. .sad{
  22. border: 4px dashed rgb(2, 197, 2);
  23. background-color: gray;
  24. }
  25. .normal{
  26. background-color: skyblue;
  27. }
  28. .atguigu1{
  29. background-color: yellowgreen;
  30. }
  31. .atguigu2{
  32. font-size: 30px;
  33. text-shadow: 2px 2px 10px red;
  34. }
  35. .atguigu3{
  36. border-radius: 20px;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <!--
  42. 绑定样式:
  43. 1、class样式
  44. 写法:class="xxx",xxx可以是字符串、对象、数组。
  45. 字符串写法适用于:类名不确定,要动态获取。
  46. 对象写法适用于:要绑定多个样式,个数不确定,名字也不确定
  47. 数组写法适用于:要绑定多个样式,个数确定,名字也确定,但不确定用不用
  48. 2、style样式
  49. :style="{fontSize:xxx}"其中xxx是动态值
  50. :style="[a,b]"其中a、b是样式对象。样式对象的key必须是css中存在的,
  51. 并且多单词组成的样式需要写成小驼峰的写法。
  52. -->
  53. <!-- 准备好一个容器 -->
  54. <div id="root">
  55. <!-- 绑定class样式--字符串写法,适用于:样式的类名不确定,需要动态指定 -->
  56. <div class="basic" :class="mood" @click="changeMood">{{name}}</div><br>
  57. <!-- 绑定class样式--数组写法,适用于:要绑定的样式个数不确定、名字也不确定 -->
  58. <div class="basic" :class="classArr">{{name}}</div><br>
  59. <!-- 绑定class样式--对象写法,适用于:要绑定的样式个数确定、名字也确定,但要动态决定用不用 -->
  60. <div class="basic" :class="classObj">{{name}}</div><br>
  61. <!-- 绑定style样式--对象写法 -->
  62. <div class="basic" :style="styleObj">{{name}}</div><br>
  63. <!-- 绑定style样式--数组写法 -->
  64. <!-- <div class="basic" :style="[styleObj,styleObj2]">{{name}}</div><br> -->
  65. <div class="basic" :style="styleArr">{{name}}</div><br>
  66. </div>
  67. </body>
  68. <script type="text/javascript">
  69. Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示
  70. const vm = new Vue({
  71. el:'#root',
  72. data:{
  73. name:'尚硅谷',
  74. mood:'normal',
  75. classArr:['atguigu1','atguigu2','atguigu3'],
  76. classObj:{
  77. atguigu1:false,
  78. atguigu2:false
  79. },
  80. styleObj:{
  81. fontSize:'40px',
  82. color:'red'
  83. },
  84. styleObj2:{
  85. backgroundColor:'orange'
  86. },
  87. styleArr:[
  88. {
  89. fontSize:'40px',
  90. color:'red'
  91. },
  92. {
  93. backgroundColor:'orange'
  94. }
  95. ]
  96. },methods:{
  97. //点击切换样式(只能切换指定的一种样式)
  98. // changeMood(){
  99. // this.mood = 'happy'
  100. // }
  101. // 点击随机切换样式
  102. changeMood(){
  103. const arr = ['happy','sad','normal']
  104. const index = Math.floor(Math.random()*3)
  105. this.mood = arr[index]
  106. }
  107. }
  108. })
  109. </script>
  110. </html>