拖动鼠标改变div层的大小宽度

<html>

<head>
<title>拖动鼠标改变div层的大小宽度-石家庄色彩顾问-亿诚</title>
<meta content="text/html; charset=gb2312" http-equiv="Content-Type">
<style> {
 box-sizing: border-box; moz-box-sizing: border-box
}
#testDiv     { background-color: buttonface; background-repeat: repeat;
               background-attachment: scroll; color: #3969A5; height: 300px;
               left: 30px; overflow: hidden; width: 500; z-index: 2;
               border: 2px outset white; margin: 0px; padding: 2px;
               background-position: 0% 50% }
body         { font-family: Verdana; font-size: 9pt }
#innerNice   { background-color: white; background-repeat: repeat; background-attachment:
               scroll; color: #3969A5; height: 100%; overflow: auto; width:
               100%; border: 2px inset white; padding: 8px;
               background-position: 0% 50% }
</style>

<SCRIPT language=javascript>
/////////////////////////////////////////////////////////////////////////
// Generic Resize by Erik Arvidsson                                    //
//                                                                     //
// You may use this script as long as this disclaimer is remained.     //
// See www.dtek.chalmers.se/~d96erik/dhtml/ for mor info               //
//                                                                     //
// How to use this script!                                             //
// Link the script in the HEAD and create a container (DIV, preferable //
// absolute positioned) and add the class="resizeMe" to it.            //
/////////////////////////////////////////////////////////////////////////

var theobject = null; //This gets a value as soon as a resize start

function resizeObject() {
 this.el        = null; //pointer to the object
 this.dir    = "";      //type of current resize (n, s, e, w, ne, nw, se, sw)
 this.grabx = null;     //Some useful values
 this.graby = null;
 this.width = null;
 this.height = null;
 this.left = null;
 this.top = null;
}

//Find out what kind of resize! Return a string inlcluding the directions
function getDirection(el) {
 var xPos, yPos, offset, dir;
 dir = "";

 xPos = window.event.offsetX;
 yPos = window.event.offsetY;

 offset = 8; //The distance from the edge in pixels

 if (yPos<offset) dir += "n";
 else if (yPos > el.offsetHeight-offset) dir += "s";
 if (xPos<offset) dir += "w";
 else if (xPos > el.offsetWidth-offset) dir += "e";

 return dir;
}

function doDown() {
 var el = getReal(event.srcElement, "className", "resizeMe");

 if (el == null) {
  theobject = null;
  return;
 }  

 dir = getDirection(el);
 if (dir == "") return;

 theobject = new resizeObject();

 theobject.el = el;
 theobject.dir = dir;

 theobject.grabx = window.event.clientX;
 theobject.graby = window.event.clientY;
 theobject.width = el.offsetWidth;
 theobject.height = el.offsetHeight;
 theobject.left = el.offsetLeft;
 theobject.top = el.offsetTop;

 window.event.returnValue = false;
 window.event.cancelBubble = true;
}

function doUp() {
 if (theobject != null) {
  theobject = null;
 }
}

function doMove() {
 var el, xPos, yPos, str, xMin, yMin;
 xMin = 8; //The smallest width possible
 yMin = 8; //             height

 el = getReal(event.srcElement, "className", "resizeMe");

 if (el.className == "resizeMe") {
  str = getDirection(el);
 //Fix the cursor
  if (str == "") str = "default";
  else str += "-resize";
  el.style.cursor = str;
 }

//Dragging starts here
 if(theobject != null) {
  if (dir.indexOf("e") != -1)
   theobject.el.style.width = Math.max(xMin, theobject.width + window.event.clientX - theobject.grabx) + "px";

  if (dir.indexOf("s") != -1)
   theobject.el.style.height = Math.max(yMin, theobject.height + window.event.clientY - theobject.graby) + "px";

  if (dir.indexOf("w") != -1) {
   theobject.el.style.left = Math.min(theobject.left + window.event.clientX - theobject.grabx, theobject.left + theobject.width - xMin) + "px";
   theobject.el.style.width = Math.max(xMin, theobject.width - window.event.clientX + theobject.grabx) + "px";
  }
  if (dir.indexOf("n") != -1) {
   theobject.el.style.top = Math.min(theobject.top + window.event.clientY - theobject.graby, theobject.top + theobject.height - yMin) + "px";
   theobject.el.style.height = Math.max(yMin, theobject.height - window.event.clientY + theobject.graby) + "px";
  }

  window.event.returnValue = false;
  window.event.cancelBubble = true;
 }
}

