In this post, we will see how to implement drag and drop functionality in browsers using the native drag and drop interface in HTML5.
The idea is simple:
- Customize an element to make it draggable
- Allow dropping the draggable element in the droppable area.
Declare two boxes
<div classname="flex gap-8"> <div id="box1" classname="w-[300px] h-[300px] border border-black flex items-center justify-center"></div> <div id="box2" classname="w-[300px] h-[300px] border border-black flex items-center justify-center"></div> </div>
These two boxes will serve as our "drop zones." The user will be able to drag an item from one box and drop it into the other.
Add a div that we want to make as draggable inside "box1"
<div id="box1" classname="w-[300px] h-[300px] border border-black flex items-center justify-center"></div> <div id="draggable1" classname="w-[50px] h-[50px] bg-red-500 cursor-move"></div>
This red square will be the item that can be dragged between the boxes.
Making the Element Draggable
To make an element draggable, we need to add the draggable attribute to it and handle the dragstart event.
The dragstart event will trigger when the user starts dragging the item. Here’s how we can implement it:
const handleOnDragStart = (event) => { event.dataTransfer.setData("text/plain", event.target.id); };
In this function, we use event.dataTransfer.setData() to store the ID of the dragged element. This ID will later help us identify which element was dragged and where it needs to be dropped.
Next, update the draggable1 div to make it draggable and add the onDragStart event handler:
<div id="draggable1" classname="w-[50px] h-[50px] bg-red-500 cursor-move" draggable="true" ondragstart="{handleOnDragStart}"></div>
Dropping the Element
const handleOnDrop = (event) => { event.preventDefault(); const data = event.dataTransfer.getData("text/plain"); event.target.appendChild(document.getElementById(data)); }; const handleDragOver = (event) => { event.preventDefault(); };
- The handleOnDrop function prevents the default behavior (which is important for allowing the drop), retrieves the ID of the dragged element from event.dataTransfer, and appends the dragged element to the drop target.
- The handleDragOver function ensures that the dragged element can be dropped by preventing the default behavior.
Finally, apply these event handlers to the boxes:
<div id="box1" ondrop="{handleOnDrop}" ondragover="{handleDragOver}" classname="w-[300px] h-[300px] border border-black flex items-center justify-center"> <div id="draggable1" classname="w-[50px] h-[50px] bg-red-500 cursor-move" draggable="true" ondragstart="{handleOnDragStart}"></div> </div> <div id="box2" ondrop="{handleOnDrop}" ondragover="{handleDragOver}" classname="w-[300px] h-[300px] border border-black flex items-center justify-center"></div>
You can add visual cues to highlight when the drag operation is in progress. We will reduce the opacity of the component.
This can be done by tracking when the drag operation is being performed in a state variable and changing the opacity.
This is how your react component should look like
<p>import { useState } from "react";</p> <p>const SimpleDragDrop = () => {<br> const [isDragging, setIsDragging] = useState(false);</p> <p>const handleOnDragStart = (event) => {<br> setIsDragging(true);<br> event.dataTransfer.setData("text/plain", event.target.id);<br> };</p> <p>const handleOnDrop = (event) => {<br> event.preventDefault();<br> setIsDragging(false);<br> const data = event.dataTransfer.getData("text/plain");<br> event.target.appendChild(document.getElementById(data));<br> };</p> <p>const handleDragOver = (event) => {<br> event.preventDefault();<br> };</p> <p>return (<br> </p><div classname="flex gap-8"> <br> <div> id="box1"<br> onDrop={handleOnDrop}<br> onDragOver={handleDragOver}<br> className="flex h-[300px] w-[300px] items-center justify-center border border-main"<br> ><br> <div> id="draggable1"<br> className={h-[50px] w-[50px] cursor-move bg-red-500 <span class="p">${</span><span class="nx">isDragging</span> <span class="p">?</span> <span class="dl">"</span><span class="s2">opacity-50</span><span class="dl">"</span> <span class="p">:</span> <span class="dl">"</span><span class="s2">opacity-100</span><span class="dl">"</span><span class="p">}</span><span class="s2">}<br> draggable={true}<br> onDragStart={handleOnDragStart}<br> /><br> </span> </div> <br> <div> id="box2"<br> onDrop={handleOnDrop}<br> onDragOver={handleDragOver}<br> className="flex h-[300px] w-[300px] items-center justify-center border border-main"<br> /><br> </div> <br> );<br> }; <p>export default SimpleDragDrop;</p> <h2> <br> <br> <br> Demo<br> </h2> <p>Live Demo</p> <p>This example demonstrates how easy it is to implement drag-and-drop functionality with just a few lines of code. Feel free to expand on this by adding more drag-and-drop targets or customizing the appearance and behavior of the elements further!</p> <h3> Read more </h3> <ul> <li>HTML Drag and Drop API</li> <li>The HTML5 Drag and Drop API</li> </ul> <p>Original Post</p> </div> </div>
以上がHTML5 でのドラッグ アンド ドロップの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

