JavaScript拖拽效果 - 实现问答比赛游戏的脚本

2008-12-31 09:19:03 | 【

一个非常棒的依靠JavaScript拖拽实现的问答游戏,出现一堆问题和答案,然后把答案拖放到相应的问题上,如果正确,答案背景显示为绿色,如果错误显示为红色,用来做智力问答非常不错

查看演示页面 View Demo 查看全部代码 View Code

在网页<head>区添加以下代码

<style type="text/css">
	/* DEMO CSS */
	body{
		width:100%;
		margin:0px;
		padding:0px;
		font-family: Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif;	/* Font to use */
	}
	#heading{
		height:100px;
	}
	
	/* END DEMO CSS */
	
	#dragScriptContainer{	/* BIG DIV containing HTML needed for the entire script */
		width:400px;
		margin:0 auto;
		border:1px solid #000;
		height:400px;
		margin-top:20px;
		padding:3px;
		-moz-user-select:no;
	}
	
	#questionDiv{	/* Big div for all the questions */
		float:left;
		height:100%;
		width:100px;
		border:1px solid #000;
		padding:2px;
	}
	#answerDiv{	/* Big div for all the answers */
		float:right;
		height:100%;
		width:50px;
		border:1px solid #000;
		padding:2px;	
	}
	
	#questionDiv div,#answerDiv div,#dragContent div{	/* General rules for small divs - i.e. specific questions and answers */
		width:45px;
		height:20px;
		line-height:20px;		
		float:left;
		margin-right:2px;
		margin-bottom:2px;
		text-align:center;
	}
	
	#dragContent div{	/* Drag content div - i.e. specific answers when they are beeing dragged */
		border:1px solid #000;
	}
	
	#answerDiv .dragDropSmallBox{	/* Small answer divs */
		border:1px solid #000;
		cursor:pointer;
	}
	
	#questionDiv .dragDropSmallBox{	/* Small answer divs */
		border:1px solid #000;
		cursor:pointer;
		background-color:#E2EBED; /* Light blue background color */
	}
	
	#questionDiv div div{	/* DIV after it has been dragged from right to left box */
		margin:0px;
		border:0px;
		padding:0px;
		background-color:#FFF;
	}
	#questionDiv .destinationBox{	/* Small empty boxes for the questions - i.e. where answers could be dragged */
		border:0px;
		background-color:#DDD;
		width:47px;
		height:22px;
		
	
		
	}
	#questionDiv .correctAnswer{	/* CSS indicating correct answer */
		background-color:green;
		color:#fff;
		border:1px solid #000;
	}
	#questionDiv .wrongAnswer{	/* CSS indicating wrong answer */
		background-color:red;
		color:#fff;
		border:1px solid #000;
	}

	#dragContent div{
		background-color:#FFF;
	}

	#questionDiv .dragContentOver{	/* Mouse over question boxes - i.e. indicating where dragged element will be appended if mouse button is relased */
		border:1px solid #F00;
	}
	
	#answerDiv.dragContentOver{	/* Mouse over answer box - i.e. indicating where dragged element will be appended if mouse button is relased */
		border:1px solid #F00;
	}
	
	/* NEVER CHANGE THIS */
	#dragContent{
		position:absolute;
		display:none;
	}	
	
	</style>
	<script type="text/javascript">
                
        /*****************************************************
         *  Share JavaScript (http://www.ShareJS.com)
         * 使用此脚本程序,请保留此声明
         * 获取此脚本以及更多的JavaScript程序,请访问 http://www.ShareJS.com
         ******************************************************/
    
	/************************************************************************************************************
	(C) www.dhtmlgoodies.com, November 2005
	
	This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	
	
	Terms of use:
	You are free to use this script as long as the copyright message is kept intact. However, you may not
	redistribute, sell or repost it without our permission.
	
	Thank you!
	
	www.dhtmlgoodies.com
	Alf Magne Kalleland
	
	************************************************************************************************************/
		
	var shuffleQuestions = true;	/* Shuffle questions ? */
	var shuffleAnswers = true;	/* Shuffle answers ? */
	var lockedAfterDrag = false;	/* Lock items after they have been dragged, i.e. the user get's only one shot for the correct answer */
	
	function quizIsFinished()
	{
		// This function is called when everything is solved		
		
	}
	
	
	/* Don't change anything below here */
	var dragContentDiv = false;
	var dragContent = false;
	
	var dragSource = false;
	var dragDropTimer = -1;
	var destinationObjArray = new Array();
	var destination = false;
	var dragSourceParent = false;
	var dragSourceNextSibling = false;
	var answerDiv;
	var questionDiv;	
	var sourceObjectArray = new Array();
	var arrayOfEmptyBoxes = new Array();
	var arrayOfAnswers = new Array();
	
	function getTopPos(inputObj)
	{		
	  if(!inputObj || !inputObj.offsetTop)return 0;		
	  var returnValue = inputObj.offsetTop;
	  while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetTop;
	  return returnValue;
	}
	
	function getLeftPos(inputObj)
	{
	  if(!inputObj || !inputObj.offsetLeft)return 0;	
	  var returnValue = inputObj.offsetLeft;
	  while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetLeft;
	  return returnValue;
	}
		
	function cancelEvent()
	{
		return false;
	}
	
	function initDragDrop(e)
	{
		if(document.all)e = event;
		if(lockedAfterDrag && this.parentNode.parentNode.id=='questionDiv')return;
		dragContentDiv.style.left = e.clientX  + Math.max(document.documentElement.scrollLeft,document.body.scrollLeft) + 'px';
		dragContentDiv.style.top = e.clientY  + Math.max(document.documentElement.scrollTop,document.body.scrollTop) + 'px';
		dragSource = this;
		dragSourceParent = this.parentNode;
		dragSourceNextSibling = false;
		if(this.nextSibling)dragSourceNextSibling = this.nextSibling;
		if(!dragSourceNextSibling.tagName)dragSourceNextSibling = dragSourceNextSibling.nextSibling;
		
		dragDropTimer=0;
		timeoutBeforeDrag();
		
		return false;
	}
	
	function timeoutBeforeDrag(){
		if(dragDropTimer>=0 && dragDropTimer<10){
			dragDropTimer = dragDropTimer +1;
			setTimeout('timeoutBeforeDrag()',10);
			return;
		}
		if(dragDropTimer>=10){
			dragContentDiv.style.display='block';
			dragContentDiv.innerHTML = '';
			dragContentDiv.appendChild(dragSource);
		
			
		}		
	}
	
	function dragDropMove(e)
	{
		if(dragDropTimer<10){
			return;
		}
		
		if(document.all)e = event;
		
		var scrollTop = Math.max(document.documentElement.scrollTop,document.body.scrollTop);
		var scrollLeft = Math.max(document.documentElement.scrollLeft,document.body.scrollLeft);
		
		dragContentDiv.style.left = e.clientX + scrollLeft + 'px';
		dragContentDiv.style.top = e.clientY + scrollTop + 'px';
		
		var dragWidth = dragSource.offsetWidth;
		var dragHeight = dragSource.offsetHeight;
		

		var objFound = false;
		
		var mouseX = e.clientX + scrollLeft;
		var mouseY = e.clientY + scrollTop;
		
		destination = false;
		for(var no=0;no<destinationObjArray.length;no++){
			var left = destinationObjArray[no]['left'];
			var top = destinationObjArray[no]['top'];
			var width = destinationObjArray[no]['width'];
			var height = destinationObjArray[no]['height'];
			
			destinationObjArray[no]['obj'].className = 'destinationBox';
			var subs = destinationObjArray[no]['obj'].getElementsByTagName('DIV');
			if(!objFound && subs.length==0){
				if(mouseX < (left/1 + width/1) && (mouseX + dragWidth/1) >left && mouseY < (top/1 + height/1) && (mouseY + dragHeight/1) >top){
					destinationObjArray[no]['obj'].className='dragContentOver';
					destination = destinationObjArray[no]['obj'];					
					objFound = true;
				}		
			}	
		}
		
		sourceObjectArray['obj'].className='';
		
		if(!objFound){
			var left = sourceObjectArray['left'];
			var top = sourceObjectArray['top'];
			var width = sourceObjectArray['width'];
			var height = sourceObjectArray['height'];
						
			if(mouseX < (left/1 + width/1) && (mouseX + dragWidth/1) >left && mouseY < (top/1 + height/1) && (mouseY + dragHeight/1) >top){
				destination = sourceObjectArray['obj'];
				sourceObjectArray['obj'].className='dragContentOver';
			}
		}
		return false;
	}
	
	
	function dragDropEnd()
	{
		if(dragDropTimer<10){
			dragDropTimer = -1;
			return;
		}
		dragContentDiv.style.display='none';
		sourceObjectArray['obj'].style.backgroundColor = '#FFF';
		if(destination){
			destination.appendChild(dragSource);
			destination.className='destinationBox';
			
			// Check if position is correct, i.e. correct answer to the question
			
			if(!destination.id || destination.id!='answerDiv'){
				var previousEl = dragSource.parentNode.previousSibling;
				if(!previousEl.tagName)previousEl = previousEl.previousSibling;
				var numericId = previousEl.id.replace(/[^0-9]/g,'');
				var numericIdSource = dragSource.id.replace(/[^0-9]/g,'');
				
				if(numericId==numericIdSource){
					dragSource.className='correctAnswer';
					checkAllAnswers();	
				}
				else
					dragSource.className='wrongAnswer';				
			}
			
			if(destination.id && destination.id=='answerDiv'){
				dragSource.className='dragDropSmallBox';
			}
			
		}else{
			if(dragSourceNextSibling)
				dragSourceNextSibling.parentNode.insertBefore(dragSource,dragSourceNextSibling);
			else
				dragSourceParent.appendChild(dragSource);
		}
		dragDropTimer = -1;
		dragSourceNextSibling = false;
		dragSourceParent = false;
		destination = false;
	}
	
	function checkAllAnswers()
	{	
		for(var no=0;no<arrayOfEmptyBoxes.length;no++){
			var sub = arrayOfEmptyBoxes[no].getElementsByTagName('DIV');
			if(sub.length==0)return;
			
			if(sub[0].className!='correctAnswer'){
				return;
			}	
			
		}	
		
		quizIsFinished();	
	}
	

	
	function resetPositions()
	{
		if(dragDropTimer>=10)return;
		
		for(var no=0;no<destinationObjArray.length;no++){
			if(destinationObjArray[no]['obj']){
				destinationObjArray[no]['left'] = getLeftPos(destinationObjArray[no]['obj'])
				destinationObjArray[no]['top'] = getTopPos(destinationObjArray[no]['obj'])	
			}		
			
		}
		sourceObjectArray['left'] = getLeftPos(answerDiv);
		sourceObjectArray['top'] = getTopPos(answerDiv);		
	}
	
	
	function initDragDropScript()
	{
		dragContentDiv = document.getElementById('dragContent');
		
		answerDiv = document.getElementById('answerDiv');
		answerDiv.onselectstart = cancelEvent;
		var divs = answerDiv.getElementsByTagName('DIV');
		var answers = new Array();
		
		for(var no=0;no<divs.length;no++){
			if(divs[no].className=='dragDropSmallBox'){
				divs[no].onmousedown = initDragDrop;
				answers[answers.length] = divs[no];
				arrayOfAnswers[arrayOfAnswers.length] = divs[no];
			}
			
		}	
		
		if(shuffleAnswers){
			for(var no=0;no<(answers.length*10);no++){
				var randomIndex = Math.floor(Math.random() * answers.length);
				answerDiv.appendChild(answers[randomIndex]);
			}		
		}
		
		sourceObjectArray['obj'] = answerDiv;
		sourceObjectArray['left'] = getLeftPos(answerDiv);
		sourceObjectArray['top'] = getTopPos(answerDiv);
		sourceObjectArray['width'] = answerDiv.offsetWidth;
		sourceObjectArray['height'] = answerDiv.offsetHeight;
		
		
		questionDiv = document.getElementById('questionDiv');
		
		questionDiv.onselectstart = cancelEvent;
		var divs = questionDiv.getElementsByTagName('DIV');
		
		var questions = new Array();
		var questionsOpenBoxes = new Array();
		

		for(var no=0;no<divs.length;no++){
			if(divs[no].className=='destinationBox'){
				var index = destinationObjArray.length;
				destinationObjArray[index] = new Array();
				destinationObjArray[index]['obj'] = divs[no];
				destinationObjArray[index]['left'] = getLeftPos(divs[no])
				destinationObjArray[index]['top'] = getTopPos(divs[no])
				destinationObjArray[index]['width'] = divs[no].offsetWidth;
				destinationObjArray[index]['height'] = divs[no].offsetHeight;
				questionsOpenBoxes[questionsOpenBoxes.length] = divs[no];
				arrayOfEmptyBoxes[arrayOfEmptyBoxes.length] = divs[no];
			}
			if(divs[no].className=='dragDropSmallBox'){
				questions[questions.length] = divs[no];
			}
				
		}
		
		if(shuffleQuestions){
			for(var no=0;no<(questions.length*10);no++){
				var randomIndex = Math.floor(Math.random() * questions.length);

				questionDiv.appendChild(questions[randomIndex]);			
				questionDiv.appendChild(questionsOpenBoxes[randomIndex]);		
				
				destinationObjArray[destinationObjArray.length] = destinationObjArray[randomIndex];
				destinationObjArray.splice(randomIndex,1);	
				
				questionsOpenBoxes[questionsOpenBoxes.length] = questionsOpenBoxes[randomIndex];
				questionsOpenBoxes.splice(randomIndex,1);
				questions[questions.length] = questions[randomIndex];
				questions.splice(randomIndex,1);	
				
				
			}		
		}
		
		questionDiv.style.visibility = 'visible';
		answerDiv.style.visibility = 'visible';
		
		document.documentElement.onmouseup = dragDropEnd;	
		document.documentElement.onmousemove = dragDropMove;	
		setTimeout('resetPositions()',150);
		window.onresize = resetPositions;
	}

	/* Reset the form */
	function dragDropResetForm()
	{
		for(var no=0;no<arrayOfAnswers.length;no++){
			arrayOfAnswers[no].className='dragDropSmallBox'
			answerDiv.appendChild(arrayOfAnswers[no]);			
		}	
	}
	
	window.onload = initDragDropScript;
	
	</script>



