uniapp动态修改样式_Vue如何动态修改CSS变量

Dear 丶 2022-10-13 05:25 2204阅读 0赞

想要动态修改css样式就得能动态的给赋值,可是里面又写不了变量(有可能可以但是我不会。。),所以想了个这种办法,通过给模板文件的:style动态复制从而间接的修改里面的变量

  1. <template>
  2. <div :style="{'--color':'red'}"></div>
  3. </template>

那接下来的就简单了,将red换成data里面的变量,把:style传过去的color变量调用起来

  1. <template>
  2. <div :style="{'--color':color}">
  3. <div class="title color">测试</div>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. data () {
  9. return {
  10. color: 'red'
  11. }
  12. }
  13. }
  14. </script>
  15. <style lang="less" scoped>
  16. .color{
  17. color: var(--color) !important;
  18. }
  19. .title{
  20. color:blue;
  21. }
  22. </style>

案例

  1. <template>
  2. <members-list
  3. title="已报名"
  4. :membersList="membersList" :finished="finished" :loading="loading"
  5. :showFollowBtn="applets !== 3"
  6. @listLoadMore="onLoadList"
  7. class="vant-list-wrap"
  8. :style="{
  9. '--scrollheight':calcScrollHeight
  10. }"
  11. />
  12. </template>
  13. <script>
  14. import { mapState } from "vuex";
  15. export default {
  16. computed: {
  17. ...mapState(["applets"]),
  18. calcScrollHeight(){
  19. // #ifdef MP-WEIXIN
  20. let { windowHeight } = this.systemInfo;
  21. if (this.StatusBar > 20) return `${ windowHeight - this.StatusBar - 44}px`
  22. return `${ windowHeight - this.StatusBar - 44}px`
  23. // #endif
  24. // #ifndef MP-WEIXIN
  25. return '0px';
  26. // #endif
  27. },
  28. },
  29. data() {
  30. return {
  31. membersList: [],
  32. pageNo: 0,
  33. totalCount: 0,
  34. loading: false,
  35. finished: false,
  36. systemInfo:{},
  37. };
  38. },
  39. mounted(){
  40. // #ifdef MP-WEIXIN
  41. let that = this;
  42. wx.getSystemInfo({
  43. success: function (res) {
  44. that.systemInfo = res
  45. },
  46. });
  47. // #endif
  48. },
  49. methods: {
  50. onLoadList() {
  51. if (!this.finished) {
  52. this.loading = true;
  53. let actId = this.$route.query.id;
  54. this.pageNo += 1;
  55. this.getActMembersList(this.pageNo, 12, actId);
  56. }
  57. },
  58. getActMembersList(pageNo, pageSize, actId) {
  59. this.$axios
  60. .post("/social-cms-app/frontend/activity/actSign/queryByPage", {
  61. pageNo: pageNo,
  62. pageSize: pageSize,
  63. actId: actId,
  64. })
  65. .then(({ data }) => {
  66. this.loading = false;
  67. if (!data) { // 话题禁用时为null
  68. this.finished = true;
  69. return;
  70. }
  71. else {
  72. let list = data.list || [];
  73. this.membersList.push(...list);
  74. console.log("getmembersList");
  75. console.log(data);
  76. this.totalCount = data.totalCount;
  77. if (this.membersList.length >= this.totalCount) {
  78. this.finished = true;
  79. }
  80. }
  81. });
  82. },
  83. }
  84. };
  85. </script>
  86. <style lang="scss" scoped>
  87. // #ifdef MP-WEIXIN
  88. .page{
  89. height: 100vh;
  90. overflow: hidden;
  91. }
  92. .vant-list-wrap{
  93. ::v-deep .page{
  94. // height: calc(100vh - 128px);
  95. padding-bottom: 0;
  96. }
  97. ::v-deep .members-list{
  98. height: var(--scrollheight)!important;
  99. padding: 0!important;
  100. van-list{
  101. scroll-view{
  102. height: var(--scrollheight)!important;
  103. .member{
  104. width: calc(100vw - 40px);
  105. margin: 0 auto;
  106. padding: 0 20px;
  107. border: none;
  108. position: relative;
  109. }
  110. .member::after{
  111. content: "";
  112. position: absolute;
  113. height: 1px;
  114. background: #F7F8FA;
  115. width: calc(100vw - 40px);
  116. bottom: 0;
  117. left: 50%;
  118. transform: translateX(-50%);
  119. }
  120. }
  121. }
  122. }
  123. }
  124. // #endif
  125. </style>

如果想要px不被编译,这使用 —startuspx 动态变量,通过:style方式避过css编译

  1. <div class="page" :style="{'--fixedtop': fixedTopCalc,'--navbarheight':'44px'}"></div>

发表评论

表情:
评论列表 (有 0 条评论,2204人围观)

还没有评论,来说两句吧...

相关阅读