• 微信小程序瀑布流布局


    1. <template>
    2. <view id="max_box">
    3. <view class="page-main">
    4. <view class="card">
    5. <view class="coloum1" >
    6. <view class="card-item" v-for="(item,index) in cardList1" :key="item.id" :style="[{background: item.color}]">
    7. <image class="itemImg" :src="item.f_image" mode="widthFix">image>
    8. <view class="item_text" >{{item.f_id}}view>
    9. view>
    10. view>
    11. <view class="coloum2">
    12. <view class="card-item" v-for="(item,index) in cardList2" :key="item.id" :style="[{background: item.color}]">
    13. <image class="itemImg" :src="item.f_image" mode="widthFix">image>
    14. <view class="item_text" >{{item.f_id}}view>
    15. view>
    16. view>
    17. view>
    18. view>
    19. view>
    20. template>
    21. <script>
    22. // 获取系统信息
    23. const systemInfo = wx.getSystemInfoSync();
    24. // 获取屏幕宽度
    25. const screenWidth = systemInfo.screenWidth;
    26. import store from '@/store/index.js'
    27. const myrpx = store.state.myrpx
    28. import http from '@/util/http.js'
    29. export default {
    30. data() {
    31. return {
    32. scrollTop: 0,
    33. old: {
    34. scrollTop: 0
    35. },
    36. num: '0',
    37. cardList:[
    38. ],
    39. leftHeight:0,//左侧列表累计高度
    40. rightHeight:0,//右侧列表累计高度
    41. commonHeigt:100, //瀑布流卡片 除图片外的固有高度
    42. cardList1:[],//左侧列表渲染数据
    43. cardList2:[],//右侧列表渲染数据
    44. }
    45. },
    46. async onLoad() {
    47. this.getPublishedforum()
    48. },
    49. methods: {
    50. init(){
    51. //初始化两侧累计高度、两侧表数据
    52. //重新通过接口获取数据
    53. this.leftHeight = 0
    54. this.rightHeight = 0
    55. //获取图片宽高比
    56. this.cardList = this.cardList.filter((item,index)=>{
    57. //获取图片宽高比
    58. uni.getImageInfo({
    59. src: item.f_image,
    60. success: res => {
    61. // 计算宽高比
    62. const ratio = res.width / res.height
    63. //图片真实高度= 真实宽度 / 宽高比 myrpx当前机型 px换算成rpx此处省略
    64. item.trueHeight = (screenWidth * myrpx * 0.48) / ratio
    65. if(this.leftHeight <= this.rightHeight){ //如果右侧列大于左侧列的总高度 则添加左边
    66. this.cardList1.push(item)
    67. this.leftHeight =this.leftHeight + item.trueHeight + this.commonHeigt
    68. }else{
    69. this.cardList2.push(item)
    70. this.rightHeight =this.rightHeight + item.trueHeight + this.commonHeigt
    71. }
    72. }
    73. })
    74. return item
    75. })
    76. },
    77. // 获取发布的帖子数据
    78. getPublishedforum() {
    79. http('/getPublishedpost', 'POST',
    80. {
    81. start:0,
    82. count:10
    83. },
    84. )
    85. .then((res) => {
    86. this.cardList = res[1].data
    87. this.init()
    88. })
    89. },
    90. }
    91. }
    92. script>
    93. <style lang="less">
    94. .page-main{
    95. width: 100%;
    96. height: 100%;
    97. display: flex;
    98. justify-content: center;
    99. .card{
    100. display: grid;
    101. grid-template-columns: repeat(2,1fr);
    102. grid-gap: 10px; /* 列之间和行之间的间隙 */
    103. }
    104. .card-item{
    105. width: 400rpx;
    106. image{
    107. width: 100%;
    108. }
    109. margin-bottom: 10rpx;
    110. }
    111. .item_text{
    112. width: 100%;
    113. height: 100rpx;
    114. background-color: skyblue;
    115. z-index: 999;
    116. }
    117. }
    118. style>

  • 相关阅读:
    JAVA面试中的SSM框架基础面试题
    Java集成腾讯云OCR身份证识别接口
    day06-工作组和域
    LSTM 浅析
    700. 二叉搜索树中的搜索
    __builtin_expect(x,0)
    计算机毕业设计(附源码)python在线电影推荐系统
    RabbitMQ-08 不公平分发与预取值
    用Google CDC来同步Cloud SQL的数据到Bigquery
    原理图与 PCB 绘制备忘
  • 原文地址:https://blog.csdn.net/T3165919332/article/details/132874199