vue根据浏览器窗口大小自适应排列多个div

javascript 复制代码
<template>
    <div class="bigBox">
        <div class="smallBox" :style="{'width':width}" v-for="item in 10" :key="item">
            <div class="contentItem">{{ item }}</div>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            width: '20%'
        };
    },
    created() {
        this.width = 100 / Math.trunc(window.innerWidth / 200) + '%';
        window.addEventListener('resize', this.reSizer);
    },
    destroyed() {
        window.removeEventListener('resize', this.reSizer);
    },
    methods: {
        reSizer() {
            this.width = 100 / Math.trunc(window.innerWidth / 200) + '%';
        }
    }
};
</script>

<style scoped lang="scss">
.bigBox {
    display: flex;
    flex-wrap: wrap;
    .smallBox {
        padding: 10px;
        .contentItem {
            height: 200px;
            border: 1px solid black;
        }
    }
}
</style>
相关推荐
进击的尘埃几秒前
Playwright Component Testing 深度拆解:组件挂载到底干了啥,视觉快照又怎么塞进 CI
javascript
OpenTiny社区26 分钟前
以界面重构文字,GenUI 正式发布!
前端·vue.js·ai编程
yuki_uix33 分钟前
深入理解 JavaScript 的 this:从困惑到掌握的完整指南
前端·javascript
学以智用38 分钟前
Vue 3 项目核心配置文件详解
前端·vue.js
yuki_uix43 分钟前
Promise 与 async/await:从回调地狱到优雅异步的演进之路
前端·javascript
Moment1 小时前
2026 趋势预测:Vibe Coding 之后,人人都会拥有专属 Agent 吗?
前端·javascript·后端
前端Hardy1 小时前
Bun 1.0 正式发布:JavaScript 运行时的新王者?启动快 5 倍,打包小 90%!
前端·javascript·面试
前端Hardy1 小时前
别再乱写正则了!一行 regex 可能让你的网站瘫痪 10 分钟
前端·javascript·面试
摸鱼的春哥2 小时前
【实战】吃透龙虾🦞,你写的Agent也能支持Skills渐进式披露
前端·javascript·后端
孟祥_成都3 小时前
用 AI,0 基础复刻网页顶级特效!😀
前端·javascript·vibecoding