ホームページ > ウェブフロントエンド > jsチュートリアル > jQuery+vue.jsで実装した9マスパズルゲームの共有例

jQuery+vue.jsで実装した9マスパズルゲームの共有例

小云云
リリース: 2017-12-29 09:29:36
オリジナル
3054 人が閲覧しました

この記事では、主に jQuery+vue.js で実装された 9 マスのパズル ゲームを紹介し、vue.js と組み合わせた jQuery の関連操作スキルを完全な例の形で分析します。必要な友人は参照してください。それが皆さんのお役に立てれば幸いです。


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <title>Title</title>

  <style>

    * {

      margin: 0;

      padding: 0;

    }

    /*#piclist {

      width: 600px;

      height: 600px;

      background-color: green;

    }*/

    .nitem {

      /*width: 200px;*/

      /*height: 200px;*/

      float: left;

      background: url(img/meinv.jpg) 0px 0px no-repeat;

      -webkit-background-size: 600px 600px;

      background-size: 600px 600px;

      font-size: 33px;

      color: red;

      font-weight: bold;

      cursor: pointer;

    }

    /*.nitem:nth-child(2){

      background-position: -200px 0;

    }

    .nitem:nth-child(3){

      background-position: -400px 0;

    }

    .nitem:nth-child(4){

      background-position: 0px -200px;

    }

    .nitem:nth-child(5){

      background-position: -200px -200px;

    }

    .nitem:nth-child(6){

      background-position: -400px -200px;

    }

    .nitem:nth-child(7){

      background-position: 0px -400px;

    }

    .nitem:nth-child(8){

      background-position: -200px -400px;

    }

    .nitem:nth-child(9){

      background-position: -400px -400px;

    }*/

    .fn-clear {

      clear: both;

      height: 0;

      line-height: 0px;

      font-size: 0px;

    }

  </style>

</head>

<body>

<p id="appbox" :style="{ width:width+&#39;px&#39;,height:height+&#39;px&#39; }">

  <p id="piclist">

    <p class="nitem"

       v-for="(item,index) in itemlist"

       :class="{remove : index === 0}"

       :index="index "

       v-bind:style="{

        &#39;backgroundPosition&#39;:-px(index)+&#39;px -&#39; + py(index) + &#39;px&#39;,

         width : width / rows + &#39;px&#39;,

         height : height / cols + &#39;px&#39;}">{{index}}

    </p>

  </p>

</p>

</body>

<script src=js/jquery-1.9.1.min.js></script>

<script src=js/vue.min.js></script>

<script>

  var olen = 0, oi = 0, cindex = 0, oa = new Array(), oindex = 0, listarray = new Array();

  var vm = new Vue({

    el: "#appbox",

    data: {

      itemlist: [],

      rows: 3,

      cols: 3,

      width: 600,

      height: 600,

    },

    methods: {

      px(index){

        return (index % this.rows) * (this.width / this.rows)

      },

      py (index){

        return parseInt((index / this.cols)) * (this.height / this.cols);

      }

    }

  });

  for (var i = 0; i < vm.rows * vm.cols; i++) {

    vm.itemlist.push(i);

  }

  function getrow(index) {

    return parseInt(index / vm.cols);

  }

  function getcols(index) {

    return index % vm.rows;

  }

  function getBound(index) {

    var left = index - 1;

    var right = index + 1;

    var top = index - vm.rows;

    var bottom = index + vm.rows;

    var len = vm.itemlist.length; //总长度

    var currentRow = getrow(index);

    var currentCol = getcols(index);

    var roundArr = new Array();

    if (left >= 0 && left < len && getrow(left) == currentRow) {

      roundArr.push(left);

    }

    if (right >= 0 && right < len && getrow(right) == currentRow) {

      roundArr.push(right);

    }

    if (top >= 0 && top < len && getcols(top) == currentCol) {

      roundArr.push(top);

    }

    if (bottom >= 0 && bottom < len && getcols(bottom) == currentCol) {

      roundArr.push(bottom);

    }

    return roundArr;

  }

  function box_switch(i, j) {

    var iobj = $(&#39;#piclist .nitem&#39;).eq(i);

    var jobj = $(&#39;#piclist .nitem&#39;).eq(j);

    var tobj = iobj.clone();

    jobj.after(tobj);

    iobj.replaceWith(jobj);

  }

  vm.$nextTick(function () {

    $(&#39;.remove&#39;).css({

      background: &#39;none&#39;,

      backgroundColor: &#39;green&#39;

    });

  });

  function box_rand(times) {

    for (var i = 0; i < times; i++) {

      oindex = $(&#39;.remove&#39;).index();

      oa = getBound(oindex);

      olen = oa.length;

      oi = Math.floor(Math.random() * olen);

      cindex = oa[oi];

      box_switch(cindex, oindex);

    }

    listarray.length = 0;

    $.each($(&#39;.nitem&#39;), function (i, item) {

      listarray.push($(item).attr(&#39;index&#39;));

    });

    if (listarray.join(&#39;,&#39;) == vm.itemlist.join(&#39;,&#39;)) {

      box_rand(times);

    }

  }

  $(function () {

    //打乱图片

    box_rand(20);

    $(&#39;.nitem&#39;).on(&#39;click  &#39;, function () {

      var cindex = $(this).index();

      var oindex = $(&#39;.remove&#39;).index();

      var oRound = getBound(oindex); //空盒子四周的索引

      if ($.inArray(cindex, oRound) < 0) { //不在

        return false;

      } else {

        box_switch(oindex, cindex);

        var listArray = new Array();

        $.each($(&#39;.nitem&#39;), function (i, item) {

          listArray.push($(item).attr(&#39;index&#39;));

        })

        if (listArray.join(&#39;,&#39;) == vm.itemlist.join(&#39;,&#39;)) {

          alert(&#39;success&#39;)

        } else {

          console.log(&#39;失败&#39;)

        }

      }

    });

  })

</script>

</html>

ログイン後にコピー

関連する推奨事項:

vue コンポーネントに基づいた推測ゲームの実装の詳細な例

月餅を食べるゲームの作成に関する HTML5 チュートリアル

js を使用して簡単なゲームを開発および実装する方法ヘビゲーム

以上がjQuery+vue.jsで実装した9マスパズルゲームの共有例の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート