根据网页宽度给BODY加不同的类名

<pre name="code" class="html"><!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
		.box{
			background: green;
			height: 200px;
		}
		.s1 .box{
			width: 900px;
		}
	</style>
</head>
<body>
	<script>
			(function() {
				var oBody, aWidths = [],
					sOldClass = 0,
					events = [];
				function resize(widths) {
					widths && (aWidths = widths, window.addEventListener ? window.addEventListener("resize", handle) : window.attachEvent && window.attachEvent("onresize", handle));
					var iWidth = document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth;
					var len = aWidths.length;
					if (len == 1){
						if (iWidth < aWidths[0]) setBodyClass(1);
						else if (iWidth > aWidths[len - 1]) setBodyClass(len + 1);
					}
					else{
						for (var i = 0; i < len; i++){
							if (iWidth >= aWidths[i] && iWidth < aWidths[i + 1]) {
								setBodyClass(i + 2);
								break
							}
						}
					}
				}
				function handle() {
					resize();
					for (var len = events.length, i = 0; i < len; i++){
						events[i]();
					} 
				}
				function setBodyClass(sNewClass) {
					if (sOldClass != sNewClass) {
						var oBody = document.body || document.getElementsByTagName("body");
						var sBodyClass = oBody.className,
							sBodyClass = /s[1-9]/.exec(sBodyClass) ? sBodyClass.replace(/s[1-9]/, "s" + sNewClass) : sBodyClass + (("" == sBodyClass ? "" : " ") + "s" + sNewClass);
						oBody.className = sBodyClass;
						sOldClass = sNewClass
					}
				};
				window.setPageStyle = resize;
				window.getPageStyle = function() {
					return sOldClass
				};
				window.onPageStyleChange = function(fn) {
					events.push(fn)
				};
			})();
			setPageStyle([1180]);
			onPageStyleChange(function(){
				console.log(getPageStyle());
			})

	</script>
	<div class="box"></div>
	<div class="update-box">
		<div class="hd">
			<h2 class="title">亲,你的浏览器已经OUT了!</h2>
			<h3 class="sub-title">
				浏览器版本过低,导致了网站部分功能无法正常显示,建议升级为:
			</h3>
			<i class="close"></i>
		</div>
		<div class="bd">
			<ul class="browser-list">
				<li><a href="http://go.microsoft.com/fwlink/?LinkId=324628">IE</a></li>
				<li><a href="http://www.google.cn/chrome/browser/desktop/index.html">chrome</a></li>
			</ul>
			
		</div>
	</div>
</body>
</html>




                
<think>好的,用户想用HTML和CSS创建顶部导航栏和侧边栏的布局。我需要先理清楚这两种元素的结构和布局方法。 首先,顶部导航栏通常横跨页面顶部,固定位置,可能需要使用Flexbox或者定位来实现。侧边栏一般在左侧或右侧,固定宽度,内容区域则占据剩余空间。这时候可能需要使用Flex布局或者Grid布局来安排两者的位置。 然后,我得考虑响应式设计。在移动设备上,侧边栏可能需要隐藏或者调整布局,顶部导航栏也可能需要折叠菜单。这时候媒体查询就派上用场了。比如,当屏幕宽度小于768px时,侧边栏可以隐藏,顶部导航栏的菜单项变为垂直排列。 接下来,参考用户提供的引用内容。引用[2]提到使用Flexbox实现导航链接的水平排列,并在移动端堆叠。引用[1]和[3]中的侧边栏样式,包括宽度、背景颜色、链接样式等。需要综合这些样式来设计顶部和侧边栏的结构。 结构上,页面主体可能分为顶部导航和侧边栏内容区域。使用Flex容器,让侧边栏和内容并排,顶部导航固定在顶部。要注意z-index,确保导航栏在顶部,不与其他元素重叠。 具体步骤:先写HTML结构,顶部用nav标签,侧边栏用aside,内容区用main。然后CSS部分,设置body为flex方向column,顶部固定高度,侧边栏和内容使用flex:1来填充剩余空间。侧边栏固定宽度,内容自动扩展。 响应式部分,使用媒体查询,当屏幕宽度较小时,侧边栏隐藏,或者调整布局为垂直。可能需要使用JavaScript来控制侧边栏的显示隐藏,但用户可能只需要CSS方案,所以暂时用媒体查询调整布局。 需要检查代码的正确性,比如Flex和Grid的应用是否正确,类名是否对应,样式是否冲突。比如,顶部导航的position: fixed可能需要设置top:0和left:0,宽度100%。侧边栏和内容区域可能需要设置margin-top来避开顶部导航的高度。 最后,测试不同屏幕尺寸下的显示效果,确保布局正确响应。可能还需要调整内边距、外边距,确保视觉一致性。</think>### 使用HTML和CSS创建顶部导航栏和侧边栏布局 #### 一、HTML结构设计 ```html <body> <!-- 顶部导航栏 --> <nav class="top-nav"> <div class="logo">LOGO</div> <ul class="nav-list"> <li><a href="#">首页</a></li> <li><a href="#">产品</a></li> <li><a href="#">服务</a></li> </ul> </nav> <!-- 主体内容容器 --> <div class="container"> <!-- 侧边栏 --> <aside class="sidebar"> <ul> <li><a href="#">菜单1</a></li> <li><a href="#">菜单2</a></li> <li><a href="#">菜单3</a></li> </ul> </aside> <!-- 内容区域 --> <main class="content"> <h1>主内容区</h1> <p>页面主要内容...</p> </main> </div> </body> ``` #### 二、CSS样式实现 ```css /* 基础布局 */ body { margin: 0; display: flex; flex-direction: column; min-height: 100vh; } /* 顶部导航栏样式 */ .top-nav { background: #333; color: white; padding: 15px; display: flex; justify-content: space-between; align-items: center; position: fixed; width: 100%; z-index: 1000; } .nav-list { display: flex; gap: 20px; list-style: none; margin: 0; padding: 0; } .nav-list a { color: white; text-decoration: none; } /* 侧边栏与内容容器 */ .container { display: flex; flex: 1; margin-top: 60px; /* 留出顶部导航空间 */ } .sidebar { width: 250px; background: #111; color: #fff; padding: 20px; position: fixed; height: calc(100vh - 60px); } .content { flex: 1; margin-left: 250px; /* 匹配侧边栏宽度 */ padding: 20px; } /* 响应式布局 */ @media (max-width: 768px) { .sidebar { width: 0; overflow: hidden; transition: 0.3s; } .content { margin-left: 0; } .nav-list { display: none; /* 移动端隐藏导航项 */ } } ``` #### 三、关键技术解析 1. **Flex布局**:通过`display: flex`实现顶部导航栏的水平排列和容器区的侧边栏-内容布局[^2] 2. **定位控制**:使用`position: fixed`固定顶部导航和侧边栏位置[^1] 3. **响应式设计**:通过`@media`查询实现移动端布局适配,侧边栏隐藏并调整内容区边距[^3] 4. **空间计算**:使用`calc()`函数动态计算侧边栏高度,确保与顶部导航栏无缝衔接
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值