搜尋
首頁web前端js教程学习YUI.Ext第五日--做拖放Darg&Drop_YUI.Ext相关

拖放某个元素Darg&Drop是windows(视窗)问世时的一个重要特征。现在我们要在浏览器里面实现,怎么做呢?先看看基本例子:

复制代码 代码如下:

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]); 
   } 

        } 
    } 
} (); 
注意的地方:

1.这里用了一个数组先收集好所有要DD(Darg&Drop,下同)的元素,再用for遍历new new YAHOO.util.DD()对象,“捆绑”成一类具有相同属性的对象:Draggable。

2.遇到一个无法入手的问题:
用YUI做Dragdrop,如果你的系统开clearType ,移动之后字体会发毛,估计ie内部render的问题 。本来打算用DDProxy代替,但一用DDProxy就无法继承下去。  

3.需手工加入xhtml的holder.  


ok这个例子暂告一段落,看看一些好玩的(演示): 


复制代码 代码如下:

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); 

       如果再把xhtml贴出来,很长 很烦 。look look DEMO.

好,今天到这儿,严重ot中。有空再说。
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
JavaScript數據類型:瀏覽器和nodejs之間是否有區別?JavaScript數據類型:瀏覽器和nodejs之間是否有區別?May 14, 2025 am 12:15 AM

JavaScript核心數據類型在瀏覽器和Node.js中一致,但處理方式和額外類型有所不同。 1)全局對像在瀏覽器中為window,在Node.js中為global。 2)Node.js獨有Buffer對象,用於處理二進制數據。 3)性能和時間處理在兩者間也有差異,需根據環境調整代碼。

JavaScript評論:使用//和 / * * / * / * /JavaScript評論:使用//和 / * * / * / * /May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript:開發人員的比較分析Python vs. JavaScript:開發人員的比較分析May 09, 2025 am 12:22 AM

Python和JavaScript的主要區別在於類型系統和應用場景。 1.Python使用動態類型,適合科學計算和數據分析。 2.JavaScript採用弱類型,廣泛用於前端和全棧開發。兩者在異步編程和性能優化上各有優勢,選擇時應根據項目需求決定。

Python vs. JavaScript:選擇合適的工具Python vs. JavaScript:選擇合適的工具May 08, 2025 am 12:10 AM

選擇Python還是JavaScript取決於項目類型:1)數據科學和自動化任務選擇Python;2)前端和全棧開發選擇JavaScript。 Python因其在數據處理和自動化方面的強大庫而備受青睞,而JavaScript則因其在網頁交互和全棧開發中的優勢而不可或缺。

Python和JavaScript:了解每個的優勢Python和JavaScript:了解每個的優勢May 06, 2025 am 12:15 AM

Python和JavaScript各有優勢,選擇取決於項目需求和個人偏好。 1.Python易學,語法簡潔,適用於數據科學和後端開發,但執行速度較慢。 2.JavaScript在前端開發中無處不在,異步編程能力強,Node.js使其適用於全棧開發,但語法可能複雜且易出錯。

JavaScript的核心:它是在C還是C上構建的?JavaScript的核心:它是在C還是C上構建的?May 05, 2025 am 12:07 AM

javascriptisnotbuiltoncorc; sanInterpretedlanguagethatrunsonenginesoftenwritteninc.1)JavascriptwasdesignedAsignedAsalightWeight,drackendedlanguageforwebbrowsers.2)Enginesevolvedfromsimpleterterpretpretpretpretpreterterpretpretpretpretpretpretpretpretpretcompilerers,典型地,替代品。

JavaScript應用程序:從前端到後端JavaScript應用程序:從前端到後端May 04, 2025 am 12:12 AM

JavaScript可用於前端和後端開發。前端通過DOM操作增強用戶體驗,後端通過Node.js處理服務器任務。 1.前端示例:改變網頁文本內容。 2.後端示例:創建Node.js服務器。

Python vs. JavaScript:您應該學到哪種語言?Python vs. JavaScript:您應該學到哪種語言?May 03, 2025 am 12:10 AM

選擇Python還是JavaScript應基於職業發展、學習曲線和生態系統:1)職業發展:Python適合數據科學和後端開發,JavaScript適合前端和全棧開發。 2)學習曲線:Python語法簡潔,適合初學者;JavaScript語法靈活。 3)生態系統:Python有豐富的科學計算庫,JavaScript有強大的前端框架。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具