如何在JavaScript中的图像上添加可拖动文本 [英] How to add draggable text on image in javascript

查看:45
本文介绍了如何在JavaScript中的图像上添加可拖动文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个非常简单的模因创建器工具.这是图像和文本的代码.我在图片上拖了文字.你能帮忙吗?

Hi I would like to create one very simple meme creator tool. Here is the code to Image and Text. I have drag text on image. could you help ?

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

这是拖动文本的代码.但是我需要将其拖到图像上.如何轻松地做到这一点.

This is code for drag text. But I need to drag this on image. How to do this in easy way.

推荐答案

只需使用 position:absolute; 而不是相对的.

Just use position: absolute; instead of 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中的图像上添加可拖动文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