{"id":55532,"date":"2022-04-28T03:01:48","date_gmt":"2022-04-27T17:01:48","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=55532"},"modified":"2022-04-29T12:32:25","modified_gmt":"2022-04-29T02:32:25","slug":"drag-and-drop-iframe-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/drag-and-drop-iframe-primer-tutorial\/","title":{"rendered":"Drag and Drop Iframe Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/drag_around.html\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"Drag and Drop Iframe Primer Tutorial\"  src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/drag_around.jpg\" title=\"Drag and Drop Iframe Primer Tutorial\" \/><\/a><p class=\"wp-caption-text\">Drag and Drop Iframe Primer Tutorial<\/p><\/div>\n<p>We were &#8220;really taken&#8221; by the possibilities trying out  <a target=_blank href='https:\/\/www.w3schools.com\/howto\/howto_js_draggable.asp' title='How To Create a Draggable HTML Element' title='Thanks to W3Schools'>How To Create a Draggable HTML Element<\/a> (by <a target=_blank href='https:\/\/www.w3schools.com\/' title='W3Schools'>W3Schools<\/a>), to drag and drop elements on non-mobile webpages.  So much so, we wrote a &#8220;proof of concept&#8221; dragger and dropper of up to three iframe elements in the one webpage.<\/p>\n<p>The Javascript &#8230;<\/p>\n<p><code><br \/>\n&lt;script type='text\/javascript'&gt;<br \/>\nvar goh='';<br \/>\nvar css='';<br \/>\nvar laste=null;<br \/>\nvar sofar=',';<br \/>\n<br \/>\nfunction dragit() {<br \/>\n  var sx='', isx=1;<br \/>\n  var ksx=isx;<br \/>\n  var rects=null;<br \/>\n<br \/> <br \/>\n  if (laste) {<br \/>\n<br \/>\n  isx=1;<br \/>\n  for (var jsx=1; jsx&lt;=ksx; jsx++) {<br \/>\n    \/\/if (document.getElementById(\"mydiv\" + sx + \"header\")) {<br \/>\n    \/\/  document.getElementById(\"mydiv\" + sx + \"header\").style.position='absolute';<br \/>\n    \/\/}<br \/>\n    if (document.getElementById(\"mydiv\" + sx) && sofar.indexOf(',if' + sx + ',') == -1) {<br \/>\n      \/\/alert(sx);<br \/>\n      dragElement(document.getElementById(\"mydiv\" + sx));<br \/>\n    }<br \/>\n    isx++;<br \/>\n    sx='' + isx;<br \/>\n  }<br \/>\n<br \/>\n  } else {<br \/>\n  \/\/ Make the DIV(s) element draggable:<br \/>\n  ksx=4;<br \/>\n  sx='4';<br \/>\n  while (!document.getElementById(\"mydiv\" + sx)) { ksx--; sx=('' + ksx).replace(\/^1$\/g,''); }<br \/>\n  isx=ksx;<br \/>\n  while (document.getElementById(\"mydiv\" + sx)) {<br \/>\n    if (document.getElementById(\"mydiv\" + sx + \"header\")) {<br \/>\n      rects=document.getElementById(\"mydiv\" + sx + \"header\").getBoundingClientRect();<br \/>\n      document.getElementById(\"mydiv\" + sx + \"header\").style.position='absolute';<br \/>\n      document.getElementById(\"mydiv\" + sx + \"header\").style.top='' + rects.top + 'px';<br \/>\n      document.getElementById(\"mydiv\" + sx + \"header\").style.left='' + rects.left + 'px';<br \/>\n    }<br \/>\n    dragElement(document.getElementById(\"mydiv\" + sx));<br \/>\n    isx--;<br \/>\n    sx=('' + isx).replace(\/^1$\/g,'');<br \/>\n  }<br \/>\n  isx=ksx;<br \/>\n  \/\/alert(isx);<br \/>\n  if (1 == 3) {<br \/>\n  if (isx == 2) {<br \/>\n    document.getElementById(\"mydiv\" + \"header\").style.height='100vh';<br \/>\n  } else if (isx == 3) {<br \/>\n    document.getElementById(\"mydiv\" + \"header\").style.height='40vh';<br \/>\n    document.getElementById(\"mydiv2\" + \"header\").style.height='40vh';<br \/>\n    \/\/document.getElementById(\"if\" + \"\").style.height='100%';<br \/>\n    \/\/document.getElementById(\"if2\" + \"\").style.height='100%';<br \/>\n  } else {<br \/>\n    document.getElementById(\"mydiv\" + \"header\").style.height='30vh';<br \/>\n    document.getElementById(\"mydiv2\" + \"header\").style.height='30vh';<br \/>\n    document.getElementById(\"mydiv3\" + \"header\").style.height='30vh';<br \/>\n    \/\/document.getElementById(\"if\" + \"\").style.height='100%';<br \/>\n    \/\/document.getElementById(\"if2\" + \"\").style.height='100%';<br \/>\n    \/\/document.getElementById(\"if3\" + \"\").style.height='100%';<br \/>\n  }<br \/>\n  }<br \/>\n<br \/> <br \/>\n  sx='';<br \/>\n  ksx=isx;<br \/>\n  isx=1;<br \/>\n  for (var jsx=1; jsx&lt;=ksx; jsx++) {<br \/>\n    \/\/if (document.getElementById(\"mydiv\" + sx + \"header\")) {<br \/>\n    \/\/  document.getElementById(\"mydiv\" + sx + \"header\").style.position='absolute';<br \/>\n    \/\/}<br \/>\n    \/\/if (document.getElementById(\"mydiv\" + sx)) {<br \/>\n    \/\/  dragElement(document.getElementById(\"mydiv\" + sx));<br \/>\n    \/\/}<br \/>\n    if (document.getElementById(\"mydiv\" + sx + \"header\") && !document.getElementById(\"if\" + sx)) {<br \/>\n      document.getElementById(\"mydiv\" + sx + \"header\").style.visibility='hidden';<br \/>\n      document.getElementById(\"mydiv\" + sx + \"header\").style.display='none';<br \/>\n    }<br \/>\n    isx++;<br \/>\n    sx='' + isx;<br \/>\n    if (document.getElementById(\"mydiv\" + sx + \"header\") && !document.getElementById(\"if\" + sx)) {<br \/>\n      document.getElementById(\"mydiv\" + sx + \"header\").style.visibility='hidden';<br \/>\n      document.getElementById(\"mydiv\" + sx + \"header\").style.display='none';<br \/>\n    }<br \/>\n  }<br \/>\n  }<br \/>\n<br \/> <br \/>\n}<br \/>\n<br \/>\nfunction dragElement(elmnt) {<br \/>\n  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;<br \/>\n  if (document.getElementById(elmnt.id + \"header\")) {<br \/>\n    \/\/ if present, the header is where you move the DIV from:<br \/>\n    document.getElementById(elmnt.id + \"header\").onmousedown = dragMouseDown;<br \/>\n  } else {<br \/>\n    \/\/ otherwise, move the DIV from anywhere inside the DIV:<br \/>\n    elmnt.onmousedown = dragMouseDown;<br \/>\n  }<br \/>\n<br \/>\n  function dragMouseDown(e) {<br \/>\n    e = e || window.event;<br \/>\n    e.preventDefault();<br \/>\n    \/\/ get the mouse cursor position at startup:<br \/>\n    pos3 = e.clientX;<br \/>\n    pos4 = e.clientY;<br \/>\n    laste=e.target;<br \/>\n    document.onmouseup = closeDragElement;<br \/>\n    \/\/ call a function whenever the cursor moves:<br \/>\n    document.onmousemove = elementDrag;<br \/>\n  }<br \/>\n<br \/>\n  function elementDrag(e) {<br \/>\n    e = e || window.event;<br \/>\n    e.preventDefault();<br \/>\n    \/\/ calculate the new cursor position:<br \/>\n    pos1 = pos3 - e.clientX;<br \/>\n    pos2 = pos4 - e.clientY;<br \/>\n    pos3 = e.clientX;<br \/>\n    pos4 = e.clientY;<br \/>\n    \/\/ set the element's new position:<br \/>\n    elmnt.style.top = (elmnt.offsetTop - pos2) + \"px\";<br \/>\n    elmnt.style.left = (elmnt.offsetLeft - pos1) + \"px\";<br \/>\n    goh=elmnt.outerHTML;<br \/>\n    if (css == '') {<br \/>\n      css=\"?css\" + e.target.id.replace('mydiv','').replace('header','').replace('if','') + \"=\" + encodeURIComponent('top:' + elmnt.style.top + ';left:' + elmnt.style.left + ';');<br \/>\n    } else {<br \/>\n      if (css.indexOf('css' + e.target.id.replace('mydiv','').replace('header','') + '=') != -1) {<br \/>\n        css=css.replace(css.split('css' + e.target.id.replace('mydiv','').replace('header','').replace('if','') + '=')[1].split('&')[0], encodeURIComponent('top:' + elmnt.style.top + ';left:' + elmnt.style.left + ';'));<br \/>\n      } else {<br \/>\n        css+=\"&css\" + e.target.id.replace('mydiv','').replace('header','').replace('if','') + \"=\" + encodeURIComponent('top:' + elmnt.style.top + ';left:' + elmnt.style.left + ';');<br \/>\n      }<br \/>\n    }<br \/>\n    console.log(goh);<br \/>\n  }<br \/>\n<br \/>\n  function closeDragElement() {<br \/>\n    \/\/ stop moving when mouse button is released:<br \/>\n    sofar+=laste.id + ',';<br \/>\n    document.onmouseup = null;<br \/>\n    document.onmousemove = null;<br \/>\n    \/\/alert(goh);<br \/>\n    \/\/dragit();<br \/>\n    goh='';<br \/>\n    if (css != '') {<br \/>\n      \/\/document.getElementById('myh1').innerHTML+=' ' + decodeURIComponent(css);<br \/>\n      setTimeout(st, 3000);<br \/>\n    }<br \/>\n  }<br \/>\n}<br \/>\n<br \/> <br \/>\n  function st() {<br \/>\n      if ((location.search.split('css=')[1] ? decodeURIComponent(location.search.split('css=')[1].split('&')[0]) : '') != '' && css.indexOf('css=') == -1) {<br \/>\n       css+='&css=' + encodeURIComponent((location.search.split('css=')[1] ? decodeURIComponent(location.search.split('css=')[1].split('&')[0]) : ''));<br \/>\n      }<br \/>\n      if ((location.search.split('css2=')[1] ? decodeURIComponent(location.search.split('css2=')[1].split('&')[0]) : '') != '' && css.indexOf('css2=') == -1) {<br \/>\n       css+='&css2=' + encodeURIComponent((location.search.split('css2=')[1] ? decodeURIComponent(location.search.split('css2=')[1].split('&')[0]) : ''));<br \/>\n      }<br \/>\n      if ((location.search.split('css3=')[1] ? decodeURIComponent(location.search.split('css3=')[1].split('&')[0]) : '') != '' && css.indexOf('css3=') == -1) {<br \/>\n       css+='&css3=' + encodeURIComponent((location.search.split('css3=')[1] ? decodeURIComponent(location.search.split('css3=')[1].split('&')[0]) : ''));<br \/>\n      }<br \/>\n      \/\/location.href=document.URL.split('#')[0].split('?')[0] + css + '&numsel=' + document.getElementById('numsel').value;<br \/>\n  }<br \/>\n&lt;\/script&gt;<br \/>\n<\/code><\/p>\n<p> &#8230; is interesting in the timing of when some &#8220;drag header&#8221; elements should be turned into <i>position:absolute<\/i> (with top:[&#8220;drag header&#8221; element].<a target=_blank title='Javascript DOM getBoundingClientRect information from W3schools' href='https:\/\/www.w3schools.com\/jsref\/met_element_getboundingclientrect.asp'>getBoundingClientRect<\/a>().top and left:[&#8220;drag header&#8221; element].getBoundingClientRect().left) determined in reverse order so that the initial <i>relative<\/i> position can be the guide for the establishment of reasonable <i>top<\/i> and <i>left<\/i> <i>absolute<\/i> positions.<\/p>\n<p>So feel free to try today&#8217;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/drag_around.html_GETME\">proof of concept<\/a> <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/drag_around.html\" title=\"Click picture\">&#8220;Drag Around&#8221; web application<\/a>.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d55532' onclick='var dv=document.getElementById(\"d55532\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/drag\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d55532' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>We were &#8220;really taken&#8221; by the possibilities trying out How To Create a Draggable HTML Element (by W3Schools), to drag and drop elements on non-mobile webpages. So much so, we wrote a &#8220;proof of concept&#8221; dragger and dropper of up &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/drag-and-drop-iframe-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,14,37],"tags":[2824,354,2237,364,1549,2208,576,587,652,3272,1871,876,894,1988,997,1986,2945,1292,1319,1418],"class_list":["post-55532","post","type-post","status-publish","format-standard","hentry","category-elearning","category-event-driven-programming","category-tutorials","tag-absolute","tag-dom","tag-drag","tag-drag-and-drop","tag-element","tag-getboundingclientrect","tag-html","tag-iframe","tag-javascript","tag-left","tag-move","tag-opacity","tag-overlay","tag-position","tag-programming","tag-proof-of-concept","tag-relative","tag-top","tag-tutorial","tag-webpage"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/55532"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=55532"}],"version-history":[{"count":7,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/55532\/revisions"}],"predecessor-version":[{"id":55550,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/55532\/revisions\/55550"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=55532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=55532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=55532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}