0

I am developing an web application where I am trying simulate a popup window using tags , for style purpose. I done this with this javascript code:

<script>
  function handleClick(url){
      document.getElementById("results").style.display = 'block';
      document.getElementById("about").innerHTML='<object type="text/html" data="'+url+'" ></object>';
      }
  function cleanDiv() {
      document.getElementById("results").style.display = 'none';
      document.getElementById("about").innerHTML=' ';
  }
  </script>

associated to this html code:

<section class="about" id="results">
    <div align="right"><a href="#" onclick="cleanDiv()">Fechar</a></div>
    <div id="about" algin="center"></div>
  </section>

and the style is on my css file.

I have almost all what I want, but I wish this "popup window" don't stay fixed in a unique position on the page, and the user could move it around with the mouse.

Someone knows how to make this with javascript/html/css only?

Kleber Mota
  • 8,521
  • 31
  • 94
  • 188

2 Answers2

0

You are looking for the HTML5 draggable attribute and events. Make the element draggable by setting draggable="true" and ondragstart="dragElem(event)". Then write your code in function dragElem(ev) { }. See W3Schools

neelsg
  • 4,802
  • 5
  • 34
  • 58
  • this option "draggable", where I should set it? In css? In html? if I just set this, I really will need of the function dragElem? If yes, what I should handle in this function? – Kleber Mota Mar 04 '14 at 14:19
  • @KleberMota: This is an attribute in HTML for the element you wish to drag. Please read up on some of the tutorials and try a few things. I will not give you the exact implementation details, I'm just pointing you in the right direction – neelsg Mar 04 '14 at 14:41
0

After more search, I ended with this code, from site http://waseemblog.com/42/movable-div-using-javascript.html

html:

<section class="about" id="results" style="left: 183px; top: 111px;" onMouseDown="dragStart(event, 'results');">
    <div align="right"><a href="#" class="classname" onclick="cleanDiv()">X</a></div>
    <div id="content" align="center"></div>
  </section>

javascript:

function getID(id)
{
    return document.getElementById(id);
}

// Global object to hold drag information.
var dragObj = new Object();

function dragStart(event, id) {
  var x, y;
  dragObj.elNode = getID(id);
  // Get cursor position with respect to the page.
  try {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  catch (e) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }
  // Save starting positions of cursor and element.
  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);
  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;
  // Capture mousemove and mouseup events on the page.
  try {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  catch (e) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}

function dragGo(event) {
 var x, y;
 // Get cursor position with respect to the page.
 try  {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  catch (e) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }
  // Move drag element by the same amount the cursor has moved.
  var drLeft = (dragObj.elStartLeft + x - dragObj.cursorStartX);
  var drTop = (dragObj.elStartTop  + y - dragObj.cursorStartY);
  if (drLeft > 0)
  {
     dragObj.elNode.style.left = drLeft  + "px";
  }
  else
  {
    dragObj.elNode.style.left = "1px";
  }
  if (drTop > 0)
  {
     dragObj.elNode.style.top  = drTop + "px";
  }
  else
  {
    dragObj.elNode.style.top  = "1px";
  }
  try {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  catch(e){
    event.preventDefault();
  }
}

function dragStop(event) {
  // Stop capturing mousemove and mouseup events.
  try {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  catch (e) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}

It can be bigger than your counterpart using jquery, but I guess it works fine in most browsers available today.

Kleber Mota
  • 8,521
  • 31
  • 94
  • 188