我正在创建一个数学游戏。游戏将允许儿童拖动减法符号" - "在两个数字中的任何一个上都会出现答案(例如9-4 = 5)。
以下代码在Chrome和Safari上运行正常,但拖放功能在Firefox上无效。
有人可以建议如何优化下面的代码并使拖动功能在Firefox上运行吗?
此处示例:http://jsfiddle.net/W572n/
先谢谢
(function(){
//declare my variables
var numLow = 1, //lowest number
numHigh = 20,//highest number
$firstNum = $('#firstNum'),
$secondNum = $('#secondNum'),
adjustedHigh, numRand1, numRand2 = null;
//Callback function when cancelling the event
function cancel(e){
if(e.preventDefault){
e.preventDefault();
}
return false;
}
//generates random numbers and stores random numbers in data-price attribute
//and displays random numbers in divs #firstNum and #secondNum
adjustedHigh = (parseFloat(numHigh) - parseFloat(numLow)) + 1;
numRand1 = Math.floor(Math.random()*adjustedHigh) + parseFloat(numLow);
numRand2 = Math.floor(Math.random()*adjustedHigh) + parseFloat(numLow);
$firstNum.attr('data-price', numRand1);
$firstNum.text( $("#firstNum").attr("data-price"));
$secondNum.attr('data-price', numRand2);
$secondNum.text( $("#secondNum").attr("data-price"));
//Get the .drop zones
var drop = $('.drop');
var draggedItem = null;
//Add the Event Listener to draggable item "+" symbol
$('.draggable-item').on('dragstart', function(e){
draggedItem = jQuery(this);
}, false);
drop.on('dragover', cancel);
drop.on('dragenter', cancel);
drop.on('drop', function(e){
//Prevent default browser behaviour
e.preventDefault();
//Do the math
var sum = parseInt($("#firstNum").attr("data-price")) + parseInt($("#secondNum").attr("data-price"));
//add result
$('span').html(sum);
/*alert(sum);*/
return false;
});
})();