JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
<main class="main">
<div class="left" id="left">
<div class="txt-show">左边区域</div>
<div id='txt1' draggable="true" class="dragable txt txt1">可移动的文字一</div>
<div id='txt2' draggable="true" class="dragable txt txt2">可移动的文字二</div>
<div id='txt3' draggable="true" class="dragable txt txt3">可移动的文字三</div>
<div id='txt4' draggable="true" class="dragable txt txt4">可移动的文字四</div>
<div id='txt5' draggable="true" class="dragable txt txt5">可移动的文字五</div>
</div>
<div class="right" id='right'>
<div class="txt-show">右边区域</div>
</div>
</main>
.main {
display: flex;
justify-content: space-around;
}
.left {
width: 300px;
height: 500px;
margin-right: 10px;
border: 1px solid red;
text-align: center;
box-sizing: border-box;
padding: 1px;
}
.right {
width: 300px;
height: 500px;
border: 1px solid lightseagreen;
text-align: center;
box-sizing: border-box;
padding: 1px;
}
.txt {
border: 1px solid gray;
margin: 1px;
padding: 5px;
cursor: move;
}
let txtObj = document.getElementsByClassName('txt')
for ( let i = 0; i < txtObj.length; i++) {
txtObj[i].ondragstart = handle_start
txtObj[i].ondrag = handle_drag
txtObj[i].ondragend = handle_end
}
function handle_start(e) {
e.dataTransfer.setData('Text', e.target.id)
console.log('handle_start-拖动开始')
}
function handle_drag(e) {
console.log('handle_drag-拖动中')
}
function handle_end(e) {
console.log('handle_end-拖动结束')
}
let target = document.getElementById('right')
target.ondragenter = handle_enter
target.ondragover = handle_over
target.ondragleave = handle_leave
target.ondrop = handle_drop
let leftTarget = document.getElementById('left')
leftTarget.ondragenter = handle_enter
leftTarget.ondragover = handle_over
leftTarget.ondragleave = handle_leave
leftTarget.ondrop = handle_drop
function handle_enter(e) {
e.preventDefault()
console.log('handle_enter-进入目的地')
}
function handle_over(e) {
e.preventDefault()
let returnObj = e.dataTransfer.getData('Text')
console.log(returnObj +'-handle_over-在目的地范围内')
}
function handle_leave(e) {
e.preventDefault()
let returnObj = e.dataTransfer.getData('Text')
console.log(returnObj)
console.log('handle_leave-没有放下就离开目的地')
}
function handle_drop(e) {
e.stopPropagation();// 不再派发事件。解决Firefox浏览器,打开新窗口的问题。
e.preventDefault()
let returnObj = e.dataTransfer.getData('Text')
if (returnObj) {
e.target.appendChild(document.getElementById(returnObj))
}
console.log(returnObj + '-handle_drop-在目的地区释放')
}
Also see: Tab Triggers