在网页<body>区添加以下代码

<div id="dragScriptContainer">
	<div id="questionDiv">
		<div class="dragDropSmallBox" id="q1">5x1</div>
		<div class="destinationBox"></div>
		<div class="dragDropSmallBox" id="q2">5x2</div>
		<div class="destinationBox"></div>
		<div class="dragDropSmallBox" id="q3">5x3</div>
		<div class="destinationBox"></div>
		<div class="dragDropSmallBox" id="q4">5x4</div>
		<div class="destinationBox"></div>
		<div class="dragDropSmallBox" id="q5">5x5</div>
		<div class="destinationBox"></div>
		<div class="dragDropSmallBox" id="q6">5x6</div>
		<div class="destinationBox"></div>
		<div class="dragDropSmallBox" id="q7">5x7</div>
		<div class="destinationBox"></div>
		<div class="dragDropSmallBox" id="q8">5x8</div>
		<div class="destinationBox"></div>
		<div class="dragDropSmallBox" id="q9">5x9</div>
		<div class="destinationBox"></div>
		<div class="dragDropSmallBox" id="q10">5x10</div>
		<div class="destinationBox"></div>
	
	</div>
	
	
	<div id="answerDiv">
		<div class="dragDropSmallBox" id="a1">5</div>
		<div class="dragDropSmallBox" id="a2">10</div>
		<div class="dragDropSmallBox" id="a3">15</div>
		<div class="dragDropSmallBox" id="a4">20</div>
		<div class="dragDropSmallBox" id="a5">25</div>
		<div class="dragDropSmallBox" id="a6">30</div>
		<div class="dragDropSmallBox" id="a7">35</div>
		<div class="dragDropSmallBox" id="a8">40</div>
		<div class="dragDropSmallBox" id="a9">45</div>
		<div class="dragDropSmallBox" id="a10">50</div>	
	</div>
