懒惰的数独——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方法实现打乱功能

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

相关推荐
_xaboy6 小时前
FormCreate 设计器 v6.3 正式发布:AI 表单助理3.0登场!
前端·vue.js·低代码·开源·表单设计器
马玉霞7 小时前
vue web端页面组件展示
前端·vue.js
代码煮茶7 小时前
Vue3 权限系统实战 | 从 0 搭建完整 RBAC 权限管理
前端·javascript·vue.js
阳火锅7 小时前
💡 告别类名地狱!Tailwind CSS 语义化转换神器来了
前端·css·vue.js
用户125758524367 小时前
XYGo Admin 即时通讯与异步任务实战:WebSocket 长连接 + 消息队列驱动后台处理
vue.js
PBitW8 小时前
组件封装注意事项
前端·vue.js
i220818 Faiz Ul9 小时前
高校教务|教务管理|基于springboot+vue的高校教务管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·高校教务系统
漂移的电子10 小时前
【el-tree】外层多选,某个属性内层单选
前端·javascript·vue.js
sheeta199811 小时前
Vue 前端基础笔记
前端·vue.js·笔记
前端那点事11 小时前
别再写垃圾组件!Vue3 如何设计「真正可复用」的高质量通用组件
前端·vue.js