【前端】【Vue】Vue3自适应瀑布流解决方案

简介: 【前端】【Vue】Vue3自适应瀑布流解决方案

Vue3自适应瀑布流解决方案


q1.png

效果如上图所示。

说明:Vue3、[vue-masonry插件](vue-masonry - npm (npmjs.com))

建议查看官方文档vue-masonry (https://www.npmjs.com/package/vue-masonry)

安装

npm install vue-masonry --save

main.js

import {VueMasonryPlugin} from 'vue-masonry';
app.use(VueMasonryPlugin)

HTML骨架

<div v-masonry fit-width="true" transition-duration="0.3s" item-selector=".card" origin-left="false" class="pets">
<div v-masonry-tile v-for="pet in pets" :key="pet['code']" class="card">
</div>
</div>

需要提醒的是:fit-width="true" 自动调整宽度,以避免出现左边或者右边出现多余空白

本页完整代码

<template>
    <div style="width:100%;margin:auto;">
        <div v-masonry fit-width="true" transition-duration="0.3s" item-selector=".card" origin-left="false" class="pets">
            <div v-masonry-tile v-for="pet in pets" :key="pet['code']" class="card">
                <n-card>
                    <p>
                        <n-tag style="margin:2px;" type="info">{{ pet["type"] }}</n-tag>
                        <n-tag style="margin:2px;" type="info">{{ pet["kind"] }}</n-tag>
                        <n-tag> {{ pet['name'] }} </n-tag>
                    </p>
                    <template #cover>
                        <img :src="pet['pic']">
                    </template>
                    <p>
                        {{ pet['name'] }},出生于{{ pet['birth'] }},
                        {{
                                pet['gender'] === 0 ? "雌" : pet['gender'] === 1 ? "雄" : pet['gender'] === 2 ? "雌雄共体" : "未知"
                        }},性格{{ pet['disposition'] }},目前健康状态为{{ pet['health'] }}。
                        目前 <n-tag type="success">{{
                                pet['state'] === 0 ? "未被领养" : "已被领养"
                        }}</n-tag>
                    </p>
                    <p style="text-align:right;">
                        <n-button type="primary" v-if="pet['state'] === 0 ? true : false">
                            我要领养
                        </n-button>
                    </p>
                </n-card>
            </div>
        </div>
    </div>
</template>
<script>
import { defineComponent } from 'vue'
import { NCard, NTag, NButton } from 'naive-ui'
import axios from 'axios'
export default defineComponent({
    components: {
        NCard, NTag, NButton,
    },
    data() {
        return {
            pets: [],
        }
    },
    mounted() {
        this.search("")
    },
    methods: {
        search(keyword) {
            axios({
                method: 'post',
                url: '/pet/search',
                params: {
                    keyword: keyword,
                },
                headers: {
                    'Content-Type': 'application/json;charset=UTF-8'
                },
            }).then((response) => {
                this.pets = response.data["data"]
            })
        }
    }
})
</script>
<style>
.pets {
    width:80%;
    margin: 0 auto;
    border: 1px solid red;
}
.card {
    width: 100%;
    max-width: 250px;
    margin:0.25em;
    border-radius: 5px;
    box-shadow: 0px 2px 10px 1px rgba(0, 0, 0, 0.1);
}
.card img {
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
}
</style>
相关文章
|
5天前
|
前端开发 JavaScript API
阿珊比较Vue和React:两大前端框架的较量
阿珊比较Vue和React:两大前端框架的较量
|
4天前
|
JavaScript 前端开发 CDN
vue3速览
vue3速览
14 0
|
4天前
|
前端开发 JavaScript 中间件
Vue3整合VxeTable,2024大厂前端面试
Vue3整合VxeTable,2024大厂前端面试
|
4天前
|
设计模式 JavaScript 前端开发
Vue3报错Property “xxx“ was accessed during render but is not defined on instance
Vue3报错Property “xxx“ was accessed during render but is not defined on instance
|
4天前
|
JavaScript API
Vue3 官方文档速通(中)
Vue3 官方文档速通(中)
20 0
|
4天前
|
缓存 JavaScript 前端开发
Vue3 官方文档速通(上)
Vue3 官方文档速通(上)
26 0
|
4天前
|
前端开发 JavaScript Java
Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)五(前端页面
Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)五(前端页面
Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)五(前端页面
|
4天前
Vue3+Vite+Pinia+Naive后台管理系统搭建之五:Pinia 状态管理
Vue3+Vite+Pinia+Naive后台管理系统搭建之五:Pinia 状态管理
8 1
|
4天前
Vue3+Vite+Pinia+Naive后台管理系统搭建之三:vue-router 的安装和使用
Vue3+Vite+Pinia+Naive后台管理系统搭建之三:vue-router 的安装和使用
10 0
|
4天前
Vue3+Vite+Pinia+Naive后台管理系统搭建之二:scss 的安装和使用
Vue3+Vite+Pinia+Naive后台管理系统搭建之二:scss 的安装和使用
8 0