</div>
<div id="dragContent"></div>
<input type="button" onclick="dragDropResetForm();return false" value="Reset">



程序配置说明
设置HTML代码
通过下面的HTML代码创建问题和答案的box

<div id="dragScriptContainer">
  <div id="questionDiv">
    <div class="dragDropSmallBox" id="q1">5x1</div>
    <div class="destinationBox"></div>
    <div class="dragDropSmallBox" id="q2">5x2</div>
    <div class="destinationBox"></div>
    <div class="dragDropSmallBox" id="q3">5x3</div>
    <div class="destinationBox"></div>
  </div>
  <div id="answerDiv">
    <div class="dragDropSmallBox" id="a1">5</div>
    <div class="dragDropSmallBox" id="a2">10</div>
    <div class="dragDropSmallBox" id="a3">15</div>

  </div>
</div> 



<div id="questionDiv">是问题的根DIV
每一个问题有两个DIV标签
    <div class="dragDropSmallBox" id="q1">5x1</div>
    <div class="destinationBox"></div>


第一个div表示问题,第二个div是一个空box,用来放置拖放过来的答案,要添加多个问题,需要成对添加这两个div

<div id="answerDiv"> 是答案的根Div,每一个答案有一个div,如<div class="dragDropSmallBox" id="a1">5</div>

注意:答案Div的ID必须和问题的Div进行对应,比如问题DIV的ID是q1,答案div的ID就应是a1,就是后面的数字必须一样

外观
要修改外观,请参照CSS里的注释进行修改

Javascript 变量
有几天JavaScript可以修改

var shuffleQuestions = true; /* 是否随机排列问题 */
var shuffleAnswers = true; /* 是否随机排列答案 */
var lockedAfterDrag = false;

Javascript函数
本程序有一个空函数

function quizIsFinished()
{
  
} 

这个函数是当所有答案都摆放正确了以后出发,你可以在函数内输入一些功能代码,如:
alert('Finished! you did it'); 


另外需要在网页头部添加以下代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 

下载"JavaScript拖拽效果 - 实现问答比赛游戏的脚本"

  • 本地下载
  • 本地下载2

相关资源