Thanks. All I need is to move two images, no other code. And the sample-code was too complex for my limited javascript-skills to extract that functionality alone, and the description on the page you linked to was too general for me to make any sens out of, and did not seem to fit my current code at all.
Anyway, this is my full html/js-code, and if you can spot what is wrong with it that would be great. Otherwise I think I'll leave that functionality for the future - need to study up on my js first, it seems.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script language="javascript" type="text/javascript" src="basic.js"></script>
<style type="text/css">
@import "basic.css";
</style>
<script type="text/javascript">
var currentId;
var objects = {};
var zIndex = 0;
function init() {
document.addEventListener("touchstart", startmove, false);
document.addEventListener("touchmove", move, false);
document.addEventListener("touchend", endmove, false);
// Movable images, named "image1" to "image2" in the html-code
makeObjectMovable(image1);
makeObjectMovable(image2);
}
function startmove(event) {
var touch = event.touches[0];
var id = touch.identifier;
currentId = id;
if (touch.target.className == "movable") {
objects[id] = {
target: touch.target,
beginX: touch.clientX,
beginY: touch.clientY,
pozX: touch.target.pozXinit,
pozY: touch.target.pozYinit
}
touch.target.style.opacity = 0.5;
touch.target.style.zIndex = ++zIndex;
}
}
function endmove(event) {
var touch = event.touches[0];
var id = currentId;
if (objects[id] != null) {
if (objects[id].target.className == "movable") {
objects[id].target.style.opacity = 1;
}
delete objects[id];
}
}
function move(event) {
var touch = event.touches[0];
var id = touch.identifier;
if (objects[id].target.className == "movable") {
objects[id].target.pozXinit = objects[id].pozX + touch.clientX - objects[id].beginX;
objects[id].target.pozYinit = objects[id].pozY + touch.clientY - objects[id].beginY;
objects[id].target.style['-webkit-transform'] = 'translate(' + objects[id].target.pozXinit + 'px,' + objects[id].target.pozYinit + 'px)';
}
event.preventDefault();
}
function makeObjectMovable(obj) {
obj.className = "movable";
obj.pozXinit = 0;
obj.pozYinit = 0;
}
</script>
</head>
<body onload="init();">
<img name="image1" id="image1" src="image1.png" />
<img name="image2" id="image2" src="image2.png" />
</body>
</html>