學(xué)習(xí)YUI.Ext第五日--做拖放Darg&Drop
更新時(shí)間:2007年03月10日 00:00:00 作者:
拖放某個(gè)元素Darg&Drop是windows(視窗)問(wèn)世時(shí)的一個(gè)重要特征?,F(xiàn)在我們要在瀏覽器里面實(shí)現(xiàn),怎么做呢?先看看基本例子:
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"]; //數(shù)組存放DargDrop的ID
Draggable = function(id, sGroup) {
//建立DragDrop對(duì)象。這個(gè)必須由YAHOO.util.DragDrop的子類來(lái)調(diào)用
//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) { //拖放后返回原點(diǎn)
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]);
}
}
}
} ();
注意的地方:
1.這里用了一個(gè)數(shù)組先收集好所有要DD(Darg&Drop,下同)的元素,再用for遍歷new new YAHOO.util.DD()對(duì)象,“捆綁”成一類具有相同屬性的對(duì)象:Draggable。
2.遇到一個(gè)無(wú)法入手的問(wèn)題:
用YUI做Dragdrop,如果你的系統(tǒng)開clearType ,移動(dòng)之后字體會(huì)發(fā)毛,估計(jì)ie內(nèi)部render的問(wèn)題 。本來(lái)打算用DDProxy代替,但一用DDProxy就無(wú)法繼承下去。
3.需手工加入xhtml的holder.
ok這個(gè)例子暫告一段落,看看一些好玩的(演示):
var correct = { opt0:"ans1", opt1:"ans2", opt2:"ans0" } // 正確答案
var answer = { opt0:"tmp0", opt1:"tmp1", opt2:"tmp2" } // 解答
// 採(cǎi)點(diǎn)
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" ]; // 臨時(shí)地方(開始放國(guó)旗的地方)
for (id in dropzone) {
new YAHOO.util.DDTarget(dropzone[id]);
}
var draggable = [ "opt0", "opt1", "opt2" ]; // 可選項(xiàng)(國(guó)旗)
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]);
}
// 綁定按鈕觸發(fā)事件,計(jì)算成績(jī)
YAHOO.util.Event.addListener("submit", "click", mark);
}
YAHOO.util.Event.addListener(window, "load", init);
如果再把xhtml貼出來(lái),很長(zhǎng) 很煩 。look look DEMO.
好,今天到這兒,嚴(yán)重ot中。有空再說(shuō)。
復(fù)制代碼 代碼如下:
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"]; //數(shù)組存放DargDrop的ID
Draggable = function(id, sGroup) {
//建立DragDrop對(duì)象。這個(gè)必須由YAHOO.util.DragDrop的子類來(lái)調(diào)用
//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) { //拖放后返回原點(diǎn)
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]);
}
}
}
} ();
1.這里用了一個(gè)數(shù)組先收集好所有要DD(Darg&Drop,下同)的元素,再用for遍歷new new YAHOO.util.DD()對(duì)象,“捆綁”成一類具有相同屬性的對(duì)象:Draggable。
2.遇到一個(gè)無(wú)法入手的問(wèn)題:
用YUI做Dragdrop,如果你的系統(tǒng)開clearType ,移動(dòng)之后字體會(huì)發(fā)毛,估計(jì)ie內(nèi)部render的問(wèn)題 。本來(lái)打算用DDProxy代替,但一用DDProxy就無(wú)法繼承下去。
3.需手工加入xhtml的holder.
ok這個(gè)例子暫告一段落,看看一些好玩的(演示):
復(fù)制代碼 代碼如下:
var correct = { opt0:"ans1", opt1:"ans2", opt2:"ans0" } // 正確答案
var answer = { opt0:"tmp0", opt1:"tmp1", opt2:"tmp2" } // 解答
// 採(cǎi)點(diǎn)
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" ]; // 臨時(shí)地方(開始放國(guó)旗的地方)
for (id in dropzone) {
new YAHOO.util.DDTarget(dropzone[id]);
}
var draggable = [ "opt0", "opt1", "opt2" ]; // 可選項(xiàng)(國(guó)旗)
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]);
}
// 綁定按鈕觸發(fā)事件,計(jì)算成績(jī)
YAHOO.util.Event.addListener("submit", "click", mark);
}
YAHOO.util.Event.addListener(window, "load", init);
如果再把xhtml貼出來(lái),很長(zhǎng) 很煩 。look look DEMO.
好,今天到這兒,嚴(yán)重ot中。有空再說(shuō)。
相關(guān)文章
yui3的AOP(面向切面編程)和OOP(面向?qū)ο缶幊?
這篇文章主要介紹了yui3的AOP(面向切面編程)和OOP(面向?qū)ο缶幊?,需要的朋友可以參考下2015-05-05學(xué)習(xí)YUI.Ext 第七天--關(guān)于View&JSONView
學(xué)習(xí)YUI.Ext 第七天--關(guān)于View&JSONView...2007-03-03學(xué)習(xí)YUI.Ext 第六天--關(guān)于樹TreePanel(Part 2異步獲取節(jié)點(diǎn))
學(xué)習(xí)YUI.Ext 第六天--關(guān)于樹TreePanel(Part 2異步獲取節(jié)點(diǎn))...2007-03-03對(duì)YUI擴(kuò)展的Gird組件 Part-2
對(duì)YUI擴(kuò)展的Gird組件 Part-2...2007-03-03Ext面向?qū)ο箝_發(fā)實(shí)踐(續(xù))
我的上一篇文章《Ext面向?qū)ο箝_發(fā)實(shí)踐》中簡(jiǎn)述了如何編寫一個(gè)面向?qū)ο蟮臄?shù)據(jù)維護(hù)小程序,但這一些都是基于一個(gè)客戶端數(shù)據(jù),即用戶一旦刷新頁(yè)面,所有的操作都將丟失,現(xiàn)在我們就接著上一篇文章來(lái)繼續(xù)講一下如何對(duì)數(shù)據(jù)表進(jìn)行增、刪、改、查操作。2008-11-11使用EXT實(shí)現(xiàn)無(wú)刷新動(dòng)態(tài)調(diào)用股票信息
最近開始對(duì)ExtJS感興趣了,今天正好有空,花了點(diǎn)時(shí)間,寫了個(gè)基于Ext的例子。2008-11-11學(xué)習(xí)YUI.Ext 第六天--關(guān)于樹TreePanel(Part 1)
這篇文章主要介紹了學(xué)習(xí)YUI.Ext 第六天--關(guān)于樹TreePanel(Part 1)2007-03-03