Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import or require. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <h3>从外部拖拽一张图片到红框中试试</h3>
<div id="box"><span>可以将文件拖放到这里!!</span></div>
<div id="dustbin">垃圾回收站</div>
            
          
!
            
              h3 {
  text-align:center;
}
#box{
			position:relative;
			width:500px;
			height:500px;
			border:2px solid red;
			margin:10px auto 0px;
			
		}
		#box span{
			position:absolute;
			left:0;
			top:0;
			right:0;
			bottom:0;
			height:50px;
			width:192px;
			margin:auto;
			display:none;
		}
		img{
			width:100px;height:100px;
		}
		#dustbin{
			width:200px;
			height:100px;
			background:#000;
			color:#fff;
			font-size:40px;
			text-align:center;
			line-height:100px;
			margin:auto;
		}
            
          
!
            
              var box = document.getElementById("box");
var dusTbin = document.getElementById("dustbin");
var span = box.getElementsByTagName("span")[0];
//目标元素事件
var img = '';
box.ondragenter = function(){//进入目标元素触发
	span.style.display = "block";
}
box.ondragover = function(ev){//在目标元素上连续触发
	var ev = ev||window.event;
	ev.preventDefault();//阻止默认事件
	span.style.display = "block";
}
box.ondragleave = function(){//离开目标元素
	span.style.display = "none";
}
box.ondrop = function(ev){//在目标元素上面释放鼠标触发
	//alert("拖拽结束!!");
	var ev = ev||window.event;
	ev.preventDefault();//阻止默认事件
	span.style.display = "none";
	var file = ev.dataTransfer.files;
	//alert(file[0].type);
	for (var i=0; i<file.length ;i++ )
	{
		if (file[i].type.indexOf("image")!=-1)
		{
			var read = new FileReader();//新建一个读取文件对象
			read.readAsDataURL(file[i]);//读取文件
			read.onload = function(){//读取文件成功之后调用什么函数
				var img = document.createElement("img");
				//alert(this.result);
				img.src = this.result;
				box.appendChild(img);
				//获取img节点 实现删除功能
				var oImg = document.getElementsByTagName("img");
				if (oImg)
				{
					for (var j=0;j<oImg.length ;j++ )
					{
						oImg[j].ondragstart = function(ev){
							ev.dataTransfer.setData("data",ev.target.innerHTML);
							img = ev.target;
						}
						oImg[j].ondragend = function(ev){
							ev.dataTransfer.clearData("data");//清楚数据
							img = null;
						}
					}

				}
				//实现删除功能(移除img节点)
				dusTbin.ondragover = function(ev){
					ev.preventDefault();
				}
				dusTbin.ondrop = function(){
					if (img)
					{
						img.parentNode.removeChild(img);
					}
				}
			}
		}else{
			alert("请上传图片!");
		}
		
	}
	
}
            
          
!
999px

Console