javascript - 如何在javascript中的图像上添加可拖动的文本

标签 javascript html

您好,我想创建一个非常简单的模因创建工具。这是图像和文本的代码。我在图像上拖动文字。你能帮忙吗?

<html>
<body>
<div id="draggable-element">Drag me!</div>
<style>
body {padding:10px}

#draggable-element {
  width:100px;
  height:10px;
  background-color:#fff;
  color:black;
  padding:10px 12px;
  cursor:move;
  position:relative; /* important (all position that's not `static`) */
}
</style>
<Script>
var selected = null, // Object of the element to be moved
    x_pos = 0, y_pos = 0, // Stores x & y coordinates of the mouse pointer
    x_elem = 0, y_elem = 0; // Stores top, left values (edge) of the element

// Will be called when user starts dragging an element
function _drag_init(elem) {
    // Store the object of the element which needs to be moved
    selected = elem;
    x_elem = x_pos - selected.offsetLeft;
    y_elem = y_pos - selected.offsetTop;
}

// Will be called when user dragging an element
function _move_elem(e) {
    x_pos = document.all ? window.event.clientX : e.pageX;
    y_pos = document.all ? window.event.clientY : e.pageY;
    if (selected !== null) {
        selected.style.left = (x_pos - x_elem) + 'px';
        selected.style.top = (y_pos - y_elem) + 'px';
    }
}

// Destroy the object when we are done
function _destroy() {
    selected = null;
}

// Bind the functions...
document.getElementById('draggable-element').onmousedown = function () {
    _drag_init(this);
    return false;
};

document.onmousemove = _move_elem;
document.onmouseup = _destroy;
</script>
</body>
</html>

这是拖动文本的代码。但我需要将其拖到图像上。如何以简单的方式做到这一点。

最佳答案

只需使用 position: absolute; 而不是 relative。

var selected = null, // Object of the element to be moved
  x_pos = 0,
  y_pos = 0, // Stores x & y coordinates of the mouse pointer
  x_elem = 0,
  y_elem = 0; // Stores top, left values (edge) of the element

// Will be called when user starts dragging an element
function _drag_init(elem) {
  // Store the object of the element which needs to be moved
  selected = elem;
  x_elem = x_pos - selected.offsetLeft;
  y_elem = y_pos - selected.offsetTop;
}

// Will be called when user dragging an element
function _move_elem(e) {
  x_pos = document.all ? window.event.clientX : e.pageX;
  y_pos = document.all ? window.event.clientY : e.pageY;
  if (selected !== null) {
    selected.style.left = (x_pos - x_elem) + 'px';
    selected.style.top = (y_pos - y_elem) + 'px';
  }
}

// Destroy the object when we are done
function _destroy() {
  selected = null;
}

// Bind the functions...
document.getElementById('draggable-element').onmousedown = function() {
  _drag_init(this);
  return false;
};

document.onmousemove = _move_elem;
document.onmouseup = _destroy;
body {
  padding: 10px
}

#draggable-element {
  width: 100px;
  height: 10px;
  background-color: #fff;
  color: black;
  padding: 10px 12px;
  cursor: move;
  position: absolute;
  /* important (all position that's not `static`) */
}
<div id="draggable-element">Drag me!</div>
<img src="https://www.google.co.in/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"/>

关于javascript - 如何在javascript中的图像上添加可拖动的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34562948/

相关文章:

javascript - 如何使用 ajax 发布 json 对象?

javascript - 如何在 BrowserWindow (Electron) 中打开 Markdown 文件?

javascript - 从嵌套数组中删除对象

php - 如何使 MailHandler.php 在 Wordpress 中工作?

javascript - JavaScript 函数有冲突吗?

html - 图片上方的文字

javascript - 如何实现像本例中那样的完全可用的图像滚动效果?

javascript - 如何使游戏音量上升

javascript - 翻转停止在 IE8 中工作的链接(WP 子主题)

html - 嵌套 div 居中的 Bootstrap 问题