懒惰的数独——lodash的shuffle方法实现随机打乱的效果

1.效果

2.代码

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.14.1/lodash.min.js"></script>
</head>
<body>
<div id="app">
    <h1>数独</h1>
    <p>继续[按下]随机按钮直到你获胜</p>

    <button @click="shuffle" style="background-color: chocolate">
        随机改变
    </button>
    <transition-group name="cell" tag="div" class="container">
        <div v-for="cell in cells" :key="cell.id" class="cell">
            {{ cell.number }}
        </div>
    </transition-group>
</div>
<script>
    new Vue({
        el: '#app',
        data: {
            cells: Array.apply(null, { length: 81 })
                .map((_, index) => {
                    return {
                        id: index,
                        number: index % 9 + 1
                    }
                })
        },
        methods: {
            shuffle () {
                this.cells = _.shuffle(this.cells)
            }
        }
    })
</script>
<style>
    .container {
        display: flex;
        flex-wrap: wrap;
        width: 238px;
        margin-top: 10px;
        background-color: coral;
    }
    .cell {
        display: flex;
        justify-content: space-around;
        align-items: center;
        width: 25px;
        height: 25px;
        border: 1px solid #47a8d0;
        margin-right: -1px;
        margin-bottom: -1px;
    }
    .cell:nth-child(3n) {
        margin-right: 0;
    }
    .cell:nth-child(27n) {
        margin-bottom: 0;
    }
    .cell-move {
        transition: transform 1s;
    }
</style>
</body>
</html>

利用transition-group和lodash的shuffle方法实现打乱功能

其他代码------小试牛刀的增添数据列表​​​​​​​

相关推荐
前端Hardy11 小时前
干掉 Virtual DOM?尤雨溪开始"强推" Vapor Mode?
vue.js·vue-router
Mr_li12 小时前
给 Vue 开发者的 uni-app 快速指南
vue.js·uni-app
icebreaker15 小时前
Weapp-vite:原生模式之外,多一种 Vue SFC 选择
前端·vue.js·微信小程序
icebreaker15 小时前
重走 Vue 长征路 Weapp-vite:编译链路与 Wevu 运行时原理拆解
前端·vue.js·微信小程序
wuhen_n15 小时前
代码生成:从AST到render函数
前端·javascript·vue.js
wuhen_n15 小时前
AST转换:静态提升与补丁标志
前端·javascript·vue.js
destinying16 小时前
性能优化之实战指南:让你的 Vue 应⽤跑得飞起
前端·javascript·vue.js
徐小夕17 小时前
JitWord Office预览引擎:如何用Vue3+Node.js打造丝滑的PDF/Excel/PPT嵌入方案
前端·vue.js·github
SuperEugene19 小时前
后台权限与菜单渲染:基于路由和后端返回的几种实现方式
前端·javascript·vue.js