1
我想創建一個特定圖像可以拖動的網頁。使用javascript和css拖動
所以這是我的代碼。
<! doctype html>
<html>
<head>
<title>
</title>
<script src="13.8.js"></script>
</head>
<body>
<img src="file:\\C:\Users\X555\Pictures\poster\yellowflowers.png" id="image" alt="white lion">
<script>
var mustDrag;
var image
function set() {
image = document.getElementById("image");
image.addEventListener("mousedown", function() { mustDrag = true }, false);
window.addEventListener("mouseup", function() { mustDrag = false; }, false);
window.addEventListener("mousemove", drag, false);
}
function drag(e) {
if (mustDrag) {
e.target.style.position = 'absolute';
e.target.style.left = Number(e.x);
e.target.style.top = Number(e.y);
}
}
window.addEventListener("load", set, false);
</script>
</body>
</html>
但它不能正常工作。有時它拖動白色它不應該。並且通常將圖像放在窗口的左下方,而不是。我根本沒有意識到這個代碼有什麼問題?
我真的很感激Shrinath,我從很長的時間尋找這種類型的代碼,很多感謝它。 –
歡迎@om ..快樂編碼! – Shrinath
感謝Shrinath,但還有兩個問題。 1-我不知道jQuery,所以我不知道offsetX是什麼。 2 - 那麼我的主要問題是「我的代碼出了什麼問題」。對我來說,每件事情都是正確的,但並非如此。代碼的錯誤在哪裏? –