search
HomeWeb Front-endHTML Tutorialdiv自适应宽度的问题,求高手帮忙!_html/css_WEB-ITnose


在左侧做了一个可以自动伸缩的菜单栏,想让菜单栏展开时,完全展开右面带地图的div,而不是被左面的菜单遮住一部分,该怎么解决?

这是菜单展开的图:搜索

这是菜单隐藏的图:

这是body:
    

        
 
        

             

        
    
    

这是样式表:

    #common_box{
     width:280px;
     height :100%;
     position:fixed;
     left:0;
     top:40px;
     border:1px solid #789;
     background:#fff;
     z-index:99
    }
    #cli_on{
     width:10px;
     height:100%;
     float:right;
     cursor:pointer;
     background:#ccc;
     text-align:center;
     line-height:180px;
     font-size:24px;
     color:#f00;
    }
    .mydiv
    {
        position:fixed;
        top:40px;
        left:0px;
        width: 100%;
        height:650px;
        }

这是伸缩菜单的js:
    <script> <br /> //左侧弹出菜单 <br /> window.onload = function () { <br /> var common_box = document.getElementById("common_box"); <br /> var cli_on = document.getElementById("cli_on"); <br /> var flag = true, r_len = 0, timer = null; <br /> cli_on.onclick = function () { <br /> if (flag) { <br /> r_len = 0; <br /> timer = setInterval(slideright, 10); <br /> <br /> } else { <br /> r_len = -270; <br /> timer = setInterval(slideleft, 10); <br /> } <br /> } <br /> function slideright() { <br /> if (r_len <= -270) { <br /> flag = !flag; <br /> cli_on.innerHTML = ""; <br /> clearInterval(timer); <br /> return false; <br /> <br /> } else { <br /> r_len -= 5; <br /> common_box.style.left = r_len + "px"; <br /> } <br /> } <br /> function slideleft() { <br /> <br /> if (r_len >= 0) { <br /> clearInterval(timer); <br /> flag = !flag; <br /> cli_on.innerHTML = ""; <br /> return false; <br /> } else { <br /> r_len += 5; <br /> common_box.style.left = r_len + "px"; <br /> } <br /> } <br /> <br /> } <br /> </script>
就是想改成像这个网站论坛首页左面菜单栏的那种感觉。


回复讨论(解决方案)

你这个页面的布局不对啊,你看你使用的是position:fixed。在控制显示位置的,这个很明显的,就会出现覆盖这个问题了。

这个你可以这么处理:
左侧的那块,使用定位处理,而右侧的那块,不使用定位,但是使用一个margin-left来控制左侧空出来的距离

当你左侧的更改时,同时改变右侧的那个margin-left的值就行了。

我这么说,能想明白吗?

mydiv{	margin-top:40px;	background-color:#eee;	height:650px;	margin-left:280px;}


window.onload = function () {	var common_box = document.getElementById("common_box");	var cli_on = document.getElementById("cli_on");	var mapDiv = document.getElementById("mapDiv");	//zheli	var flag = true, r_len = 0, timer = null;	cli_on.onclick = function () {		if (flag) {			r_len = 0;			timer = setInterval(slideright, 10);		} else {			r_len = -270;			timer = setInterval(slideleft, 10);		}	}	function slideright() {		if (r_len <= -270) {			flag = !flag;			cli_on.innerHTML = "";			clearInterval(timer);			return false;		} else {			r_len -= 5;			common_box.style.left = r_len + "px";			mapDiv.style.marginLeft = 270 + r_len + "px";			//zheli		}	}	function slideleft() {		if (r_len >= 0) {			clearInterval(timer);			flag = !flag;			cli_on.innerHTML = "";			return false;		} else {			r_len += 5;			common_box.style.left = r_len + "px";			mapDiv.style.marginLeft = 270 + r_len + "px";			//zheli		}	}}


在你的基础上,稍微改了一点。试试是你要的效果吗?没有测兼容性。

不过,一些效果的话,html部分的布局也是很重要的,先想好布局,之后的js和css写起来,才会更简单的。

感觉应该需要改变div的宽度。用left什么的左右移动菜单和地图,由于之前它们已经绘制完毕了,左右移动时,不是遮盖了自身的一部分(这个效果应该只是左侧菜单需要的),就是由于初始时加载的画面太小已经固定,不能动态适应宽度变化(当你向左侧移动地图时,右侧新增的部分应该不会被自动填充)。
因此,我建议应该在timer的事件中改变两侧div的宽度,同时重新填充div中的内容,使其适应宽度的变化。

<!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"><style>#common_box{     width:15%;     height :100%;     position:fixed;     left:0;     top:40px;     border:1px solid #789;     background:#fff;     z-index:99;	 float:left;	 padding:0;    }    #cli_on{     width:10px;     height:100%;     float:right;     cursor:pointer;     background:#ccc;     text-align:center;     line-height:180px;     font-size:24px;     color:#f00;    }    .mydiv    {        position:fixed;        top:40px;        right:0px;        width: 85%;        height:650px;		float: right;		padding:0;        }</style><script type="text/javascript">	//左侧弹出菜单        window.onload = function () {            var common_box = document.getElementById("common_box");            var cli_on = document.getElementById("cli_on");			var mapDiv = document.getElementById("mapDiv");            var flag = true, r_len = 0, timer = null;						var mapDiv_width = mapDiv.clientWidth;			var common_box_width = common_box.clientWidth;			var body_client_width = mapDiv.clientWidth + common_box.clientWidth;			            cli_on.onclick = function () {                if (flag) {                    r_len = common_box_width;                    timer = setInterval(slideright, 10);                } else {                    r_len = 0;                    timer = setInterval(slideleft, 10);                }            }            function slideright() {                if (r_len <= 10) {                    flag = !flag;					common_box.style.width = 10 + "px";					mapDiv.style.width = body_client_width - common_box.clientWidth + "px";					cli_on.innerHTML = "菜单栏";					mapDiv.innerHTML = "地图";                    clearInterval(timer);                    return false;                } else {                    r_len -= 5;                    common_box.style.width = r_len + "px";					mapDiv.style.width = body_client_width - common_box.clientWidth + "px";					mapDiv.innerHTML = "地图";                }            }            function slideleft() {                if (r_len >= common_box_width) {                    clearInterval(timer);                    flag = !flag;					common_box.style.width = common_box.clientWidth + "px";					mapDiv.style.width = mapDiv_width + "px";                    cli_on.innerHTML = "菜单栏";					mapDiv.innerHTML = "地图";                    return false;                } else {                    r_len += 5;                    common_box.style.width = r_len + "px";					mapDiv.style.width = body_client_width - common_box.clientWidth + "px";					mapDiv.innerHTML = "地图";                }            }        }</script><body>	<div>		<div id="common_box" style="background-color:yellow; overflow: hidden;">             <div id="cli_on">菜单栏</div>        </div>    		<div id="mapDiv" class ="mydiv" style="background-color:green;">地图</div>     </div></body></html>

感觉应该需要改变div的宽度。用left什么的左右移动菜单和地图,由于之前它们已经绘制完毕了,左右移动时,不是遮盖了自身的一部分(这个效果应该只是左侧菜单需要的),就是由于初始时加载的画面太小已经固定,不能动态适应宽度变化(当你向左侧移动地图时,右侧新增的部分应该不会被自动填充)。
因此,我建议应该在timer的事件中改变两侧div的宽度,同时重新填充div中的内容,使其适应宽度的变化。

<!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"><style>#common_box{     width:15%;     height :100%;     position:fixed;     left:0;     top:40px;     border:1px solid #789;     background:#fff;     z-index:99;	 float:left;	 padding:0;    }    #cli_on{     width:10px;     height:100%;     float:right;     cursor:pointer;     background:#ccc;     text-align:center;     line-height:180px;     font-size:24px;     color:#f00;    }    .mydiv    {        position:fixed;        top:40px;        right:0px;        width: 85%;        height:650px;		float: right;		padding:0;        }</style><script type="text/javascript">	//左侧弹出菜单        window.onload = function () {            var common_box = document.getElementById("common_box");            var cli_on = document.getElementById("cli_on");			var mapDiv = document.getElementById("mapDiv");            var flag = true, r_len = 0, timer = null;						var mapDiv_width = mapDiv.clientWidth;			var common_box_width = common_box.clientWidth;			var body_client_width = mapDiv.clientWidth + common_box.clientWidth;			            cli_on.onclick = function () {                if (flag) {                    r_len = common_box_width;                    timer = setInterval(slideright, 10);                } else {                    r_len = 0;                    timer = setInterval(slideleft, 10);                }            }            function slideright() {                if (r_len <= 10) {                    flag = !flag;					common_box.style.width = 10 + "px";					mapDiv.style.width = body_client_width - common_box.clientWidth + "px";					cli_on.innerHTML = "菜单栏";					mapDiv.innerHTML = "地图";                    clearInterval(timer);                    return false;                } else {                    r_len -= 5;                    common_box.style.width = r_len + "px";					mapDiv.style.width = body_client_width - common_box.clientWidth + "px";					mapDiv.innerHTML = "地图";                }            }            function slideleft() {                if (r_len >= common_box_width) {                    clearInterval(timer);                    flag = !flag;					common_box.style.width = common_box.clientWidth + "px";					mapDiv.style.width = mapDiv_width + "px";                    cli_on.innerHTML = "菜单栏";					mapDiv.innerHTML = "地图";                    return false;                } else {                    r_len += 5;                    common_box.style.width = r_len + "px";					mapDiv.style.width = body_client_width - common_box.clientWidth + "px";					mapDiv.innerHTML = "地图";                }            }        }</script><body>	<div>		<div id="common_box" style="background-color:yellow; overflow: hidden;">             <div id="cli_on">菜单栏</div>        </div>    		<div id="mapDiv" class ="mydiv" style="background-color:green;">地图</div>     </div></body></html>


嗯,按照你的帮助修改运行了一下代码,果真左面的菜单栏不会覆盖右面的地图了,但在菜单栏隐藏时,右面的地图会在右面留出15%的空白,没法被自动填充


感觉应该需要改变div的宽度。用left什么的左右移动菜单和地图,由于之前它们已经绘制完毕了,左右移动时,不是遮盖了自身的一部分(这个效果应该只是左侧菜单需要的),就是由于初始时加载的画面太小已经固定,不能动态适应宽度变化(当你向左侧移动地图时,右侧新增的部分应该不会被自动填充)。
因此,我建议应该在timer的事件中改变两侧div的宽度,同时重新填充div中的内容,使其适应宽度的变化。

<!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"><style>#common_box{     width:15%;     height :100%;     position:fixed;     left:0;     top:40px;     border:1px solid #789;     background:#fff;     z-index:99;	 float:left;	 padding:0;    }    #cli_on{     width:10px;     height:100%;     float:right;     cursor:pointer;     background:#ccc;     text-align:center;     line-height:180px;     font-size:24px;     color:#f00;    }    .mydiv    {        position:fixed;        top:40px;        right:0px;        width: 85%;        height:650px;		float: right;		padding:0;        }</style><script type="text/javascript">	//左侧弹出菜单        window.onload = function () {            var common_box = document.getElementById("common_box");            var cli_on = document.getElementById("cli_on");			var mapDiv = document.getElementById("mapDiv");            var flag = true, r_len = 0, timer = null;						var mapDiv_width = mapDiv.clientWidth;			var common_box_width = common_box.clientWidth;			var body_client_width = mapDiv.clientWidth + common_box.clientWidth;			            cli_on.onclick = function () {                if (flag) {                    r_len = common_box_width;                    timer = setInterval(slideright, 10);                } else {                    r_len = 0;                    timer = setInterval(slideleft, 10);                }            }            function slideright() {                if (r_len <= 10) {                    flag = !flag;					common_box.style.width = 10 + "px";					mapDiv.style.width = body_client_width - common_box.clientWidth + "px";					cli_on.innerHTML = "菜单栏";					mapDiv.innerHTML = "地图";                    clearInterval(timer);                    return false;                } else {                    r_len -= 5;                    common_box.style.width = r_len + "px";					mapDiv.style.width = body_client_width - common_box.clientWidth + "px";					mapDiv.innerHTML = "地图";                }            }            function slideleft() {                if (r_len >= common_box_width) {                    clearInterval(timer);                    flag = !flag;					common_box.style.width = common_box.clientWidth + "px";					mapDiv.style.width = mapDiv_width + "px";                    cli_on.innerHTML = "菜单栏";					mapDiv.innerHTML = "地图";                    return false;                } else {                    r_len += 5;                    common_box.style.width = r_len + "px";					mapDiv.style.width = body_client_width - common_box.clientWidth + "px";					mapDiv.innerHTML = "地图";                }            }        }</script><body>	<div>		<div id="common_box" style="background-color:yellow; overflow: hidden;">             <div id="cli_on">菜单栏</div>        </div>    		<div id="mapDiv" class ="mydiv" style="background-color:green;">地图</div>     </div></body></html>


嗯,按照你的帮助修改运行了一下代码,果真左面的菜单栏不会覆盖右面的地图了,但在菜单栏隐藏时,右面的地图会在右面留出15%的空白,没法被自动填充
我写的效果就是需要手动重绘地图的,你需要将
mapDiv.innerHTML = "地图";
这些地方替换成地图的加载代码,这样地图才能自动适应画面变化。

mydiv{	margin-top:40px;	background-color:#eee;	height:650px;	margin-left:280px;}


window.onload = function () {	var common_box = document.getElementById("common_box");	var cli_on = document.getElementById("cli_on");	var mapDiv = document.getElementById("mapDiv");	//zheli	var flag = true, r_len = 0, timer = null;	cli_on.onclick = function () {		if (flag) {			r_len = 0;			timer = setInterval(slideright, 10);		} else {			r_len = -270;			timer = setInterval(slideleft, 10);		}	}	function slideright() {		if (r_len <= -270) {			flag = !flag;			cli_on.innerHTML = "";			clearInterval(timer);			return false;		} else {			r_len -= 5;			common_box.style.left = r_len + "px";			mapDiv.style.marginLeft = 270 + r_len + "px";			//zheli		}	}	function slideleft() {		if (r_len >= 0) {			clearInterval(timer);			flag = !flag;			cli_on.innerHTML = "";			return false;		} else {			r_len += 5;			common_box.style.left = r_len + "px";			mapDiv.style.marginLeft = 270 + r_len + "px";			//zheli		}	}}


在你的基础上,稍微改了一点。试试是你要的效果吗?没有测兼容性。

不过,一些效果的话,html部分的布局也是很重要的,先想好布局,之后的js和css写起来,才会更简单的。


试了一下,效果实现了!是不是用position:fixed;直接固定位置挺不好的?



感觉应该需要改变div的宽度。用left什么的左右移动菜单和地图,由于之前它们已经绘制完毕了,左右移动时,不是遮盖了自身的一部分(这个效果应该只是左侧菜单需要的),就是由于初始时加载的画面太小已经固定,不能动态适应宽度变化(当你向左侧移动地图时,右侧新增的部分应该不会被自动填充)。
因此,我建议应该在timer的事件中改变两侧div的宽度,同时重新填充div中的内容,使其适应宽度的变化。

<!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"><style>#common_box{     width:15%;     height :100%;     position:fixed;     left:0;     top:40px;     border:1px solid #789;     background:#fff;     z-index:99;	 float:left;	 padding:0;    }    #cli_on{     width:10px;     height:100%;     float:right;     cursor:pointer;     background:#ccc;     text-align:center;     line-height:180px;     font-size:24px;     color:#f00;    }    .mydiv    {        position:fixed;        top:40px;        right:0px;        width: 85%;        height:650px;		float: right;		padding:0;        }</style><script type="text/javascript">	//左侧弹出菜单        window.onload = function () {            var common_box = document.getElementById("common_box");            var cli_on = document.getElementById("cli_on");			var mapDiv = document.getElementById("mapDiv");            var flag = true, r_len = 0, timer = null;						var mapDiv_width = mapDiv.clientWidth;			var common_box_width = common_box.clientWidth;			var body_client_width = mapDiv.clientWidth + common_box.clientWidth;			            cli_on.onclick = function () {                if (flag) {                    r_len = common_box_width;                    timer = setInterval(slideright, 10);                } else {                    r_len = 0;                    timer = setInterval(slideleft, 10);                }            }            function slideright() {                if (r_len <= 10) {                    flag = !flag;					common_box.style.width = 10 + "px";					mapDiv.style.width = body_client_width - common_box.clientWidth + "px";					cli_on.innerHTML = "菜单栏";					mapDiv.innerHTML = "地图";                    clearInterval(timer);                    return false;                } else {                    r_len -= 5;                    common_box.style.width = r_len + "px";					mapDiv.style.width = body_client_width - common_box.clientWidth + "px";					mapDiv.innerHTML = "地图";                }            }            function slideleft() {                if (r_len >= common_box_width) {                    clearInterval(timer);                    flag = !flag;					common_box.style.width = common_box.clientWidth + "px";					mapDiv.style.width = mapDiv_width + "px";                    cli_on.innerHTML = "菜单栏";					mapDiv.innerHTML = "地图";                    return false;                } else {                    r_len += 5;                    common_box.style.width = r_len + "px";					mapDiv.style.width = body_client_width - common_box.clientWidth + "px";					mapDiv.innerHTML = "地图";                }            }        }</script><body>	<div>		<div id="common_box" style="background-color:yellow; overflow: hidden;">             <div id="cli_on">菜单栏</div>        </div>    		<div id="mapDiv" class ="mydiv" style="background-color:green;">地图</div>     </div></body></html>


嗯,按照你的帮助修改运行了一下代码,果真左面的菜单栏不会覆盖右面的地图了,但在菜单栏隐藏时,右面的地图会在右面留出15%的空白,没法被自动填充
我写的效果就是需要手动重绘地图的,你需要将
mapDiv.innerHTML = "地图";
这些地方替换成地图的加载代码,这样地图才能自动适应画面变化。
地图这个只在mapDiv里加载,想要完全加载只能靠改变div的宽度来实现了

定位浮动什么的,我会觉得,能少用,就尽量少用,如果用的太多,以后添加功能,更改样式时,都会变得更难。

当然,有时候也不得不用,只要不是把所有的布局,都用float,position这样就好了。

也算是个人习惯。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.