如何设置
的固定宽度和高度

goshowk 2005-10-24 12:40:24
如何设置<div>的固定宽度和高度,使得无论<div>和</div>内部放入多大的内容或图片都不会超过设置的宽度和高度???
...全文
3856 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fantiny 2005-10-24
  • 打赏
  • 举报
回复
里面的setFitScreen()看懂你就知道怎么设置了。
fantiny 2005-10-24
  • 打赏
  • 举报
回复
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Document </TITLE>
<style type="text/css">
body{
margin: 0px;
background-color: #999999 ;
}
.DivMain {
position : absolute;
text-align : center ;
overflow : hidden;
}
.ImgMain {
position : absolute;
overflow : hidden;
}
.TBMain {
background-color: #CCCCCC ;
padding: 0px;
}
</style>

<SCRIPT LANGUAGE="JavaScript">
<!--

var originalWidth;
var originalHeight;
var ObjDrag;

function setInit(objImg) {

originalWidth = objImg.width;
originalHeight = objImg.height;
//initImg(objImg);
fmImgZoom.Original.click();
}

function initImg(objCurrent) {

var intBodyWidth ;
var intBodyHeight ;
var objTbMain ;
var objDivMain ;
//var objImgMain ;

intBodyWidth = document.body.clientWidth ;
intBodyHeight = document.body.clientHeight ;

objTbMain = document.getElementById("tbMain") ;
objDivMain = document.getElementById("divMain") ;
//objImgMain = document.getElementById("imgMain") ;

objTbMain.top = 0;
objTbMain.height = 20;
objTbMain.width = intBodyWidth;

objDivMain.top = 20 ;
objDivMain.left = 0 ;
objDivMain.height = intBodyHeight - 20 ;
objDivMain.width = intBodyWidth ;

initZoom(objCurrent);
}

function initZoom(obj){

var intObjWidth ;
var intObjHeight ;
var intDivHeight ;
var intZoomRatio ;
var objDivMain ;
var objTbMain;

objDivMain = document.getElementById("DivMain");
objTbMain = document.getElementById("tbMain") ;

intDivHeight = objDivMain.height;
intObjHeight = obj.height;
intZoomRatio = intDivHeight / intObjHeight;

obj.style.zoom = intZoomRatio ;
obj.style.top = 0;
obj.style.left = 0;

fnWritePos (obj.style.left,obj.style.top,obj.style.zoom) ;
}

function setFitScreen() {

var objImg;

objImg = document.getElementById("imgMain") ;
initImg(objImg);
}

function setOriginal() {

var objImg;
var objDivMain ;
var intBodyWidth ;
var intBodyHeight ;

intBodyWidth = document.body.clientWidth ;
intBodyHeight = document.body.clientHeight ;

objImg = document.getElementById("imgMain") ;
objDivMain = document.getElementById("divMain") ;

objImg.style.left = 0 ;
objImg.style.top = 0 ;
objImg.width = originalWidth;
objImg.height = originalHeight;
objImg.style.zoom = 1 ;

objDivMain.style.width = intBodyWidth ;
objDivMain.style.height = intBodyHeight - 20 ;

fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);
}

function fnMouseDown(obj) {
ObjDrag=obj;
ObjDrag.setCapture();
ObjDrag.l=event.x-ObjDrag.style.pixelLeft;
ObjDrag.t=event.y-ObjDrag.style.pixelTop;
}

function fnMouseMove() {
if(ObjDrag!=null) {
ObjDrag.style.left = event.x-ObjDrag.l;
ObjDrag.style.top = event.y-ObjDrag.t;
fnWritePos(ObjDrag.style.left,ObjDrag.style.top,ObjDrag.style.zoom);
}
}

function fnMouseUp() {
if(ObjDrag!=null) {
ObjDrag.releaseCapture();
ObjDrag=null;
}
}

function fnWritePos(intLeft,intTop,intZoom) {
var objbtnImgInfo;
objbtnImgInfo = document.getElementById("btnImgInfo") ;
objbtnImgInfo.innerText = " ImgLeft:" + intLeft + " ImgTop:" + intTop + " ImgZoom:" + intZoom ;
}

function fnZoomIn() {

var objImg;

objImg = document.getElementById("imgMain") ;
objImg.style.zoom = parseFloat(objImg.style.zoom) + 0.01 ;
fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);
}

function fnZoomOut() {

var objImg;

objImg = document.getElementById("imgMain") ;
objImg.style.zoom = parseFloat(objImg.style.zoom) - 0.01 ;
fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);
}

function fnContextMenu() {
window.event.returnValue=false;
}

function fnDivMouseDown() {
if ( event.button == 2 && event.srcElement.id != "imgMain") {
setOriginal();
}
}

function setDisplay() {

var objImg;

objImg = document.getElementById("imgMain") ;

if(objImg.style.visibility == ""){
objImg.style.visibility = "hidden";
}
else{
objImg.style.visibility = "";
}
}

//-->
</SCRIPT>
</HEAD>

<BODY oncontextmenu="fnContextMenu()">
<form name="fmImgZoom">
<TABLE id="tbMain" class="TBMain">
<TR>
<TD height="20">
<input type="button" name="ZoomIn" value="ZoomIn" onclick="fnZoomIn()">
<input type="button" name="ZoomOut" value="ZoomOut" onclick="fnZoomOut()">
<input type="button" name="Original" value="Original" onclick="setOriginal()">
<input type="button" name="FitScreen" value="FitScreen" onclick="setFitScreen()">
<input type="button" name="FitScreen" value="hide/view" onclick="setDisplay()">
<input id="btnImgInfo" size="50">
</TD>
</TR>
</TABLE>
<div id="divMain" class="DivMain" onmousedown="fnDivMouseDown()"><img src="22.jpg" id="imgMain" class="imgMain" onload="setInit(this)" onmousedown="fnMouseDown(this)" onmousemove="fnMouseMove()" onmouseup="fnMouseUp()" ></div>
</form>
</BODY>
</HTML>
fantiny 2005-10-24
  • 打赏
  • 举报
回复
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5" />
<title>Untitled Document</title>
<STYLE TYPE="text/css">
<!--

.DivMain {
position : absolute;
text-align : center ;
overflow : hidden;
width : 100px;
height : 100px;
}
-->
</STYLE>
</head>

<body>

<div class="DivMain">
<img src="23.jpg"></div>

</body>
</html>
hsboy 2005-10-24
  • 打赏
  • 举报
回复
overflow : hidden;
不过这样div中的内容容纳不了的部分就看不见了。
fantiny 2005-10-24
  • 打赏
  • 举报
回复
.DivMain {
position : absolute;
text-align : center ;
overflow : hidden;
width : 100px;
height : 100px;
}

61,115

社区成员

发帖
与我相关
我的任务
社区描述
层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。
社区管理员
  • HTML(CSS)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