gpt4 book ai didi

jQuery Draggable(),拖动时执行动态功能

转载 作者:行者123 更新时间:2023-12-01 04:29:52 25 4
gpt4 key购买 nike

我有一个 jQuery Draggable() 函数,并且在拖动时基于 if 执行几个其他函数。像这样的事情:

$("div.element").draggable({
drag: function() {
if (a=1){
function1();
}
if (a=2){
function2();
}
if (a=3){
function3();
}
}
});

涉及很多变量,我希望从性能角度对其进行优化。是否可以使可拖动“知道”在拖动时执行什么功能,而无需每次都进行 if 检查。诸如拖动之类的操作只执行 function2() 和 function3()。

谢谢

最佳答案

您可以使用 jQuery 数据将适当的函数对象与可拖动元素一起存储:

// declare the appropriate function for each element
$("select elements that need function1").data("dragFunction", function() {
// whatever
});
$("select elements that need function2").data("dragFunction", function() {
// whatever
});

// and then... just execute whatever function is stored in "dragFunction"
$("div.element").draggable({
drag: function() { return $(this).data("dragFunction")(); }
});

关于jQuery Draggable(),拖动时执行动态功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3549828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com