Home >Web Front-end >H5 Tutorial >HTML5 actual combat and analysis of native drag (two drag events dragstart, drag and dragend)

HTML5 actual combat and analysis of native drag (two drag events dragstart, drag and dragend)

黄舟
黄舟Original
2017-02-11 11:42:131765browse


Drag events

Through drag events, we can control dragging many things. Which element or where the drag event occurs is the most critical. Some events are fired on the dragged element, and some events are fired on the drop target. When an element is dragged, the events triggered are: dragstart event, drag event and dragend event.

When you press the mouse button and start moving the mouse, the dragstart event will be triggered on the dragged element. At this time, the cursor changes to a "cannot place" symbol (there is a backslash in the circle), indicating that the element cannot be placed on its own door. When dragging starts, JavaScript code can be run through the ondragstart event handler.

After the dragstart event is triggered, the drag event will be triggered immediately, and the drag event will continue to be triggered while the element is being dragged. This event is similar to the mousemove and touchmove events. When dragging stops (whether the element is placed on a valid drop target or an invalid drop target), the dragend event occurs.

The targets of the three events mentioned above are all triggered by the dragged element. By default, the browser does not change the appearance of the dragged element during dragging. But you can modify it yourself. However, most browsers create a semi-transparent copy of the element being dragged, which always follows the cursor. When an element is dragged to a valid drop target, the events that will be triggered are: dragenter event, dragover event and dragleave or drop event.

As long as an element is dragged to the drop target, the dragenter event will be triggered (similar to the mouseover event). This is followed by the dragover event, and when the dragged element is still moving within the range of the drop target, the dragover event will be triggered continuously. If the element is dragged out of the drop target, the dragover event no longer occurs, but the dragleave event is triggered (similar to the mouseout event). If the element is placed in the drop target, the drop event will be triggered instead of the dragleave event. The targets of dragenter events, dragover events, and dragleave or drop events are all elements that serve as drop targets.

Custom placement targets

When dragging an element past some invalid placement targets, you can see a special mouse gesture (a backslash in a circle), indicating that it cannot be placed . Although all elements support drop target events, these elements are not allowed to be dropped by default. If an element is dragged past an element that is not allowed to be dropped, no matter what the user does, the drop event will not occur. However, you can make any element a valid drop target by overriding the default behavior of the dragenter and dragover events.

After overriding the default behavior, you will find that when dragging the element to the drop target, the cursor changes to a symbol that allows placement. In Firefox 3.5+, the default behavior of a drop event is to open the url that was dropped on the drop target. If you drag an image onto the drop target, the page will redirect to the image file. If you drag and drop text onto the drop target, an invalid url error will result. Therefore, in order for Firefox to support normal drag and drop, it is necessary to cancel the default behavior of the drop event and prevent the URL of the dragged element from being opened. A small example is as follows

 HTML code

<!-- 拖拽”梦龙小站”到”梦龙”地方的小例子 -->
<ul>
	<li draggable="true">梦龙小站</li>
	<li draggable="true">梦龙小站</li>
	<li draggable="true">梦龙小站</li>
</ul>
<p id="p1">梦龙</p>

 CSS code

li{ width:100px; height:30px; border:1px #000000 solid; margin:20px; list-style:none;}
#p1{ width:100px; height:100px; background:red; margin:300px;}

 JavaScript code

window.onload = function(){
	var aLi = document.getElementsByTagName(&#39;li&#39;);
	var op = document.getElementById(&#39;p1&#39;);
	var iNow = 0;
	
	
	for(var i=0;i<aLi.length;i++){
		aLi[i].ondragstart = function(){ //拖拽前触发
		
			this.style.background = &#39;yellow&#39;;
		
		};
		
		aLi[i].ondragend = function(){  //拖拽结束触发
		
			this.style.background = &#39;&#39;;
		
		};
		
		/*aLi[i].ondrag = function(){ //开始与结束之间,连续触发
			
			document.title = iNow++;
			
		};*/
	}
	
	op.ondragenter = function(){  //相当于onmouseover

		this.style.background = &#39;green&#39;;
		
		ev.preventDefault();  //阻止默认事件:元素就可以释放了
		
	};
	
	op.ondragleave = function(){  //相当于onmouseout
		
		this.style.background = &#39;red&#39;;
		
	};
	
	op.ondragover = function(ev){ //进入目标、离开目标之间,连续触发
		
		ev.preventDefault();  //阻止默认事件:元素就可以释放了
		
		document.title = iNow++;
		
	};

	op.ondrop = function(ev){  //释放鼠标的时候触发

		
	
		this.style.background = &#39;red&#39;;	
	
		alert("梦龙小站,鼠标已经释放");

		ev.preventDefault();  //阻止默认事件:防止打开拖拽元素的url
	
	};
	
};

The above is the content, please check for more related content Follow the PHP Chinese website (www.php.cn)!




Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn