# Eliminate "Ghosting" when Dragging an Image in the Browser

**URL:** https://forum.kirupa.com/t/eliminate-ghosting-when-dragging-an-image-in-the-browser/636353
**Category:** web dev
**Created:** [April 2, 2017, 6:50am UTC](https://forum.kirupa.com/t/eliminate-ghosting-when-dragging-an-image-in-the-browser/636353 "2017-04-02T06:50:44Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![luis\_vera](https://avatars.discourse-cdn.com/v4/letter/l/9fc29f/32.png) [@luis\_vera](https://forum.kirupa.com/u/luis_vera)
#### Post date: [April 2, 2017, 6:50am UTC](https://forum.kirupa.com/t/eliminate-ghosting-when-dragging-an-image-in-the-browser/636353/1 "2017-04-02T06:50:44Z")

</div>

Hi, i’m working in a game, now I have the following problem I have to drag an image, but the navigator create a ghost image, that’s the problem, I have to eleminate it

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [April 2, 2017, 4:12pm UTC](https://forum.kirupa.com/t/eliminate-ghosting-when-dragging-an-image-in-the-browser/636353/2 "2017-04-02T16:12:29Z")

</div>

There are several ways of fixing that. I think the easiest is to set the `user-drag` CSS property to **none** on the image elements that will be dragged. Let me know if you tried that and it works 🙂

---

<div class="post-metadata">

### Author: ![luis\_vera](https://avatars.discourse-cdn.com/v4/letter/l/9fc29f/32.png) [@luis\_vera](https://forum.kirupa.com/u/luis_vera)
#### Post date: [April 3, 2017, 5:30am UTC](https://forum.kirupa.com/t/eliminate-ghosting-when-dragging-an-image-in-the-browser/636353/3 "2017-04-03T05:30:38Z")

</div>

Thank you very much for your answer, but that does not eliminate the ghost image that is produced, it only leaves it fixed, but the same ghost image is created

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [April 3, 2017, 5:37am UTC](https://forum.kirupa.com/t/eliminate-ghosting-when-dragging-an-image-in-the-browser/636353/4 "2017-04-03T05:37:55Z")

</div>

When I tried setting the `user-drag` (and as I also found out `-webkit-user-drag`,`-moz-user-drag`) to **none** , I didn’t see the ghosted image. Another solution is to set the _ **draggable** _ attribute on the image to **false** :

```
<!DOCTYPE html>
<html>

<head>
    <title>Drag Image Without Browser Ghosting</title>
    <style>
        #contentContainer {
            width: 550px;
            height: 350px;
            border: 5px black solid;
            overflow: hidden;
            background-color: #FFEE33;
        }
        #contentContainer img {
          position: absolute;
        }
    </style>
</head>

<body>
    <div id="contentContainer">
      <img draggable="false" id="dragImage" src="https://www.kirupa.com/images/blueSquare.png"/>
    </div>
</body>

</html>

```

Does this solution work better? 🙂

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [April 3, 2017, 6:47am UTC](https://forum.kirupa.com/t/eliminate-ghosting-when-dragging-an-image-in-the-browser/636353/5 "2017-04-03T06:47:50Z")

</div>

Ok - after fiddling with this some more, one sure-fire solution is to wrap the image inside a div element and drag the `div` element around. Check out this:

```
<!DOCTYPE html>
<html>

<head>
    <title>Drag Image Without Browser Ghosting</title>
    <style>
        #contentContainer {
          padding: 40;
        }
        #dragImage {
          width: 300px;
          height: 200px;
          background-color: blue;
          cursor: move;
          position: absolute;
        }
        #dragImage img {
          pointer-events: none;
          width: 300px;
          height: 200px;
        }
    </style>
</head>

<body>
    <div id="contentContainer">
      <div id="dragImage">
        <img src="https://www.kirupa.com/images/blueSquare.png"/>
      </div>
    </div>
    <script>
      var startDragging = false;

      var dragImage = document.querySelector("#dragImage");

      var elementX = 0;
      var elementY = 0;

      dragImage.addEventListener("mousedown", startDrag, false);
      dragImage.addEventListener("mouseup", stopDrag, false);
      dragImage.addEventListener("mousemove", drag, false);

      function startDrag(e) {
        startDragging = true;

        elementX = e.clientX - dragImage.offsetLeft;
        elementY = e.clientY - dragImage.offsetTop;

      }
      function stopDrag(e) {
        startDragging = false;
      }
      function drag(e) {
        if (startDragging) {
          var xPos = e.clientX - elementX;
          var yPos = e.clientY - elementY;
          dragImage.style.left = xPos + "px";
          dragImage.style.top = yPos + "px";
        }
      }
    </script>
</body>

</html>

```

Note that I set the `pointer-events` CSS property to **none** on our image element to ensure no mouse interactions affect it 🙂

---

<div class="post-metadata">

### Author: ![johniey](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@johniey](https://forum.kirupa.com/u/johniey)
#### Post date: [April 3, 2017, 3:56pm UTC](https://forum.kirupa.com/t/eliminate-ghosting-when-dragging-an-image-in-the-browser/636353/6 "2017-04-03T15:56:22Z")

</div>

I’m running a PC with vista and Ken is right, if you disable the “show content while dragging” option, this won’t be seen. However, I’m assuming 99% of windows users havn’t done this in which case they can see the ghosting. It’s really not a big deal, and I may have to come to terms with it, but it just seems unprofessional and amatur. This can be seen in Explorer, Firefox, and Google Chrome to the extent of my knowlege.

This is the site I built for my friend, [http://www.creaturerevenge.com/](http://www.creaturerevenge.com/) It being the first css site I’ve ever built, its bound to generate criticism, but if you click/hold on ANY image and drag you will see the effect I am talking about. If you do the same for this site, you will just get a “cancel” icon. I’m just trying to figure out what is allowing for this difference.

---

<div class="post-metadata">

### Author: ![luis\_vera](https://avatars.discourse-cdn.com/v4/letter/l/9fc29f/32.png) [@luis\_vera](https://forum.kirupa.com/u/luis_vera)
#### Post date: [April 21, 2017, 2:28am UTC](https://forum.kirupa.com/t/eliminate-ghosting-when-dragging-an-image-in-the-browser/636353/7 "2017-04-21T02:28:17Z")

</div>

Thank you very much, just giving the “draggable” property in false, solved my problem.

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [April 21, 2017, 4:27pm UTC](https://forum.kirupa.com/t/eliminate-ghosting-when-dragging-an-image-in-the-browser/636353/8 "2017-04-21T16:27:15Z")

</div>

Haha! Glad that worked 🙂
