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

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.

HTML

            
              <h2>拖拽红色的小方块到蓝色方块中</h2>
<div id="drap" draggable="true"></div>
<div id="box"></div>
<img id="pic" src="http://s1.dwstatic.com/group1/M00/4E/22/9df96aa3d92f54d3ef87c0f2f120a858.jpg" width="50" height="50"/>
            
          
!

CSS

            
              h2 {
  text-align:center;
}
#drap {
	width: 100px;
	height: 100px;
	background: red;
}
#box {
	width: 500px;
	height: 500px;
	border: 2px solid blue;
	margin: 50px auto;
}
            
          
!

JS

            
              var drap = document.getElementById("drap");
var box = document.getElementById("box");
var pic = document.getElementById("pic");

//被拖拽元素事件
drap.ondragstart = function(ev){ // 拖拽前
	var ev = ev || window.event;
	//火狐浏览器下需设置dataTransfer对象才可以拖拽除图片外的其他标签
	ev.dataTransfer.setData("key","poetries");
	//effectAllowed : 设置光标样式(none, copy, copyLink, copyMove, link, linkMove,move, all 和uninitialized)
	ev.dataTransfer.effectAllowed = "copy";
	//设置被拖拽的小元素 setDragImage :三个参数(指定的元素,坐标X,坐标Y)
	ev.dataTransfer.setDragImage(pic,25,25);
	this.style.background = "green";
}

drap.ondrag = function(){ // 拖拽过程中
	this.innerText = "被拖拽中...";
}

drap.ondragend = function(){ // 拖拽结束
	this.style.background = "red";
	this.innerHTML = "";
}

//目标元素事件
box.ondragenter = function(){ //进入目标元素触发
	this.innerHTML = "可将文件拖放到这里!";
}
box.ondragover = function(ev){ //进入目标、离开目标之间,连续触发
	var ev = ev || window.event;
	ev.preventDefault(); 
	this.style.background = "pink";
}
box.ondragleave = function(){ //离开目标元素触发
	this.innerHTML = "";
	this.style.background = "none";
}
box.ondrop = function(ev){//在目标元素上释放鼠标触发
	//alert("拖放结束")
	this.innerHTML = ev.dataTransfer.getData("key");
}
            
          
!
999px

Console