It seems like there might be some confusion, but I recommend learning from various sources of information to ensure you grasp all aspects of the lesson.
The original example included attributes in the div to enable content to be dragged and dropped onto it. Here is a snippet of the code with explanations for each attribute used.
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)">
The key attributes are:
ondrop="drop(event)
, which triggers an event when the element is dropped
ondragover="allowDrop(event)"
, which allows the drop action to occur
The main takeaway here is that a div's ability to receive a 'drop' operation depends on having the necessary attributes set. If these conditions are not met (i.e.,
ondrop="drop(event)" ondragover="allowDrop(event)"
), the drop action will not be permitted.
I have retained and enhanced this example to provide a more comprehensive understanding since you are already familiar with it. Some points to consider include:
- Notice how red highlighting persists if the draggable item is dropped on an invalid target. This demonstrates the completion of the 'drag' function, while the 'drop' function remains pending. The script modifies CSS properties to achieve these visual effects.
- I have included multiple Codepen examples to showcase using different tabs/windows, contrasting with the W3Schools example to illustrate functionality without 'script' or 'style' tags.
- The script is kept simple, setting CSS properties for each div individually and reusing variables intentionally for experimental purposes only.
- In-code comments are provided to explain complex sections for easier comprehension.
http://codepen.io/anon/pen/WwdgbY
http://codepen.io/anon/pen/zqpJjo
Hopefully, this clarifies things for you!