微信小程序

微信小程序实现瀑布流布局(方式一)

2022-02-28  本文已影响0人  苏苏哇哈哈

1.实现效果

瀑布流.gif

2.实现原理

3.实现代码

<view class="content">
  <view class="left">
    <block wx:for="{{list}}" wx:key="index">
      <template is="item" data="{{...item}}" wx:if="{{index%2==0}}"></template>
    </block>
  </view>
  <view class="right">
    <block wx:for="{{list}}" wx:key="index">
      <template is="item" data="{{...item}}" wx:if="{{index%2==1}}"></template>
    </block>
  </view>
</view>
<template name="item">
  <view class="item">
    <image class="item-img" src="{{url}}" mode="widthFix"></image>
    <view class="item-title-box">
      <navigator url="url" class="item-title">{{title}}</navigator>
    </view>
    <view class="name">
      <image class="item-ava" src="{{avatar}}"></image>
      <text class="name-title">{{name}}</text>
      <view class="heart">
        <text>{{num}}</text>
      </view>
    </view>
  </view>
</template>
page {
  padding: 10rpx;
}

.content {
  text-align: justify;
}

.item {
  background-color: #fff;
  margin: 10rpx 3%;
  margin-bottom: 20rpx;
  display: inline-block;
  width: 96%;
  border-radius: 20rpx;
  overflow: hidden;
}

.item-ava {
  width: 40rpx;
  height: 40rpx;
  border-radius: 20rpx;
}

.heart {
  width: 30rpx;
  height: 26rpx;
  margin-right: 8rpx;
}

.heart {
  display: flex;
  align-items: center;
}

.item-img {
  width: 100%
}

.item-title {
  font-size: 24rpx;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
  font-family: 'PingFang SC-Medium';
  color: #1e1e1e;
  margin: 15rpx;
  line-height: 27rpx;
}

.item .name {
  display: flex;
  padding: 0 15rpx;
  margin-top: 20rpx;
  padding-bottom: 10rpx;
  align-items: center;
  font-size: 22rpx;
  color: #1e1e1e;
  font-family: 'PingFang SC-Medium';
}

.name image {
  flex: 0 0 auto;
}

.item-title-box {
  display: flex;
  position: relative;
}

.name-title {
  flex: 1;
  margin-left: 18rpx;
  margin-right: 20rpx;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
}

.name text:last-child {
  color: #c4c4c4;
  text-decoration: underline;
  line-height: 10rpx;

}

.left,
.right {
  display: inline-block;
  vertical-align: top;
  width: 49%;
}
Page({
  data: {
    list: [{
      name: '苏苏',
      num: '1',
      title: '测试数据测试测试测试测测试数据测试测试测试测测试数据测试测试测试测测试数据测试测试测试测',
      url: 'https://gitee.com/susuhhhhhh/su-sus-picture/raw/master/%E5%A5%BD%E7%9C%8B%E5%9B%BE%E7%89%87/4.png',
      avatar: 'https://gitee.com/susuhhhhhh/su-sus-picture/raw/master/%E5%A5%BD%E7%9C%8B%E5%9B%BE%E7%89%87/4.png'
    },
    {
      name: '苏苏2',
      num: '1',
      title: '测试数据测试测试测试测测试数据测试测试测试测测试数据测试测试测试测测试数据测试测试测试测',
      url: 'https://gitee.com/susuhhhhhh/su-sus-picture/raw/master/%E5%A5%BD%E7%9C%8B%E5%9B%BE%E7%89%87/7.png',
      avatar: 'https://gitee.com/susuhhhhhh/su-sus-picture/raw/master/%E5%A5%BD%E7%9C%8B%E5%9B%BE%E7%89%87/7.png'
    },
    {
      name: '苏苏3',
      num: '1',
      title: '测试数据测试测试测试测测试数据测试测试测试测测试数据测试测试测试测测试数据测试测试测试测',
      url: 'https://gitee.com/susuhhhhhh/su-sus-picture/raw/master/%E5%A5%BD%E7%9C%8B%E5%9B%BE%E7%89%87/6.png',
      avatar: 'https://gitee.com/susuhhhhhh/su-sus-picture/raw/master/%E5%A5%BD%E7%9C%8B%E5%9B%BE%E7%89%87/6.png'
    },
    {
      name: '苏苏4',
      num: '1',
      title: '测试数据测试测试测试测测试数据测试测试测试测测试数据测试测试测试测测试数据测试测试测试测',
      url: 'https://gitee.com/susuhhhhhh/su-sus-picture/raw/master/%E5%A5%BD%E7%9C%8B%E5%9B%BE%E7%89%87/3.png',
      avatar: 'https://gitee.com/susuhhhhhh/su-sus-picture/raw/master/%E5%A5%BD%E7%9C%8B%E5%9B%BE%E7%89%87/3.png'
    },
    {
      name: '苏苏5',
      num: '1',
      title: '测试数据测试测试测试测测试数据测试测试测试测测试数据测试测试测试测测试数据测试测试测试测',
      url: 'https://gitee.com/susuhhhhhh/su-sus-picture/raw/master/%E5%A5%BD%E7%9C%8B%E5%9B%BE%E7%89%87/1.png',
      avatar: 'https://gitee.com/susuhhhhhh/su-sus-picture/raw/master/%E5%A5%BD%E7%9C%8B%E5%9B%BE%E7%89%87/1.png'
    },
    {
      name: '苏苏6',
      num: '1',
      title: '测试数据测试测试测试测测试数据测试测试测试测测试数据测试测试测试测测试数据测试测试测试测',
      url: 'https://gitee.com/susuhhhhhh/su-sus-picture/raw/master/%E5%A5%BD%E7%9C%8B%E5%9B%BE%E7%89%87/2.png',
      avatar: 'https://gitee.com/susuhhhhhh/su-sus-picture/raw/master/%E5%A5%BD%E7%9C%8B%E5%9B%BE%E7%89%87/2.png'
    },
    ]
  }
})

4.完整代码,尽在公众号'苏苏的bug',更多小程序demo,关注苏苏的码云,如果对你有用,欢迎您的star+订阅!

上一篇 下一篇

猜你喜欢

热点阅读