当前位置:   article > 正文

uniapp 图片自适应宽高分单图,双图,多图显示_uniapp图片适配

uniapp图片适配
<template>
    <view class="comment-box">
        <block v-for="(item,index) in list" :key="index">
            <view class="flex">
                <image :src="item.user_image" mode="aspectFill" class="br50p" style="width: 70rpx;height: 70rpx;">
                </image>
                <view class="ml10 flex flex-direction-column ">
                    <view class="fs28 c333">{{item.user_name ||''}}</view>
                    <view class="fs24 c999 ">{{item.comment_time || ''}}</view>
                </view>
            </view>
            <view class="flex flex-direction-column">
                <div class="w500 mt15 fs26 c333">
                    {{item.context || ''}}
                </div>
                <div class="flex flex-wrap   mt15">
                    <block v-for="(img,imgIndex) in item.images" :key="imgIndex">
                        <image @tap="$utils.previewImage(img.url,item.images.map(m=>m.url))" :src="img.url"
                            mode="aspectFill"
                            :class="item.images.length<=1?'big-img':item.images.length<=2?'mid-img':'small-img'" :style="{
                                width:screenWidthHeight(item.images)+'px',
                                height:screenWidthHeight(item.images)+'px',
                            }">
                        </image>
                    </block>
                </div>
            </view>
            <view class="hr" v-if="index!=list.length-1">

            </view>
        </block>
    </view>
</template>

<script>
    export default {
        props: {
            list: {
                type: Array,
                default: () => {
                    return []
                }
            }
        },

        data() {
            return {
                // 视图宽度
                screenWidth: 0,
                // 视图高度
                screenHeight: 0
            }
        },
        mounted() {
            this.screenWidth = uni.getSystemInfoSync().screenWidth;
            this.screenHeight = uni.getSystemInfoSync().screenHeight;
        },
        methods: {
            // 自动计算宽高
            screenWidthHeight(arr) {
                if (arr.length === 1) {
                    return this.screenWidth / 1 - 30;
                } else if (arr.length === 2) {
                    return this.screenWidth / 2 - 40;
                } else {
                    return this.screenWidth / 3 - 30;
                }
            }
        }
    }
</script>

<style lang="scss" scoped>
    .small-img {

        margin-right: 5px;
        margin-bottom: 5px;
        border-radius: 8px;
        border: 1px solid #f1f1f1;

        &:last-child {
            margin-right: 0;
        }
    }

    .big-img {
        margin-right: 5px;
        margin-bottom: 5px;
        border-radius: 8px;
        border: 1px solid #f1f1f1;

        &:last-child {
            margin-right: 0;
        }
    }

    .mid-img {
        margin-right: 10px;
        margin-bottom: 5px;
        border-radius: 8px;
        border: 1px solid #f1f1f1;

        &:last-child {
            margin-right: 0;
        }
    }


    .hr {
        width: 100%;
        height: 2rpx;
        background-color: #ececec;
        margin: 20rpx 0;
    }
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115

效果图:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/954101
推荐阅读
相关标签
  

闽ICP备14008679号