現実世界におけるJavaScriptのアプリケーションには、サーバー側のプログラミング、モバイルアプリケーション開発、モノのインターネット制御が含まれます。 2。モバイルアプリケーションの開発は、ReactNativeを通じて実行され、クロスプラットフォームの展開をサポートします。 3.ハードウェアの相互作用に適したJohnny-Fiveライブラリを介したIoTデバイス制御に使用されます。

私はあなたの日常的な技術ツールを使用して機能的なマルチテナントSaaSアプリケーション(EDTECHアプリ)を作成しましたが、あなたは同じことをすることができます。 まず、マルチテナントSaaSアプリケーションとは何ですか? マルチテナントSaaSアプリケーションを使用すると、Singの複数の顧客にサービスを提供できます

この記事では、許可によって保護されたバックエンドとのフロントエンド統合を示し、next.jsを使用して機能的なedtech SaaSアプリケーションを構築します。 FrontEndはユーザーのアクセス許可を取得してUIの可視性を制御し、APIリクエストがロールベースに付着することを保証します

JavaScriptは、現代のWeb開発のコア言語であり、その多様性と柔軟性に広く使用されています。 1)フロントエンド開発:DOM操作と最新のフレームワーク(React、Vue.JS、Angularなど)を通じて、動的なWebページとシングルページアプリケーションを構築します。 2)サーバー側の開発:node.jsは、非ブロッキングI/Oモデルを使用して、高い並行性とリアルタイムアプリケーションを処理します。 3)モバイルおよびデスクトップアプリケーション開発:クロスプラットフォーム開発は、反応および電子を通じて実現され、開発効率を向上させます。

JavaScriptの最新トレンドには、TypeScriptの台頭、最新のフレームワークとライブラリの人気、WebAssemblyの適用が含まれます。将来の見通しは、より強力なタイプシステム、サーバー側のJavaScriptの開発、人工知能と機械学習の拡大、およびIoTおよびEDGEコンピューティングの可能性をカバーしています。

JavaScriptは現代のWeb開発の基礎であり、その主な機能には、イベント駆動型のプログラミング、動的コンテンツ生成、非同期プログラミングが含まれます。 1)イベント駆動型プログラミングにより、Webページはユーザー操作に応じて動的に変更できます。 2)動的コンテンツ生成により、条件に応じてページコンテンツを調整できます。 3)非同期プログラミングにより、ユーザーインターフェイスがブロックされないようにします。 JavaScriptは、Webインタラクション、シングルページアプリケーション、サーバー側の開発で広く使用されており、ユーザーエクスペリエンスとクロスプラットフォーム開発の柔軟性を大幅に改善しています。

Pythonはデータサイエンスや機械学習により適していますが、JavaScriptはフロントエンドとフルスタックの開発により適しています。 1. Pythonは、簡潔な構文とリッチライブラリエコシステムで知られており、データ分析とWeb開発に適しています。 2。JavaScriptは、フロントエンド開発の中核です。 node.jsはサーバー側のプログラミングをサポートしており、フルスタック開発に適しています。

JavaScriptは、最新のブラウザにすでに組み込まれているため、インストールを必要としません。開始するには、テキストエディターとブラウザのみが必要です。 1)ブラウザ環境では、タグを介してHTMLファイルを埋め込んで実行します。 2)node.js環境では、node.jsをダウンロードしてインストールした後、コマンドラインを介してJavaScriptファイルを実行します。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

Dreamweaver Mac版
ビジュアル Web 開発ツール

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。
