HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<header>
<h1> Un ejemplo del uso de Drag & Drop en HTML5 </h1>
</header>
<section>
<div id="cuadro1" ondragenter="return enter(event)" ondragover="return over(event)" ondragleave="return leave(event)" ondrop="return drop(event)">
<div class="cuadradito" id="arrastrable1" draggable="true" ondragstart="start(event)" ondragend="end(event)">
1
</div>
<div class="cuadradito" id="arrastrable2" draggable="true" ondragstart="start(event)" ondragend="end(event)">
2
</div>
<div class="cuadradito" id="arrastrable3" draggable="true" ondragstart="start(event)" ondragend="end(event)">
3
</div>
</div>
<div id="cuadro2" ondragenter="return enter(event)" ondragover="return over(event)" ondragleave="return leave(event)" ondrop="return drop(event)">
</div>
<div id="cuadro3" ondragenter="return enter(event)" ondragover="return over(event)" ondragleave="return leave(event)" ondrop="return clonar(event)">
Clonadora
</div>
<div id="papelera" ondragenter="return enter(event)" ondragover="return over(event)" ondragleave="return leave(event)" ondrop="return eliminar(event)">Papelera</div>
</section>
body{
background: #303030;
}
h1 {
color: #CA7218;
}
#cuadro1, #cuadro2, #cuadro3, #papelera {
float: left;
width: 90px;
height: 90px;
padding: 10px;
margin: 10px;
}
#cuadro1{
background: #55f;
}
#cuadro2 {
background: #5f5;
}
#cuadro3 {
background: #f55;
}
#papelera {
background: #5ff;
}
.cuadradito {
background: #288;
border: 2px solid #882;
width: 5px;
height: 5px;
padding: 0 10px 15px;
margin: 5px;
}
#arrastrable1 {
background: #228;
border: 2px solid #822;
}
#arrastrable2 {
background: #282;
border: 2px solid #228;
}
#arrastrable3 {
background: #822;
border: 2px solid #282;
contador = 0; // Variable global para tener poder poner un id unico a cada elemento cuando se clona.
function start(e) {
e.dataTransfer.effecAllowed = 'move'; // Define el efecto como mover (Es el por defecto)
e.dataTransfer.setData("Data", e.target.id); // Coje el elemento que se va a mover
e.dataTransfer.setDragImage(e.target, 0, 0); // Define la imagen que se vera al ser arrastrado el elemento y por donde se coje el elemento que se va a mover (el raton aparece en la esquina sup_izq con 0,0)
e.target.style.opacity = '0.4';
}
function end(e){
e.target.style.opacity = ''; // Pone la opacidad del elemento a 1
e.dataTransfer.clearData("Data");
}
function enter(e) {
e.target.style.border = '3px dotted #555';
}
function leave(e) {
e.target.style.border = '';
}
function over(e) {
var elemArrastrable = e.dataTransfer.getData("Data"); // Elemento arrastrado
var id = e.target.id; // Elemento sobre el que se arrastra
// return false para que se pueda soltar
if (id == 'cuadro1'){
return false; // Cualquier elemento se puede soltar sobre el div destino 1
}
if ((id == 'cuadro2') && (elemArrastrable != 'arrastrable3')){
return false; // En el cuadro2 se puede soltar cualquier elemento menos el elemento con id=arrastrable3
}
if (id == 'cuadro3')
return false;
if (id == 'papelera')
return false; // Cualquier elemento se puede soltar en la papelera
}
/**
*
* Mueve el elemento
*
**/
function drop(e){
var elementoArrastrado = e.dataTransfer.getData("Data"); // Elemento arrastrado
e.target.appendChild(document.getElementById(elementoArrastrado));
e.target.style.border = ''; // Quita el borde
tamContX = $('#'+e.target.id).width();
tamContY = $('#'+e.target.id).height();
tamElemX = $('#'+elementoArrastrado).width();
tamElemY = $('#'+elementoArrastrado).height();
posXCont = $('#'+e.target.id).position().left;
posYCont = $('#'+e.target.id).position().top;
// Posicion absoluta del raton
x = e.layerX;
y = e.layerY;
// Si parte del elemento que se quiere mover se queda fuera se cambia las coordenadas para que no sea asi
if (posXCont + tamContX <= x + tamElemX){
x = posXCont + tamContX - tamElemX;
}
if (posYCont + tamContY <= y + tamElemY){
y = posYCont + tamContY - tamElemY;
}
document.getElementById(elementoArrastrado).style.position = "absolute";
document.getElementById(elementoArrastrado).style.left = x + "px";
document.getElementById(elementoArrastrado).style.top = y + "px";
}
/**
*
* Elimina el elemento que se mueve
*
**/
function eliminar(e){
var elementoArrastrado = document.getElementById(e.dataTransfer.getData("Data")); // Elemento arrastrado
elementoArrastrado.parentNode.removeChild(elementoArrastrado); // Elimina el elemento
e.target.style.border = ''; // Quita el borde
}
/**
*
* Clona el elemento que se mueve
*
**/
function clonar(e){
var elementoArrastrado = document.getElementById(e.dataTransfer.getData("Data")); // Elemento arrastrado
elementoArrastrado.style.opacity = ''; // Dejamos la opacidad a su estado anterior para copiar el elemento igual que era antes
var elementoClonado = elementoArrastrado.cloneNode(true); // Se clona el elemento
elementoClonado.id = "ElemClonado" + contador; // Se cambia el id porque tiene que ser unico
contador += 1;
elementoClonado.style.position = "static"; // Se posiciona de forma "normal" (Sino habria que cambiar las coordenadas de la posición)
e.target.appendChild(elementoClonado); // Se añade el elemento clonado
e.target.style.border = ''; // Quita el borde del "cuadro clonador"
}
Also see: Tab Triggers