Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a div, containing an img draggable

I'm trying to make a div draggable. The div contains an img and some text that serves as a label for the image. The problem is that when I start the drag by clicking on the img, the img gets dragged, and not the parent div. How do I fix that?

<div className="container-selected" id={id} draggable="true" onDragStart={this.dragStart} onDragEnd={this.drop} onClick={this.click}>
  <img src={imgSrc} />
  <span className="item-id">Some text</span>
</div>

Here's the CSS:

.container-selected {
  padding: 10px;
  position: relative;
  z-index: 0;

  img {
    width: 3em;
    z-index: -1;
  }

  .item-id {
    position: absolute;
    left: 0;
    top:  53px;
    width: 100%;
    text-align: center;
  }
}
like image 352
Dmitry Shvedov Avatar asked Sep 15 '25 04:09

Dmitry Shvedov


1 Answers

This is many years later, but I ran into the same problem, and finally figured it out.

An image is draggable by default, so when you drag an image in a div, the image gets dragged. All you have to do is the make the image non-draggable by:

<img draggable="false" src={status} />

Now when you drag the div, the div and its contained image are dragged together.

like image 117
Roobie Nuby Avatar answered Sep 16 '25 18:09

Roobie Nuby