好了,现在这个无聊的拖动作品,至少可以打发下你的时间,对不,欣赏下自己的作品吧,接下来,我们开始做正式做数独游戏了。
第一步,我们先生成一个1-9的数字对象矩形,这个矩形可以拖动。
先设计htmltag
- <ol id="numberBox" style="list-style-type: none; width: 90px; height: 90px;">
- </ol>
- #numberBox > li
- {
- width: 30px;
- height: 25px;
- text-align: center;
- font-size: 20px;
- padding-top: 5px;
- float: left;
- color: White;
- }
- $(
- function() {
- [{ number: 1, bgcolor: "#C71585" }, { number: 2, bgcolor: "#800080" }, { number: 3, bgcolor: "#B8860B" },
- { number: 4, bgcolor: "rgb(0,0,128)" }, { number: 5, bgcolor: "rgb(30,144,255)" },
- { number: 6, bgcolor: "rgb(255,165,0)" },
- { number: 7, bgcolor: "hsl(0,75%,50%)" }, { number: 8, bgcolor: "hsl(30,50%,50%)" },
- { number: 9, bgcolor: "hsl(120,75%,38%)"}].forEach(
- function(key, index) {
- $.log(key);
- var li = $("<li>").html(key.number).css("backgroundColor", key.bgcolor).attr("draggable","true");
- $.log(li);
- li[0].addEventListener("dragstart", function() {
- }, false);
- $("#numberBox").append(li);
- }
- );
- }
- );
好,然后你运行下页面,可以看到一个九宫格,并且每一个格子都可以拖动。
第二步,我们要类似的创建我们填数字得的区域了。
首先还是htmlTag
- <ol id="player" style="list-style-type: none; width: 450px; height: 450px;">
- </ol>
- #player .default
- {
- float: left;
- width: 48px;
- height: 33px;
- border: solid 1px rgb(0,0,0);
- font-size: 20px;
- padding-top: 15px;
- text-align: center;
- background-color: #B8860B;
- }
- #player .fix
- {
- float: left;
- width: 48px;
- height: 33px;
- border: solid 1px rgb(0,0,0);
- font-size: 20px;
- padding-top: 15px;
- text-align: center;
- background-color: #FFFABC;
- }
- #player .ation
- {
- float: left;
- width: 48px;
- height: 33px;
- border: solid 1px rgb(0,0,0);
- font-size: 20px;
- padding-top: 15px;
- text-align: center;
- background-color: #FFA07A;
- }
- $(
- function() {
- "500000300090500400004000700051037289302080604008052137035000900609000823080023006".split("").forEach(
- function(item, index) {
- $.log(item);
- var li = $("<li>")
- if (item != "0") {
- li.addClass("fix");
- li[0].innerHTML = item;
- }
- else {
- li[0].classList.add("default");
- li[0].addEventListener("dragenter",
- function(e) {
- $.log(e);
- }, false);
- li[0].addEventListener("dragover",
- function(e) {
- if (e.preventDefault) {
- e.preventDefault(); //不要执行与事件关联的默认动作
- }
- if (e.stopPropagation) {
- e.stopPropagation(); //停止事件的传播
- }
- $.log(e);
- return false;
- }, false);
- li[0].addEventListener("dragleave",
- function(e) {
- }, false);
- li[0].addEventListener("drop",
- function(e) {
- if (e.preventDefault) {
- e.preventDefault(); //不要执行与事件关联的默认动作
- }
- if (e.stopPropagation) {
- e.stopPropagation(); //停止事件的传播
- }
- }, false);
- }
- $("#player").append(li);
- }
- );
- }
- );
