drag drop小游戏
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style rel="stylesheet">
.goods {
width: 500px;
border: 1px solid red;
padding: 30px;
height: 150px;;
} .good {
float: left;;
margin: 10px 20px;;
width: 100px;
height: 100px;;
} #one { background: green; } #two { background: black;
color: #ffffff;
} #three { background: pink;
} .destination {
width: 600px;;
height: 500px;
border: 1px solid deepskyblue;
}
</style>
</head>
<body>
默认顺序 1 2 3
<div class="goods">
物品容器
<div class="good" id="temp" data-type="one"> </div>
<div class="good" id="" data-type="two">2</div> </div> 仓库
<div class="destination"> </div>
<script type="text/javascript"> //容器类
function Container(obj) {
this.goods = document.querySelectorAll(obj.goods) || [];
this.rule = obj.rule || [];
this.relSort = [];
this.container = document.querySelector(obj.container) || document.body;
this.ondrag = obj.ondrag || null;
this.addEvent(this.goods);
this.count = this.goods.length;
this.success = obj.success || null;
this.failed = obj.failed || null; } Container.prototype.compare = function (arr1, arr2) {
var result = true;
arr1.forEach(function (item, index) {
if (item != arr2[index]) {
result = false;
}
})
return result;
} Container.prototype.addEvent = function (goods) { var me = this;
Array.prototype.forEach.call(goods, function (item) {
item.setAttribute('draggable', true);
item.ondragstart = function (e) {
if (me.ondrag) {
e.dataTransfer.setData("id", e.target.id);
me.ondrag.call(me, e);
}
}
});
this.container.ondrop = function (e) {
e.preventDefault();
var source = e.dataTransfer.getData("id");
e.target.appendChild(document.querySelector('#' + source));
me.relSort.push(source);
if (me.relSort.length == me.count) {
if (me.compare(me.rule, me.relSort)) {
me.success && me.success.call(me);
} else {
me.failed && me.failed.call(me);
}
}
}
this.container.ondragover = function (e) {
e.preventDefault();
} } var a = new Container({
goods: '.good',//需呀拖动物体的选择器
rule: ['one', 'two', 'three'], //正确的顺序
container: '.destination',// 载体选择器
ondrag: function (e) { //开始拖动事件
},
success: function () {//如果游戏成功 回调函数
alert('你赢了');
},
failed: function () {//如果游戏失败 回调函数
alert('游戏失败');
} }); </script> </body>
</html>
drag drop小游戏的更多相关文章
- canvas drag 实现拖拽拼图小游戏
博主一直心心念念想做一个小游戏- 前端时间终于做了一个小游戏,直到现在才来总结,哈哈- 以后要勤奋点更新博客! 实现原理 1.如何切图? 用之前的方法就是使用photoshop将图片切成相应大小的图 ...
- HTML5魔法堂:全面理解Drag & Drop API
一.前言 在HTML4的时代,各前端工程师为了实现拖拽功能可说是煞费苦心,初听HTML5的DnD API觉得那些痛苦的日子将一去不复返,但事实又是怎样的呢?下面我们一起来看看DnD API的真面 ...
- Draggabilly – 轻松实现拖放功能(Drag & Drop)
Draggabilly 是一个很小的 JavaScript 库,专注于拖放功能.只需要简单的设置参数就可以在你的网站用添加拖放功能.兼容 IE8+ 浏览器,支持多点触摸.可以灵活绑定事件,支持 Req ...
- 【开源】微信小程序、小游戏以及 Web 通用 Canvas 渲染引擎 - Cax
Cax 小程序.小游戏以及 Web 通用 Canvas 渲染引擎 Github → https://github.com/dntzhang/cax 点我看看 DEMO 小程序 DEMO 正在审核中敬请 ...
- 2048小游戏(Java)(swing实现)(二)
这里是上一次的成果,只能用鼠标点,没法用键盘 最近扩充了一下知识面,实现了用键盘操控2048小游戏 但是还是不支持同时使用键盘和鼠标同时操作 import javax.swing.*; //impor ...
- 2048小游戏(Java)(swing实现)(一)
自己写的2048小游戏,仅支持鼠标操作 主要是我不知道怎么添加键盘监听 import javax.swing.*; import java.awt.*; import java.awt.event.* ...
- 仿苹果电脑任务栏菜单&&拼图小游戏&&模拟表单控件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【重点突破】——Drag&Drop拖动与释放
一.引言 在学习HTML5新特性的时候,学到了Drag&Drop这两种拖放API,这里根据拖动的是“源对象”还是“目标对象”做两个小练习,主要是为了理解与应用HTML5为拖放行为提供的7个事件 ...
- Chrome自带恐龙小游戏的源码研究(完)
在上一篇<Chrome自带恐龙小游戏的源码研究(七)>中研究了恐龙与障碍物的碰撞检测,这一篇主要研究组成游戏的其它要素. 游戏分数记录 如图所示,分数及最高分记录显示在游戏界面的右上角,每 ...
随机推荐
- [改善Java代码]使用匿名类的构造函数
建议39: 使用匿名类的构造函数 阅读如下代码,看看是否可以编译: public class Client { public static void main(String[] args) { Lis ...
- ubuntu不能正常使用make menuconfig的解决方案
so easy sudo apt-get install build-essentialsudo apt-get install libncurses5sudo apt-get install lib ...
- EasyUI的datagrid获取所有正在编辑状态的行的行编号
今天项目需要用了下EasyUI的datagrid的行编辑功能,跟着API来,只要是将各种状态时的处理逻辑弄好,还是蛮不错的. 开发过程中,遇到了个问题,在编辑完成后我需要获取datagrid所有处于编 ...
- Asp_CRUD
Asp_增删改查.逻辑流程 启动服务器. 地址为127.0.0.1 端口为随机分配 2607 然后在浏览器中输入http://localhost:2670/CRUD_main.ashx 浏览器像服务器 ...
- Asp.Net生命周期
最近回顾了一些新知识,在网上搜索了一下生命周期的相关知识:在这里与大家一起分享一下: Asp.net是微软.Net战略的一个组成部分.它相对以前的Asp有了很大的发展,引入了许多的新机制.本文就Asp ...
- unity3d所要知道的基础知识体系大纲,可以对照着学习,不定期更新
本文献给,想踏入3D游戏客户端开发的初学者. 毕业2年,去年开始9月开始转作手机游戏开发,从那时开始到现在一共面的游戏公司12家,其中知名的包括搜狐畅游.掌趣科技.蓝港在线.玩蟹科技.天神互动.乐元素 ...
- Centos6.5 64linux系统基础优化(一)
1 SecureCRT配色方案 http://blog.csdn.net/zklth/article/details/8937905 2 32位和64位Centos linux系统的区别及实际查看 ...
- presentViewController: 如何不覆盖原先的 viewController界面
PresentViewController 如何不遮挡住原来的viewController界面呢? 可能有时候会遇到这种需求,需要弹出一个功能比较独立的视图实现一些功能,但是却不想单纯添加一个View ...
- 从0开始学习react(三)
这次我们来讲解第三节知识,考虑了下,先不去讲什么理论了,毕竟网上一搜一大堆,而且理论真心看不太懂啊!!! 今天我们就直接上实例喽! 大家HIGH起来!!!(想了好久,还是没舍得删这句话) 1.根据下图 ...
- css3学习笔记之背景
background-size background-size指定背景图像的大小 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 &l ...