<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 8 年前。
<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 8 年前。
目前我正在开发 html5 拖放应用程序,但我在 jsfidle 网站上找到了这段代码。它工作正常但是当我在本地系统的 webstroms 编辑器中使用时它不工作。所以请告诉我我能做什么以及如何解决这个问题。
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
<script src="https://code.jquery.com/ui/1.9.2/jquery-ui.js" type="text/javascript"></script>
<script type="text/javascript">
// get reference to the canvas and its context
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.font = "16px helvetica";
// variables
// some text objects defining text on the canvas
var texts = [];
// variables used to get mouse position on the canvas
var $canvas = $("#canvas");
var canvasOffset = $canvas.offset();
var offsetX = canvasOffset.left;
var offsetY = canvasOffset.top;
var scrollX = $canvas.scrollLeft();
var scrollY = $canvas.scrollTop();
// variables to save last mouse position
// used to see how far the user dragged the mouse
// and then move the text by that distance
var startX;
var startY;
// this var will hold the index of the selected text
var selectedText = -1;
// make the <li> draggable
$("ul li").draggable({
helper: 'clone'
});
// drop on canvas
$("#canvas").droppable({
accept: "ul li",
drop: function (event, ui) {
ctx.fillText($(ui.draggable).clone().text(), ui.position.left - event.target.offsetLeft, ui.position.top - event.target.offsetTop);
var text = $(ui.draggable).clone().text();
var x = ui.position.left - event.target.offsetLeft;
var y = ui.position.top - event.target.offsetTop;
var width = ctx.measureText(text).width;
var height = 16;
// save this text info in an object in texts[]
texts.push({
text: text,
x: x,
y: y,
width: width,
height: height
});
// draw all texts to the canvas
draw();
}
});
// clear the canvas draw all texts
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < texts.length; i++) {
var text = texts[i];
ctx.fillText(text.text, text.x, text.y);
}
}
// test if x,y is inside the bounding box of texts[textIndex]
function textHittest(x, y, textIndex) {
var text = texts[textIndex];
return (x >= text.x && x <= text.x + text.width && y >= text.y - text.height && y <= text.y);
}
// handle mousedown events
// iterate through texts[] and see if the user
// mousedown'ed on one of them
// If yes, set the selectedText to the index of that text
function handleMouseDown(e) {
e.preventDefault();
startX = parseInt(e.clientX - offsetX);
startY = parseInt(e.clientY - offsetY);
// Put your mousedown stuff here
for (var i = 0; i < texts.length; i++) {
if (textHittest(startX, startY, i)) {
selectedText = i;
}
}
}
// done dragging
function handleMouseUp(e) {
e.preventDefault();
selectedText = -1;
}
// also done dragging
function handleMouseOut(e) {
e.preventDefault();
selectedText = -1;
}
// handle mousemove events
// calc how far the mouse has been dragged since
// the last mousemove event and move the selected text
// by that distance
function handleMouseMove(e) {
if (selectedText < 0) {
return;
}
e.preventDefault();
mouseX = parseInt(e.clientX - offsetX);
mouseY = parseInt(e.clientY - offsetY);
// Put your mousemove stuff here
var dx = mouseX - startX;
var dy = mouseY - startY;
startX = mouseX;
startY = mouseY;
var text = texts[selectedText];
text.x += dx;
text.y += dy;
draw();
}
// listen for mouse events
$("#canvas").mousedown(function (e) {
handleMouseDown(e);
});
$("#canvas").mousemove(function (e) {
handleMouseMove(e);
});
$("#canvas").mouseup(function (e) {
handleMouseUp(e);
});
$("#canvas").mouseout(function (e) {
handleMouseOut(e);
});
</script>
<style type="text/css">
body {
background-color: ivory;
}
#canvas {
border:1px solid red;
}
</style>
</head>
<body>
<ul id="drag">
<li class="new-item">Drag me down1</li>
<li class="new-item">Drag me down2</li>
<li class="new-item">Drag me down3</li>
</ul>
<canvas id="canvas" width=300 height=300></canvas>
</body>
</html>
最佳答案
您的脚本在页面上的元素创建之前运行 - 您应该在加载时运行它。
你有 jQuery,所以你可以用它来做到这一点。只需更改代码如下:
$(function() {
// the javascript
}
关于javascript - HTML5 拖放不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25763818/
相关文章:
javascript - 为什么要使用 JavaScript 扩展运算符来更新数组中的项目?
javascript - 如何一次更改一个特征(多多边形)的颜色 - 动态样式 google API
javascript - 如何使用 D3 在最后一个栏/项目的右端设置圆圈
javascript - jQuery .text() 方法不显示索引项值
javascript - 如何使用 CreateSVGRect 在 SVG 文档中创建样式矩形?
html - 如何在边缘不透明的情况下使用 css 模糊滤镜?