Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
87 views
in Technique[技术] by (71.8m points)

javascript - Drag and Drop HTML5

Folks, how can i update the styles of dragged element

  1. Update the element which is under drag
  2. Update the position/space from where the element was dragged

it seems that using

event.target.addClass or event.target.styles.  etc

updates the element which is under drag as well as position from where it was taken

please take a look at this screenshot link, this is what i want to achieve when dragging the element, basically onDragStart

thank you

question from:https://stackoverflow.com/questions/65925912/drag-and-drop-html5

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With this you can change style of element being dragged and keep the original style (or change it the way you like):

function handleDragStart(e) {
  this.style.color = 'red';  
}

function handleOnDrag(e) {
  this.style.color = 'black';
}

let items = document.querySelectorAll('ul>li');
items.forEach(function(item) {
  item.addEventListener('dragstart', handleDragStart, false);
  item.addEventListener('drag', handleOnDrag, false);
});
<ul>
  <li draggable="true">lorem ipsum</li>
  <li draggable="true">lorem ipsum</li>
  <li draggable="true">lorem ipsum</li>
</ul>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...