custom_variable.uvue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <view id="var_1" style="flex:1;--var_margin: 2px;" class="var_1" :class="var_bind">
  3. <view>
  4. <view class="var_1_1_1">
  5. <text class="firstParagraph">此段落应有蓝色背景和绿色文本。</text>
  6. </view>
  7. <text class="secondParagraph">此段落应有红色背景和黄色文本。</text>
  8. </view>
  9. <text style="width: var(--var_width_invalid, 375rpx);background-color:yellowgreen;">无效值width应为375rpx</text>
  10. <view id="chanageVarBox" :class="text_style_var_def">
  11. <text class="text_style"
  12. v-if="text_style_var_1_on">font-size:26px;style:normal;color:#ccc;background-color:#488cff</text>
  13. <text class="text_style" v-else>font-size:16px;style:italic;color:#803390;background-color:#48ff32</text>
  14. </view>
  15. <view class="view_layout_style_container" id="view_container">
  16. <view class="view_layout_style"></view>
  17. <view class="view_layout_style" style="background-color: blue;--var_align_self: flex-start;"></view>
  18. </view>
  19. <button id="changeVarButton" @tap="do_change_var()">修改自定义变量</button>
  20. <view class="test-v-if">
  21. <view v-if="var_v_if" style="height:var(--var_height);background-color: var(--var_backgroundColor);">
  22. </view>
  23. </view>
  24. <button class="test-v-if-button" @tap="do_vif()">v-if测试</button>
  25. </view>
  26. </template>
  27. <script>
  28. export default {
  29. data() {
  30. return {
  31. statusbar: false,
  32. var_bind: "var_1_bind_1",
  33. var_v_if: false,
  34. text_style_var_def: "text_style_var_1",
  35. text_style_var_1_on: true,
  36. }
  37. },
  38. methods: {
  39. do_change_var() {
  40. let view_container = uni.getElementById("view_container")
  41. if (this.text_style_var_1_on) {
  42. this.text_style_var_def = "text_style_var_2"
  43. this.text_style_var_1_on = false
  44. view_container?.style?.setProperty("--var_flex_direction", "row");
  45. view_container?.style?.setProperty("--var_justify_content", "flex-end");
  46. view_container?.style?.setProperty("--var_margin", "");
  47. // 与 style 中的写法表现不一致,且可能与非简写属性存在冲突
  48. // view_container?.style?.setProperty("border", "var(--var_border)");
  49. view_container?.style?.setProperty("background-color", "var(--background-color)");
  50. } else {
  51. this.text_style_var_def = "text_style_var_1"
  52. this.text_style_var_1_on = true
  53. view_container?.style?.setProperty("--var_margin", "10px");
  54. view_container?.style?.setProperty("--var_flex_direction", "column");
  55. view_container?.style?.setProperty("--var_justify_content", "center");
  56. view_container?.style?.setProperty("background-color", "#FFF");
  57. }
  58. this.var_bind = (this.var_bind == "var_1_bind_2") ? "var_1_bind_1" : "var_1_bind_2"
  59. },
  60. do_vif() {
  61. this.var_v_if = !this.var_v_if
  62. }
  63. }
  64. }
  65. </script>
  66. <style>
  67. .var_1 {
  68. padding-left: env(safe-area-inset-left);
  69. padding-right: env(safe-area-inset-right);
  70. --font-color: #00ff00;
  71. --var_width: 375rpx;
  72. --var_backgroundColor: #ff0000;
  73. }
  74. .var_1_bind_1 {
  75. --var_height: 30px;
  76. }
  77. .var_1_bind_2 {
  78. --var_height: 60px;
  79. }
  80. .var_1_1_1 {
  81. --var_backgroundColor: #0000ff;
  82. }
  83. .content {
  84. flex: 1;
  85. }
  86. .firstParagraph {
  87. background-color: var(--var_backgroundColor);
  88. color: var(--font-color);
  89. }
  90. .secondParagraph {
  91. --font-color: yellow;
  92. background-color: var(--var_backgroundColor);
  93. color: var(--font-color);
  94. }
  95. .text_style_var_1 {
  96. --text-font-size: 26px;
  97. --text-font-style: normal;
  98. --text-color: #ccc;
  99. --text-background-color: #488cff;
  100. }
  101. .text_style_var_2 {
  102. --text-font-size: 16px;
  103. --text-font-style: italic;
  104. --text-color: #803390;
  105. --text-background-color: #48ff32;
  106. }
  107. .text_style {
  108. font-family: var(--swiper-item-font-family);
  109. font-size: var(--text-font-size);
  110. font-style: var(--text-font-style);
  111. color: var(--text-color);
  112. background-color: var(--text-background-color);
  113. }
  114. .view_layout_style_container {
  115. --var_flex_direction: column;
  116. --var_align_self: center;
  117. --var_justify_content: center;
  118. --var_border: 2px red solid;
  119. --var_border2: 2px green solid;
  120. --background-color: #CCC;
  121. --var_height: 110px;
  122. --var_margin: 10px;
  123. height: var(--var_height);
  124. flex-direction: var(--var_flex_direction);
  125. justify-content: var(--var_justify_content);
  126. margin: var(--var_margin);
  127. }
  128. .view_layout_style {
  129. width: 50px;
  130. height: 50px;
  131. background-color: brown;
  132. align-self: var(--var_align_self);
  133. }
  134. </style>