EstebanDLPT (Talk | contribs) m |
Davidacevedo (Talk | contribs) |
||
Line 250: | Line 250: | ||
</script> | </script> | ||
+ | |||
+ | <script> | ||
+ | // target elements with the "draggable" class | ||
+ | interact('.draggable') | ||
+ | .draggable({ | ||
+ | // enable inertial throwing | ||
+ | inertia: true, | ||
+ | // keep the element within the area of it's parent | ||
+ | restrict: { | ||
+ | restriction: "parent", | ||
+ | endOnly: true, | ||
+ | elementRect: { | ||
+ | top: 0, | ||
+ | left: 0, | ||
+ | bottom: 1, | ||
+ | right: 1 | ||
+ | } | ||
+ | }, | ||
+ | // enable autoScroll | ||
+ | autoScroll: true, | ||
+ | |||
+ | // call this function on every dragmove event | ||
+ | onmove: dragMoveListener, | ||
+ | // call this function on every dragend event | ||
+ | onend: function(event) { | ||
+ | var textEl = event.target.querySelector('p'); | ||
+ | |||
+ | textEl && (textEl.textContent = | ||
+ | 'moved a distance of ' + | ||
+ | (Math.sqrt(Math.pow(event.pageX - event.x0, 2) + | ||
+ | Math.pow(event.pageY - event.y0, 2) | 0)) | ||
+ | .toFixed(2) + 'px'); | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | function dragMoveListener(event) { | ||
+ | var target = event.target, | ||
+ | // keep the dragged position in the data-x/data-y attributes | ||
+ | x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx, | ||
+ | y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy; | ||
+ | |||
+ | // translate the element | ||
+ | target.style.webkitTransform = | ||
+ | target.style.transform = | ||
+ | 'translate(' + x + 'px, ' + y + 'px)'; | ||
+ | |||
+ | // update the posiion attributes | ||
+ | target.setAttribute('data-x', x); | ||
+ | target.setAttribute('data-y', y); | ||
+ | } | ||
+ | |||
+ | |||
+ | /* The dragging code for '.draggable' from the demo above | ||
+ | * applies to this demo as well so it doesn't have to be repeated. */ | ||
+ | |||
+ | // enable draggables to be dropped into this | ||
+ | interact('.dropzone').dropzone({ | ||
+ | // only accept elements matching this CSS selector | ||
+ | // accept: '#yes-drop', | ||
+ | // Require a 75% element overlap for a drop to be possible | ||
+ | overlap: 0.75, | ||
+ | |||
+ | // listen for drop related events: | ||
+ | |||
+ | ondropactivate: function(event) { | ||
+ | // activates when you grab an object | ||
+ | // add active dropzone feedback | ||
+ | event.target.classList.add('drop-active'); | ||
+ | //console.log("a"); | ||
+ | }, | ||
+ | ondragenter: function(event) { | ||
+ | // activates when object enters dropzone | ||
+ | var draggableElement = event.relatedTarget, | ||
+ | dropzoneElement = event.target; | ||
+ | |||
+ | // feedback the possibility of a drop | ||
+ | dropzoneElement.classList.add('drop-target'); | ||
+ | draggableElement.classList.add('can-drop'); | ||
+ | // draggableElement.textContent = 'Dragged in'; | ||
+ | //console.log("b"); | ||
+ | }, | ||
+ | ondragleave: function(event) { | ||
+ | // activates when object leaves dropzone | ||
+ | // remove the drop feedback style | ||
+ | event.target.classList.remove('drop-target'); | ||
+ | event.relatedTarget.classList.remove('can-drop'); | ||
+ | //event.relatedTarget.textContent = 'Dragged out'; | ||
+ | //console.log("c"); | ||
+ | }, | ||
+ | ondrop: function(event) { | ||
+ | // activates when object is dropped in dropzone | ||
+ | // event.relatedTarget.textContent = 'Dropped'; | ||
+ | var draggableElementID = event.relatedTarget.id; | ||
+ | if (event.relatedTarget.id == "alan") { | ||
+ | document.getElementById(draggableElementID).style.display = "none"; | ||
+ | document.getElementById("dropzoneImage").src="https://media1.tenor.com/images/9efdeb655742370ffdedf960649d5150/tenor.gif?itemid=10898740"; | ||
+ | } else if (event.relatedTarget.id == "esteban") { | ||
+ | document.getElementById(draggableElementID).style.display = "none"; | ||
+ | document.getElementById("dropzoneImage").src="https://i.ytimg.com/vi/8r-9Ep9rarA/maxresdefault.jpg"; | ||
+ | } else if (event.relatedTarget.id == "acebae") { | ||
+ | document.getElementById(draggableElementID).style.display = "none"; | ||
+ | document.getElementById("dropzoneImage").src="https://acedog.co/images/acedog-627x378.png"; | ||
+ | } else if (event.relatedTarget.id == "sofi") { | ||
+ | document.getElementById(draggableElementID).style.display = "none"; | ||
+ | document.getElementById("dropzoneImage").src="https://i.ytimg.com/vi/mU_uE-d870U/maxresdefault.jpg"; | ||
+ | } else if (event.relatedTarget.id == "bobby") { | ||
+ | document.getElementById(draggableElementID).style.display = "none"; | ||
+ | document.getElementById("dropzoneImage").src="https://i.redd.it/wc8gkbw5kxv01.jpg"; | ||
+ | } | ||
+ | //console.log("d"); | ||
+ | }, | ||
+ | ondropdeactivate: function(event) { | ||
+ | // activates when you drop an object anywhere | ||
+ | // remove active dropzone feedback | ||
+ | event.target.classList.remove('drop-active'); | ||
+ | event.target.classList.remove('drop-target'); | ||
+ | //console.log("e"); | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | interact('.drag-drop') | ||
+ | .draggable({ | ||
+ | inertia: true, | ||
+ | restrict: { | ||
+ | restriction: "parent", | ||
+ | endOnly: true, | ||
+ | elementRect: { | ||
+ | |||
+ | top: 0, | ||
+ | left: 0, | ||
+ | bottom: 1, | ||
+ | right: 1 | ||
+ | } | ||
+ | }, | ||
+ | autoScroll: true, | ||
+ | // dragMoveListener from the dragging demo above | ||
+ | onmove: dragMoveListener, | ||
+ | }); | ||
+ | </script> | ||
<!-- Custom JavaScript --> | <!-- Custom JavaScript --> |
Revision as of 21:43, 13 October 2018