Mootools - 使用 .implement 将变量添加到所有可拖动和可放置的元素

发布于 2024-11-19 02:42:37 字数 1808 浏览 2 评论 0原文


我使用 mootools Drag.move 类制作拖放系统,但我需要所有可拖动和可放置的元素具有一些额外的变量,并且可能添加一两个函数。我研究了如何使用 .implement 来做到这一点,但我不知道如何将其融入到我的代码中:

window.addEvent('domready', function(){

 $$('#draggables DIV').makeDraggable({

droppables: $$('#droppables DIV'),

onEnter: function(draggable, droppable){
    droppable.setStyle('background', 'orange');
    droppable.setStyle('opacity', '0.4');
    snap_left = droppable.getStyle('left');
    snap_top = droppable.getStyle('top');   
    document.getElementById("slot").innerHTML=droppable.id;
},

onLeave: function(draggable, droppable){
    droppable.setStyle('background', null);
},

onDrop: function(draggable, droppable){

    if (droppable){
        droppable.setStyle('background', '');
        draggable.setStyle('left', snap_left );
        draggable.setStyle('top', snap_top );                   
    } else {
        draggable.setStyle('left', snap_left );
        draggable.setStyle('top', snap_top );
    }
}

 });

});

我想要的是否可以使用 .implement?
我可以将这些东西添加到所有可拖动和可放置的元素中吗?
提前!

-泰国蝎子

编辑:
我尝试直接向 mootools 库中的主类添加选项,并尝试从 onEnter 事件访问它们,如下所示:

    onEnter: function(draggable, droppable){

if (droppable.occupied){ //here is where im tryin to acces it, the default option is set to occupied: true droppable.setStyle('background', 'red'); droppable.setStyle('opacity', '0.4'); document.getElementById("slot").innerHTML=droppable.id; } else { droppable.setStyle('background', 'orange'); droppable.setStyle('opacity', '0.4'); snap_left = droppable.getStyle('left'); snap_top = droppable.getStyle('top'); document.getElementById("slot").innerHTML=droppable.id; } },

但没有任何工作。

Im making a drag and drop system with mootools drag.move class, but i need all the draggable and droppable elements to have some extra variables and maybe add in a function or two. Ive researched on how to do this using .implement but Ive got no idea how to fit that into my code:

window.addEvent('domready', function(){

 $('#draggables DIV').makeDraggable({

droppables: $('#droppables DIV'),

onEnter: function(draggable, droppable){
    droppable.setStyle('background', 'orange');
    droppable.setStyle('opacity', '0.4');
    snap_left = droppable.getStyle('left');
    snap_top = droppable.getStyle('top');   
    document.getElementById("slot").innerHTML=droppable.id;
},

onLeave: function(draggable, droppable){
    droppable.setStyle('background', null);
},

onDrop: function(draggable, droppable){

    if (droppable){
        droppable.setStyle('background', '');
        draggable.setStyle('left', snap_left );
        draggable.setStyle('top', snap_top );                   
    } else {
        draggable.setStyle('left', snap_left );
        draggable.setStyle('top', snap_top );
    }
}

 });

});

Is what I want posible using .implement?

Can I add these things to all draggable and droppable elements?

ty in advance!

-Thaiscorpion

edit:
Ive tried adding options directly to the main class in the mootools library and tried accesing them from the onEnter event like this:

    onEnter: function(draggable, droppable){

if (droppable.occupied){ //here is where im tryin to acces it, the default option is set to occupied: true droppable.setStyle('background', 'red'); droppable.setStyle('opacity', '0.4'); document.getElementById("slot").innerHTML=droppable.id; } else { droppable.setStyle('background', 'orange'); droppable.setStyle('opacity', '0.4'); snap_left = droppable.getStyle('left'); snap_top = droppable.getStyle('top'); document.getElementById("slot").innerHTML=droppable.id; } },

but not getting anything to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昵称有卵用 2024-11-26 02:42:37

您可以使用元素存储。

draggable.store("occupied", true);

....

if (draggable.retrieve("occupied") === true) {

}

.... 函数或任何内容都可以按元素存储

element.store("somekey", function() {
    element.toggleClass("foo");
});

element.retrieve("somekey").call(element);

,等等。

使用 Implement:

Element.implement({
    dragfoo: function() {
        this.set("drag", { });
        return this;
    }
});

// allows you:

$("someid").dragfoo();

不过,如果您需要存储,请使用存储并且不要在实际元素上存储属性。 mootools 存储实际上使用闭包后面的对象哈希表。在 IE 中拥有专有元素属性可能会大大减慢元素访问速度。

you can use element storage.

draggable.store("occupied", true);

....

if (draggable.retrieve("occupied") === true) {

}

.... functions or anything can be stored per element

element.store("somekey", function() {
    element.toggleClass("foo");
});

element.retrieve("somekey").call(element);

and so forth.

to use Implement:

Element.implement({
    dragfoo: function() {
        this.set("drag", { });
        return this;
    }
});

// allows you:

$("someid").dragfoo();

though if you need storage, use storage and dont store properties on the actual element. mootools storage really uses an object hash table that's behind a closure. having proprietary element attributes/properties in IE can slow things down considerably in element access.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文