function getReal(el, type, value) {
 temp = el;
 while ((temp != null) && (temp.tagName != "BODY")) {
  if (eval("temp." + type) == value) {
   el = temp;
   return el;
  }
  temp = temp.parentElement;
 }
 return el;
}

document.onmousedown = doDown;
document.onmouseup   = doUp;
document.onmousemove = doMove;
</SCRIPT>

<meta content="Microsoft FrontPage 4.0" name="GENERATOR">
<meta name="ProgId" content="FrontPage.Editor.Document">
</head>

<body>

<div class="resizeMe" id="testDiv">
  <div id="innerNice">
    <p align="center"> </p>
    <p align="center">
请在边框处拖动鼠标
    <p> </p>
    <p> </p>
    <p> </p>
  </div>
</div>
</body>
</html> 
时间: 2024-08-29 07:46:45

拖动鼠标改变div层的大小宽度的相关文章

鼠标拖动改变层的大小

<html><head><title>拖动改变层的大小</title><meta content="text/html; charset=gb2312" http-equiv="Content-Type"><style> {box-sizing: border-box; moz-box-sizing: border-box}#testDiv { background-color: buttonf

鼠标拖动改变DIV等网页元素的大小的最佳实践

1.初次实现 1.1 html代码 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>div change

手动--拖动条--改变字体大小

一个多月的加班,终于熬到头了,领导说下午休息一下,我就把自己工作中途写的一个简短的小demo拿出来,说是分享一下,其实还是有一些小bug,希望有人能找出,并给指错,一起进步,一起成长,想想这一个多月,过的又充实,又慌乱,希望一会会振作一些把,其他的不多说了,代码如下,自己实现效果! 实现思想: 三个div,当鼠标拖动小按钮的时候,计算出左边的div class='scroll' 的宽度不断的变化,而 设置的字体最大为50px, 拖动条的宽度为200px, 由此可见,当拖动的宽度为100px的时候

Qt:无标题栏无边框程序的拖动和改变大小

From: http://blog.csdn.net/kfbyj/article/details/9284923 最近做项目遇到的问题,总结下. 有时候我们觉得系统的标题栏和按钮太丑太呆板,想做自己的标题栏以及最大化.最小化.关闭,菜单按钮,我们就需要 [cpp] view plaincopyprint? setWindowFlags(Qt::FramelessWindowHint); 但是这样过后,我们就不能拖动窗口改变位置,以及拖动边缘改变窗口大小了. 有两种方案处理这种情况: 1.自己对m

Js无刷新添加新层,拖动DIV层可互换位置的JavaScript实现

<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>Js无刷新添加新层</title><style>body {margin:0px;padding:0px;font-size:12px;text-align:center;}body > div {text-ali

Qt 无标题无边框程序的拖动和改变大小

2013-7-9阅读5435 评论16 最近做项目遇到的问题,总结下. 有时候我们觉得系统的标题栏和按钮太丑太呆板,想做自己的标题栏以及最大化.最小化.关闭,菜单按钮,我们就需要 setWindowFlags(Qt::FramelessWindowHint); 但是这样过后,我们就不能拖动窗口改变位置,以及拖动边缘改变窗口大小了. 有两种方案处理这种情况: 1.自己对mouseMoveEvent,mousePressEvent,mouseReleaseEvent 等事件进行处理. 2.Qt可以处

问题:如何在固定大小的DIV层插入N多个图片

这是贴友问的一个问题,具体需求是: 如何在固定大小的DIV层插入N多个图片,使其一行排列,超出层宽时出现滑动条? 原以为利用overflow属性可以实现,但是测试失败.后来利用div层叠实现了效果. HTML代码: 1: <!-- 如何在固定大小的DIV层插入N多个图片,使其一行排列,超出层宽时出现滑动条? --> 2: <!DOCTYPE html> 3: <html> 4: <head> 5: <meta http-equiv="cont

【转】C# 控件的自定义拖动、改变大小方法

在用VS的窗体设计器时,我们可以发现控件都是可以拖动的,并且还可以调整大小.怎么在自己的程序中可以使用上述功能呢? 下面的方法值得借鉴! using System; using System.Windows.Forms; using System.Drawing; namespace ControlSizeChangeEx { /// <summary> /// This class implements sizing and moving functions for /// runtime

函数传参,改变Div任意属性的值&amp;&amp;图片列表:鼠标移入/移出改变图片透明度

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-