1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| <!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Draggable input type="range"</title>
<meta name="Author" content="NoSmoking">
<style>
html, body{
padding:0;
margin:1em;
font:1em/1.25em Verdana, Arial, sans-serif;
background:#FFF;
}
form {
position:absolute;
margin:0;
padding:1em;
left:5em;
top:5em;
border:1px solid #abc;
background:#eef;
box-shadow: 2px 2px 5px #ccc;
width:20em;
}
form.over{
background:#eee;
box-shadow: 0px 0px 0px #888;
opacity:0.5;
}
form:hover{
border:1px dashed #abc;
cursor: move;
}
input[type="range"]{
cursor:pointer;
}
</style>
</head>
<body>
<h1>Draggable <code>input type="range"</code></h1>
<form draggable="true">
déplacez-moi !
<input type="range">
</form>
<script>
// récup. de l'élément FORM
var oElem = document.querySelectorAll('form')[0];
// mise en place d'un flag
oElem.actif = true;
// test si clic sur élément lui même ou sur enfant
oElem.addEventListener( 'mousedown', function(e) {
// récup. élément déclencheur
var oTarget = e.target || e.srcElement;
this.actif = oTarget == this;
}, false);
// affecte l'événement minimum
oElem.addEventListener( 'dragstart', function(e) {
if (this.actif) {
this.classList.toggle('over');
// positions de départ de l'élément
this.posDrag = {
dX : e.screenX,
dY : e.screenY,
oX : this.offsetLeft,
oY : this.offsetTop
};
// par exemple
e.dataTransfer.setData('Text', this.id);
} else {
// annule action par défaut
e.preventDefault();
}
}, false);
// retour à la normale
oElem.addEventListener( 'dragend', function(e) {
this.classList.toggle('over');
// positionne l'élément à sa nouvelle place
this.style.left = (this.posDrag.oX +(e.screenX -this.posDrag.dX)) +'px',
this.style.top = (this.posDrag.oY +(e.screenY -this.posDrag.dY)) +'px';
}, false);
</script>
</body>
</html> |
Partager