javascript-jsPlumb使用单击而不是拖动来连接元素
作者:互联网
我正在尝试使用jsPlumb将问题与测验中的答案联系起来.我的大部分工作期望我希望能够单击一个问题,然后单击一个答案,而不是从一个端点拖动到另一个端点.这是因为在触摸设备上拖动很繁琐.这可能吗?
Here is my jsbin with the dragging working
这是我正在使用的jQuery.
$(document).ready(function () {
var targetOption = {
anchor: "LeftMiddle",
isSource: false,
isTarget: true,
reattach: true,
endpoint: "Rectangle",
connector: "Straight",
connectorStyle: { strokeStyle: "#ccc", lineWidth: 5 },
paintStyle: { width: 20, height: 20, fillStyle: "#ccc" },
setDragAllowedWhenFull: true
}
var sourceOption = {
tolerance: "touch",
anchor: "RightMiddle",
maxConnections: 1,
isSource: true,
isTarget: false,
reattach: true,
endpoint: "Rectangle",
connector: "Straight",
connectorStyle: { strokeStyle: "#ccc", lineWidth: 5 },
paintStyle: { width: 20, height: 20, fillStyle: "#ccc" },
setDragAllowedWhenFull: true
}
jsPlumb.importDefaults({
ConnectionsDetachable: true,
ReattachConnections: true
});
jsPlumb.addEndpoint('match1', sourceOption);
jsPlumb.addEndpoint('match2', sourceOption);
jsPlumb.addEndpoint('match3', sourceOption);
jsPlumb.addEndpoint('match4', sourceOption);
jsPlumb.addEndpoint('answer1', targetOption);
jsPlumb.addEndpoint('answer2', targetOption);
jsPlumb.addEndpoint('answer3', targetOption);
jsPlumb.addEndpoint('answer4', targetOption);
jsPlumb.draggable('match1');
jsPlumb.draggable('answer1');
});
解决方法:
我认为,如果您不需要可拖动的东西,那么就不应该使用它,而应使用常规的click = connect方法.
这是一个例子:
基本上我做了什么:
//current question clicked on
var questionSelected = null;
var questionEndpoint = null;
//remember the question you clicked on
$("ul.linematchitem > li").click( function () {
//remove endpoint if there is one
if( questionSelected !== null )
{
jsPlumb.removeAllEndpoints(questionSelected);
}
//add new endpoint
questionSelected = $(this)[0];
questionEndpoint = jsPlumb.addEndpoint(questionSelected, sourceOption);
});
//now click on an answer to link it with previously selected question
$("ul.linematchplace > li").click( function () {
//we must have previously selected question
//for this to work
if( questionSelected !== null )
{
//create endpoint
var answer = jsPlumb.addEndpoint($(this)[0], targetOption);
//link it
jsPlumb.connect({ source: questionEndpoint, target: answer });
//cleanup
questionSelected = null;
questionEndpoint = null;
}
});
当您单击问题列表元素时-它添加端点,当您单击答案元素时-它也添加端点并将其与先前选择的问题连接.
我相信这是您想要做的?
附言附带说明一下,为了使用户更加直观,请首先使问题的端点可见,以便用户确定要执行的操作-单击.选择问题后,可用的回答端点会弹出提示,提示用户应单击下一步.
标签:jsplumb,javascript,jquery 来源: https://codeday.me/bug/20191031/1973362.html