【问题标题】:How to add draggable text on image in javascript如何在javascript中的图像上添加可拖动文本
【发布时间】:2016-01-02 05:31:44
【问题描述】:

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

<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>

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

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    只需使用 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"/>

    【讨论】:

    • 谢谢,如何在css中使透明?
    • 背景是透明的,除非你在css中添加了background-color: #fff;
    猜你喜欢
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 2019-03-03
    • 2012-07-15
    • 2017-06-17
    • 2017-04-28
    • 1970-01-01
    相关资源
    最近更新 更多