Dragable float window with scrollable text inside : Drag Drop : GUI Components : JavaScript DHTML examples (example source code) Organized by topic

JavaScript DHTML
C++
PHP


JavaScript DHTML  »  GUI Components   » [  Drag Drop  ]   
 



Dragable float window with scrollable text inside

Please note that some example is only working under IE or Firefox.

 /* CopyRight www.youngpup.net */

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html><head>
<script language="javascript">
/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 **************************************************
 * 10.28.2001 - fixed minor bug where events
 * sometimes fired off the handle, not the root.
 **************************************************/

var Drag = {

  obj : null,

  init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
  {
    o.onmousedown  = Drag.start;

    o.hmode      = bSwapHorzRef ? false true ;
    o.vmode      = bSwapVertRef ? false true ;

    o.root = oRoot && oRoot != null ? oRoot : o ;

    if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
    if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
    if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
    if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

    o.minX  = typeof minX != 'undefined' ? minX : null;
    o.minY  = typeof minY != 'undefined' ? minY : null;
    o.maxX  = typeof maxX != 'undefined' ? maxX : null;
    o.maxY  = typeof maxY != 'undefined' ? maxY : null;

    o.xMapper = fXMapper ? fXMapper : null;
    o.yMapper = fYMapper ? fYMapper : null;

    o.root.onDragStart  = new Function();
    o.root.onDragEnd  = new Function();
    o.root.onDrag    = new Function();
  },

  start : function(e)
  {
    var o = Drag.obj = this;
    e = Drag.fixE(e);
    var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
    var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
    o.root.onDragStart(x, y);

    o.lastMouseX  = e.clientX;
    o.lastMouseY  = e.clientY;

    if (o.hmode) {
      if (o.minX != null)  o.minMouseX  = e.clientX - x + o.minX;
      if (o.maxX != null)  o.maxMouseX  = o.minMouseX + o.maxX - o.minX;
    else {
      if (o.minX != nullo.maxMouseX = -o.minX + e.clientX + x;
      if (o.maxX != nullo.minMouseX = -o.maxX + e.clientX + x;
    }

    if (o.vmode) {
      if (o.minY != null)  o.minMouseY  = e.clientY - y + o.minY;
      if (o.maxY != null)  o.maxMouseY  = o.minMouseY + o.maxY - o.minY;
    else {
      if (o.minY != nullo.maxMouseY = -o.minY + e.clientY + y;
      if (o.maxY != nullo.minMouseY = -o.maxY + e.clientY + y;
    }

    document.onmousemove  = Drag.drag;
    document.onmouseup    = Drag.end;

    return false;
  },

  drag : function(e)
  {
    e = Drag.fixE(e);
    var o = Drag.obj;

    var ey  = e.clientY;
    var ex  = e.clientX;
    var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
    var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
    var nx, ny;

    if (o.minX != nullex = o.hmode ? Math.max(ex, o.minMouseX: Math.min(ex, o.maxMouseX);
    if (o.maxX != nullex = o.hmode ? Math.min(ex, o.maxMouseX: Math.max(ex, o.minMouseX);
    if (o.minY != nulley = o.vmode ? Math.max(ey, o.minMouseY: Math.min(ey, o.maxMouseY);
    if (o.maxY != nulley = o.vmode ? Math.min(ey, o.maxMouseY: Math.max(ey, o.minMouseY);

    nx = x + ((ex - o.lastMouseX(o.hmode ? : -1));
    ny = y + ((ey - o.lastMouseY(o.vmode ? : -1));

    if (o.xMapper)    nx = o.xMapper(y)
    else if (o.yMapper)  ny = o.yMapper(x)

    Drag.obj.root.style[o.hmode ? "left" "right"= nx + "px";
    Drag.obj.root.style[o.vmode ? "top" "bottom"= ny + "px";
    Drag.obj.lastMouseX  = ex;
    Drag.obj.lastMouseY  = ey;

    Drag.obj.root.onDrag(nx, ny);
    return false;
  },

  end : function()
  {
    document.onmousemove = null;
    document.onmouseup   = null;
    Drag.obj.root.onDragEnd(  parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" "right"])
                  parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" "bottom"]));
    Drag.obj = null;
  },

  fixE : function(e)
  {
    if (typeof e == 'undefined') e = window.event;
    if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
    if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
    return e;
  }
};
</script>
<script language="javascript">
/* =======================================================
* ypSimpleScroll
* 3/11/2001

* http://www.yougnpup.net/
* ======================================================= */
ypSimpleScroll.prototype.scrollNorth = function() { this.startScroll(90) }
ypSimpleScroll.prototype.scrollSouth = function() { this.startScroll(270) }
ypSimpleScroll.prototype.scrollWest = function() { this.startScroll(180) }
ypSimpleScroll.prototype.scrollEast = function() { this.startScroll(0) }
ypSimpleScroll.prototype.startScroll = function(deg, speed) {
if (this.loaded)
{
if (this.aniTimerwindow.clearTimeout(this.aniTimer)
this.overrideScrollAngle(deg)
this.speed = speed ? speed : this.origSpeed
this.lastTime = (new Date()).getTime() this.y.minRes
this.aniTimer = window.setTimeout(this.gRef + ".scroll()"this.y.minRes)
}
}
ypSimpleScroll.prototype.endScroll = function() {
if (this.loaded)
{
window.clearTimeout(this.aniTimer)
this.aniTimer = 0;
this.speed = this.origSpeed
}
}
ypSimpleScroll.prototype.overrideScrollAngle = function(deg) {
if (this.loaded)
{
deg = deg % 360
if (deg % 90 == 0) {
var cos = deg == : deg == 180 ? -0
var sin = deg == 90 ? -: deg == 270 0
else {
var angle = deg * Math.PI / 180
var cos = Math.cos(angle)
var sin = Math.sin(angle)
sin = -sin
}
this.fx = cos / (Math.abs(cos+ Math.abs(sin))
this.fy = sin / (Math.abs(cos+ Math.abs(sin))
this.stopH = deg == 90 || deg == 270 this.scrollLeft : deg < 90 || deg > 270 this.scrollW : 0
this.stopV = deg == || deg == 180 this.scrollTop : deg < 180 this.scrollH
}
}
ypSimpleScroll.prototype.overrideScrollSpeed = function(speed) {
if (this.loadedthis.speed = speed
}
ypSimpleScroll.prototype.scrollTo = function(stopH, stopV, aniLen) {
if (this.loaded)
{
if (stopH != this.scrollLeft || stopV != this.scrollTop) {
if (this.aniTimerwindow.clearTimeout(this.aniTimer)
this.lastTime = (new Date()).getTime()
var dx = Math.abs(stopH - this.scrollLeft)
var dy = Math.abs(stopV - this.scrollTop)
var d = Math.sqrt(Math.pow(dx,2+ Math.pow(dy,2))
this.fx = (stopH - this.scrollLeft(dx + dy)
this.fy = (stopV - this.scrollTop(dx + dy)
this.stopH = stopH
this.stopV = stopV
this.speed = d / aniLen * 1000
window.setTimeout(this.gRef + ".scroll()"this.y.minRes)
}
}
}
ypSimpleScroll.prototype.jumpTo = function(nx, ny) { 
if (this.loaded)
{
nx = Math.min(Math.max(nx, 0)this.scrollW)
ny = Math.min(Math.max(ny, 0)this.scrollH)
this.scrollLeft = nx
this.scrollTop = ny
if (this.y.ns4)this.content.moveTo(-nx, -ny)
else {
this.content.style.left = -nx + "px"
this.content.style.top = -ny + "px"
}
}
}
ypSimpleScroll.minRes = 10
ypSimpleScroll.ie = document.all ? 0
ypSimpleScroll.ns4 = document.layers ? 0
ypSimpleScroll.dom = document.getElementById ? 0
ypSimpleScroll.mac = navigator.platform == "MacPPC"
ypSimpleScroll.mo5 = document.getElementById && !document.all ? 0
ypSimpleScroll.prototype.scroll = function() {
this.aniTimer = window.setTimeout(this.gRef + ".scroll()"this.y.minRes)
var nt = (new Date()).getTime()
var d = Math.round((nt - this.lastTime1000 this.speed)
if (d > 0)
{
var nx = d * this.fx + this.scrollLeft
var ny = d * this.fy + this.scrollTop
var xOut = (nx >= this.scrollLeft && nx >= this.stopH|| (nx <= this.scrollLeft && nx <= this.stopH)
var yOut = (ny >= this.scrollTop && ny >= this.stopV|| (ny <= this.scrollTop && ny <= this.stopV)
if (nt - this.lastTime != &&
((this.fx == && this.fy == 0||
(this.fy == && xOut||
(this.fx == && yOut||
(this.fx != && this.fy != && xOut && yOut)))
{
this.jumpTo(this.stopH, this.stopV)
this.endScroll()
}
else {
this.jumpTo(nx, ny)
this.lastTime = nt
}
}
}
function ypSimpleScroll(id, left, top, width, height, speed, contentWidth, initLeft, initTop)
{
var y = this.y = ypSimpleScroll
if (!initLeftinitLeft = 0
if (!initTopinitTop = 0
if (!contentWidthcontentWidth = width
if (document.layers && !y.ns4history.go(0)
if (y.ie || y.ns4 || y.dom) {
this.loaded = false
this.id = id
this.origSpeed = speed
this.aniTimer = false
this.op = ""
this.lastTime = 0
this.clipH = height
this.clipW = width
this.scrollTop = initTop
this.scrollLeft = initLeft
this.gRef = "ypSimpleScroll_"+id
eval(this.gRef+"=this")
var d = document
d.write('<style type="text/css">')
d.write('#' this.id + 'Container left:' + left + 'px; top:' + top + 'px; width:' + width + 'px; height:' + height + 'px; clip:rect(' + width + ' ' + height + ' 0); overflow:hidden; }')
d.write('#' this.id + 'Container, #' + this.id + 'Content position:absolute; }')
d.write('#' this.id + 'Content left:' + (-initLeft'px; top:' + (-initTop'px; width:' + contentWidth + 'px}')
d.write('</style>')
}
}
ypSimpleScroll.prototype.load = function() {
var d, lyrId1, lyrId2
d = document
lyrId1 = this.id + "Container"
lyrId2 = this.id + "Content"
this.container = this.y.dom ? d.getElementById(lyrId1this.y.ie ? d.all[lyrId1: d.layers[lyrId1]
this.content = obj2 = this.y.ns4 ? this.container.layers[lyrId2this.y.ie ? d.all[lyrId2: d.getElementById(lyrId2)
this.docH = Math.max(this.y.ns4 ? this.content.document.height : this.content.offsetHeight, this.clipH)
this.docW = Math.max(this.y.ns4 ? this.content.document.width : this.content.offsetWidth, this.clipW)
this.scrollH = this.docH - this.clipH
this.scrollW = this.docW - this.clipW
this.loaded = true
this.scrollLeft = Math.max(Math.min(this.scrollLeft, this.scrollW),0)
this.scrollTop = Math.max(Math.min(this.scrollTop, this.scrollH),0)
this.jumpTo(this.scrollLeft, this.scrollTop)
}
</script>
<style type="text/css">
  #root {
    position:absolute;
    height:100px;
    width:150px;
    background-color:#F4F4F4;
    border:1px solid #333;
    font-family:verdana, sans-serif;
    font-size:10px;
    }

  #handle {
    margin:2px;
    padding:2px;
    width:142px;
    color:white;
    background-color:navy;
    cursor:default;
    }
  
  #thumb {
    position:absolute;
    height:25px;
    width:11px;
    background-color:#eee;
    border:1px outset #eee;
    }

  {
    margin-top:0px;
    margin-bottom:1em;
    }
</style>

<script language="javascript">
  var theHandle, theRoot, theThumb, theScroll;
  var thumbTravel, ratio;

  theScroll = new ypSimpleScroll("scroll"21912875);
  
  window.onload = function() {
    theHandle = document.getElementById("handle");
    theRoot   = document.getElementById("root");
    theThumb  = document.getElementById("thumb");

    theScroll.load();

    Drag.init(theHandle, theRoot);
    Drag.init(theThumb, null, 1351351971);

    // the number of pixels the thumb can travel vertically (max - min)
    thumbTravel = theThumb.maxY - theThumb.minY;

    // the ratio between scroller movement and thumbMovement
    ratio = theScroll.scrollH / thumbTravel;

    theThumb.onDrag = function(x, y) {
      theScroll.jumpTo(null, Math.round((y - theThumb.minY* ratio));
    }
  
  }
</script><style type="text/css">#scrollContainer left:2px; top:19px; width:128px; height:75px; clip:rect(0 128 75 0); overflow:hidden; }#scrollContainer, #scrollContent position:absolute; }#scrollContent left:0px; top:0px; width:128px; }</style></head>


<body>
<div id="root" style="left: 20px; top: 20px;">
  <div id="handle">Handle</div>
  <div id="thumb" style="left: 135px; top: 19px;"></div>
  <div id="scrollContainer">
    <div style="left: 0px; top: 0px;" id="scrollContent">
      <p>Lorem
ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy
nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Ut wisi enim ad minim veniam, quis nostrud exercitation ulliam corper
suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem
veleum iriure dolor in hendrerit in vulputate velit esse molestie
consequat, vel willum lunombro dolore eu feugiat nulla facilisis at
vero eros et accumsan et iusto odio dignissim qui blandit praesent
luptatum zzril delenit augue duis dolore te feugait nulla facilisi. </p>
    </div>
  </div>
</div>

</body></html>

           
       
Related examples in the same category
1.  DynAPI Examples - Drag Drop /w Collision DetectionHas Download File
2.  Display Drag Icon in drag and dropHas Download File
3.  Drag Over Event: Drag and Drop the file over recycle binsHas Download File
4.  Yahoo! UI Library - Drag and Drop 1Has Download File
5.  Yahoo! UI Library - Drag and Drop 2Has Download File
6.  Yahoo! UI Library - Drag and Drop 3Has Download File
7.  Yahoo! UI Library - Drag and Drop 5Has Download File
8.  Dragged object is on topHas Download File
9.  Drag and Drop - DDProxyHas Download File
10.  Yahoo! UI Library - Drag and Drop: ResizeHas Download File
11.  Drag and Drop: Multiple groups, targetable affordanceHas Download File
12.  Drag and Drop to sort slidesHas Download File
13.  Drag and Drop to sort listsHas Download File
14.  Drag inside and outside a rangeHas Download File
15.  Drag stillHas Download File
16.  Crop an imageHas Download File
17.  Drag and drop custom window with title bar
18.  Dragable transparent window
19.  Drag within a range 2
20.  Dragable sorted listHas Download File
21.  Dragable and sortable toolbarHas Download File
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.