拼图(Simple Vue 8 Puzzle)

拼图(Simple Vue 8 Puzzle)


更多有趣示例 尽在 知屋安砖社区

示例

在这里插入图片描述

VUE

<template>
  <section>
    <transition-group tag="div" name="slide" id="puzzle">
      <div class="tile" v-for="(tile, i) in tiles" :key="tile" @click="move(i)":class="{ empty: tile === '9' }">
        {{ tile }}
      </div>
    </transition-group>
  </section>
</template>

<script>
export default {
  data() {
    return {
      tiles: _.shuffle('123456789'.split('')),
      solution: '123456789'.split(''),
      moves: [
        [1, 3],
        [0, 2, 4],
        [1, 5],
        [0, 4, 6],
        [1, 3, 5, 7],
        [2, 4, 8],
        [3, 7],
        [4, 6, 8],
        [5, 7]
      ]
    }
  },
  watch: {
    solved(newVal, oldVal) {
      alert('Solved.')
    }
  },
  computed: {
    empty() {
      return this.tiles.findIndex(tile => tile === '9')
    },
    solved() {
      return _.isEqual(this.tiles, this.solution)
    }
  },
  methods: {
    move(i) {
      if (this.moves[i].includes(this.empty)) {
        let tileIndex  = i
        let tile       = this.tiles[i]
        let emptyIndex = this.empty
        
        this.$set(this.tiles, tileIndex, this.tiles[emptyIndex])
        this.$set(this.tiles, emptyIndex, tile)
      }
    }
  }
}
</script>

<style>
html, body, section{
  height: 100%;
  width: 100%;
}

body{
  background: whitesmoke;
}
  
section{
  align-items: center;
  display: flex;
  justify-content: center;
}

#puzzle{
  background: #CCC;
  border: 5px solid black;
  display: grid;
  grid-gap: 1px;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  height: 240px;
  width: 240px;
}
  
.tile{
  align-items: center;
  background: white;
  border-radius: 12px;
  cursor: pointer;
  display: flex;
  font-size: 36px;
  font-weight: bold;
  justify-content: center;
}
  
.tile.empty{
  opacity: 0;
  pointer-events: none;
}
  
.slide-move{
  transition: transform .5s ease;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值