微信小程序实现css 瀑布流布局方法

在微信小程序中经常使用瀑布流来进行页面的布局,今天就遇到了这样的情况,之前是一直用flex布局来着,但是flex布局带来的问题是图片高度不同那么进行布局的时候有些图片的下面就会留出很多富余的空间这样看着就不是很好了,所以在这里采用瀑布流的方法:Multi-column,废话少说上代码:

index.wxml 片段(直接用到了瀑布流布局)

<view class='type-easy'>
            <block wx:for='{{easyItemArray}}'>
                <view class='typeDetail-con'>
                    <!--利用组件显示文章信息-->
                    <index-type-detail-component easyItem='{{item}}'></index-type-detail-component>
                </view>
            </block>
        </view>

index.css代码:(主要看type-easy)

.type-easy{
    padding: 0rpx 23rpx;
    /* flex-wrap: wrap;
    align-items: flex-start;
    vertical-align: bottom; */
    /*
    两列显示
    */
    column-count: 2;
    column-gap: 18rpx;

}

.typeDetail-con:nth-child(2n+1){
    margin-right: 18rpx;
}
.typeDetail-con:nth-child(2n){
    margin-right: 0rpx;
}

组件index-type-detail-component

<!--components/index-type-detail-component/index-type-detail-component.wxml-->
<view class="con">
    <image src='{{easyItem.goodsPicture}}' style="width:343rpx;height:auto;" mode='widthFix' class="im"></image>
    <view class="text">{{easyItem.text}}</view>
    <view class="author-con">
        <image src='{{easyItem.authorAvatar}}' style="width:32rpx;height:32rpx;border-radius:32rpx;"></image>
        <view style="font-size:20rpx;margin-left:10rpx;">{{easyItem.author}}</view>
    </view>
    <image src='../../resources/cover.png' style="width:128rpx;height:36rpx;" class="im_t"></image>
    <text class="watched-count">{{easyItem.watched}}</text>
</view>

组件css :主要是.con的样式

/* components/index-type-detail-component/index-type-detail-component.wxss */
.con,.author-con{
    display: flex;
    justify-content: flex-start;
    align-items: center;
    flex-wrap: nowrap;
}

.con{
    position: relative;
    float: left;
    width:343rpx;
    flex-direction: column;
    border-radius: 10rpx;
    /* margin-right: 18rpx; */
    box-shadow: 0rpx 8rpx 8rpx #d0d0d0;
    -webkit-box-shadow:0rpx 8rpx 8rpx #d0d0d0;
    margin-bottom: 18rpx;
    padding-bottom: 10rpx;
    /*
        避免在元素中断行产生新列
    */
    -webkit-column-break-inside: avoid;
    break-inside: avoid;
    /* counter-increment:item-counter; */
}

.author-con{
    width: 100%;
    flex-direction: row;
    margin-top:5rpx;margin-left:10rpx;
}
.im{
    border-top-left-radius: 10rpx;
    border-top-right-radius: 10rpx;
}
.im_t{
    border-top-right-radius: 10rpx;
    position: absolute;
    top: 0rpx;
    right: 0rpx;
}
.watched-count{
    position: absolute;
    top: 0rpx;
    right: 35rpx;
    color:#fff;
    font-size: 25rpx;
}
.text{
    font-size:25rpx;margin-top:5rpx;margin-left:10rpx;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现微信小程序瀑布流布局的代码如下: ``` <!-- .wxml --> <scroll-view scroll-y="true" bindscrolltolower="loadMoreData"> <view class="waterfall"> <view class="waterfall-cell" wx:for="{{items}}" wx:key="{{item.id}}"> <image class="waterfall-img" src="{{item.imgUrl}}" mode="aspectFit"></image> <view class="waterfall-title">{{item.title}}</view> </view> </view> </scroll-view> /* .wxss */ .waterfall { display: flex; flex-wrap: wrap; justify-content: space-between; padding: 10rpx; } .waterfall-cell { width: 49%; margin-bottom: 10rpx; background-color: #fff; border-radius: 10rpx; box-shadow: 0 2rpx 10rpx rgba(0,0,0,.2); } .waterfall-img { width: 100%; height: 0; padding-bottom: 100%; border-radius: 10rpx 10rpx 0 0; } .waterfall-title { padding: 10rpx; font-size: 28rpx; color: #333; } /* .js */ Page({ data: { items: [ { id: 1, title: 'Item 1', imgUrl: 'item1.jpg' }, { id: 2, title: 'Item 2', imgUrl: 'item2.jpg' }, { id: 3, title: 'Item 3', imgUrl: 'item3.jpg' }, ... ] }, loadMoreData() { // TODO: 加载更多数据 } }) ``` 以上代码中,我们使用了 `scroll-view` 组件实现了垂直滚动,并且绑定了 `bindscrolltolower` 事件,当用户滑动到底部时会触发该事件。在 `scroll-view` 内部,使用了一个 `view` 容器包裹所有的瀑布流单元格。每个瀑布流单元格使用一个 `view` 组件实现,包含了一张图片和一个标题。在 CSS 样式中,我们使用了 Flex 布局实现瀑布流布局,并设置了每个单元格的宽度、高度、margin等属性,并使用圆角和阴影使其更加美观。在 JS 代码中,我们使用了一个数组 `items` 存储了所有的数据,并且定义了一个 `loadMoreData` 函数,用于加载更多数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值