如何设置div的固定宽度和高度
2个回答
2016-12-20
展开全部
直接上个实例代码吧:
<!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>
2016-12-20 · 知道合伙人互联网行家
关注
展开全部
css的border属性可以设置边框的粗细、样式和颜色,而边框的宽度和高度取决于div的宽度和高度,例如
div{width:200px; height:100px; border: 1px solid #ff0000;}
上面css代码设置div宽度200px,高度100px,边框为1px宽的红色实线
div{width:200px; height:100px; border: 1px solid #ff0000;}
上面css代码设置div宽度200px,高度100px,边框为1px宽的红色实线
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询