发布于 2016-12-04 12:17:18 | 154 次阅读 | 评论: 0 | 来源: 网友投递
YUI JavaScript框架
Yahoo! UI Library (YUI) 是一个开放原始码的 JavaScript函数库,为了能建立一个高互动的网页,它采用了AJAX,DHTML 和 DOM 等程式码技术。它也包含了许多 CSS 资源。使用授权为 BSD许可证。
YAHOO.example.DDApp = function() {
var dd;
return {
init2: function() {
// var dropzone =["dz"];
// for(i in dropzone){
// new YAHOO.util.DDTarget(dropzone[i]);
// };
var draggable =["dd_1","dd_2","dd_3"]; //数组存放DargDrop的ID
Draggable = function(id, sGroup) {
//建立DragDrop对象。这个必须由YAHOO.util.DragDrop的子类来调用
//Sets up the DragDrop object. Must be called in the constructor of any YAHOO.util.DragDrop subclass
this.init(id, sGroup);
}
Draggable.prototype = new YAHOO.util.DD(); //继承父类
Draggable.prototype.startDrag = function(x, y) {
YAHOO.util.Dom.setStyle(this.getEl(), "opacity", 0.5);
}
Draggable.prototype.endDrag = function(e) { //拖放后返回原点
var draggable = this.getEl();
YAHOO.util.Dom.setStyle(draggable, "opacity", 1.0);
draggable.style.left = "0px";
draggable.style.top = "0px";
}
for(i in draggable){
new Draggable(draggable[i]);
}
}
}
} ();
注意的地方:
var correct = { opt0:"ans1", opt1:"ans2", opt2:"ans0" } // 正确答案
var answer = { opt0:"tmp0", opt1:"tmp1", opt2:"tmp2" } // 解答
// 採点
function mark(event)
{
var points = 0; //
var max = 3; //
for (key in correct) {
points += correct[key] == answer[key] ? 1: 0;
}
var score = Math.floor(points / max * 100);
var result = document.getElementById("result");
result.innerHTML = (score > 70 ? "合格": "不合格") + ":" + score + "%";
}
// 初始化
function init(event)
{
var dropzone = [ "ans0", "ans1", "ans2", // 答案栏
"tmp0", "tmp1", "tmp2" ]; // 临时地方(开始放国旗的地方)
for (id in dropzone) {
new YAHOO.util.DDTarget(dropzone[id]);
}
var draggable = [ "opt0", "opt1", "opt2" ]; // 可选项(国旗)
Draggable = function(id, sGroup) {
this.init(id, sGroup);
}
Draggable.prototype = new YAHOO.util.DD();
Draggable.prototype.canAccept = function(draggable, dropzone) {
if (dropzone.id.match(/^opt[012]$/)) {
return false;
}
for (key in answer) {
if (answer[key] == dropzone.id) {
return false;
}
}
return true;
}
Draggable.prototype.startDrag = function(x, y) {
YAHOO.util.Dom.setStyle(this.getEl(), "opacity", 0.5);
}
Draggable.prototype.onDragEnter = function(e, id) {
var dropzone = YAHOO.util.DDM.getElement(id);
var draggable = this.getEl();
if (this.canAccept(draggable, dropzone)) {
dropzone.style.backgroundColor = "orange";
}
}
Draggable.prototype.onDragOut = function(e, id) {
var dropzone = YAHOO.util.DDM.getElement(id);
dropzone.style.backgroundColor = "white";
}
Draggable.prototype.onDragDrop = function(e, id) {
var dropzone = YAHOO.util.DDM.getElement(id);
var draggable = this.getEl();
if (this.canAccept(draggable, dropzone)) {
dropzone.style.backgroundColor = "white";
dropzone.appendChild(draggable);
answer[draggable.id] = dropzone.id; // 解答更新
}
}
Draggable.prototype.endDrag = function(e) {
var draggable = this.getEl();
YAHOO.util.Dom.setStyle(draggable, "opacity", 1.0);
draggable.style.left = "0px";
draggable.style.top = "0px";
}
for (id in draggable) {
new Draggable(draggable[id]);
}
// 绑定按钮触发事件,计算成绩
YAHOO.util.Event.addListener("submit", "click", mark);
}
YAHOO.util.Event.addListener(window, "load", init);