整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

HTML框架,将当前页面分为三个板块

HTML框架,将当前页面分为三个板块


lt;!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

</head>

<frameset rows="10%,*">

<frame src="upper/one.php" name="logo">

<frameset cols="20%,*">

<frame src="upperleft/02.html" name="shuming" target="neirong">

<frame src="book/threecountries/01.html" name="neirong">

</frameset>


</frameset>

</html>

以上是html的框架,可以对页面进行分割,分为上下[上部占10%,下半部分分为左右两部分,左边20%,右边80%的宽度],也就是将整个页面分为三个板块,上面是网站信息和登录的信息,下面左半部分是选择菜单栏,右边是显示的部分,手机和电脑通用的.也可以根据自己的需要进行分类.

几天发了几个视频,视频标题跟本文章标题一样,带大家用五种方式实现了一道有关布局的面试题。现特此附上源码。如果有想看整个内容介绍的,可以根据本标题或选择关注本人,或搜索标题内容,选择视频也可以看到更详细内容。

面试题的要求如下:

假设高度已知,请写出三栏布局,其中左栏,右栏各为300px,中间自适应,要求用尽可能多的方式实现这个布局。

在此我用五种方式实现了这个题目的要求,我在视频里面详情介绍了。下面贴出整个实现源码。

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>五种方式实现布局</title>

<style>

*{

padding:0;

margin:0

}

.layout div{

height:100px;

}

.layout{

margin-bottom:20px;

}

</style>

</head>

<body>

<!-- 第一种布局 浮动布局 -->

<section class="layout float">

<style>

.float .left{

float:left;

width:300px;

background-color: red;

}

.float .right{

float:right;

width:300px;

background-color: green;

}

.float .center{

background-color:yellow;

}

</style>

<article class="left-right-center">

<div class="left"></div>

<div class="right"></div>

<div class="center">

<h1>这是浮动布局</h1>

<p>这是浮动布局</p>

</div>

</article>

</section>

<!-- 第二种布局 绝对定位 父相子绝-->

<section class="layout absolute">

<style>

.absolute .left-center-right{

position: relative;

}

.absolute .left-center-right >div{

position: absolute;

}

.absolute .left{

width:300px;

left:0;

background-color: red;

}

.absolute .center{

left:300px;

right:300px;

background-color: yellow;

}

.absolute .right{

right:0;

width:300px;

background-color: green;

}

</style>

<article class="left-center-right">

<div class="left"></div>

<div class="center">

<h1>这绝对定位布局</h1>

<p>这绝对定位布局</p>

</div>

<div class="right"></div>

</article>

</section>


<!-- 第三种布局 flexBox布局 -->

<section class="layout flex">

<style>

.flex{ margin-top:140px; }

.flex .left-center-right{ display: flex; }

.flex .left{ width:300px; background-color: red; }

.flex .center{ flex:1; background-color: yellow; }

.flex .right{ width:300px; background-color: green; }

</style>

<article class='left-center-right'>

<div class="left"></div>

<div class="center">

<h1>这flexBox布局</h1>

<p>这flexBox布局</p>

</div>

<div class="right"></div>

</article>

</section>
<!-- 这是第四种布局解决方案 表格 布局-->

<section class="layout table">

<style>

.table .left-center-right{ display: table; width:100%; height:100px; }
.table .left-center-right>div{ display: table-cell; }
.table .left{ width:300px; background-color: red; }

.table .center{ width:auto; background-color: yellow; }

.table .right{ width:300px; background-color: green; }

</style>

<article class="left-center-right">

<div class="left"></div>

<div class="center">

<h1>这是表格布局解决方案</h1>

<p>这是表格布局解决方案</p>

</div>

<div class="right"></div>

</article>

</section>
<!-- 第五种解决方案 网格布局 -->

<section class="layout grid">

<style>

.grid .left-center-right{

display: grid;

grid-template-rows: 100px;

grid-template-columns: 300px auto 300px;

}
.grid .left{ background-color: red; }

.grid .center{ background-color: yellow; }

.grid .right{ background-color: green; }

</style>

<article class="left-center-right">

<div class="left"></div>

<div class="center">

<h1>这是网格布局解决方案</h1>

<p>这是网格布局解决方案</p>

</div>

<div class="right"></div>

</article>

</section>

</body>

</html>


附上最终效果图

5自适应是现在主流的技术,导航栏菜单是最常见的一种,今天我们一起来学习一下它是如何使用HTML,CSS和JavaScript来构建响应式导航栏和汉堡菜单的。

这就是它的样子,是不是很熟悉呢?

H5导航菜单

好的,那就先从HTML开始:

<header class=”header”>
<nav class=”navbar”>
<a href=”#” class=”nav-logo”>WebDev.</a>
<ul class=”nav-menu”>
<li class=”nav-item”>
<a href=”#” class=”nav-link”>Services</a>
</li>
<li class=”nav-item”>
<a href=”#” class=”nav-link”>Blog</a>
</li>
<li class=”nav-item”>
<a href=”#” class=”nav-link”>About</a>
</li>
<li class=”nav-item”>
<a href=”#” class=”nav-link”>Contact</a>
</li>
</ul>
<div class=”hamburger”>
<span class=”bar”></span>
<span class=”bar”></span>
<span class=”bar”></span>
</div>
</nav>
</header>

通过这些代码,我们实现了:

  • 给header标签指定名为header的CSS类的,可以作为导航菜单的容器。
  • 给nav标签指定名为navbar的CSS类。
  • 具有nav-logo类的链接
  • 具有nav-menu类的ul
  • 在ul内部,我们有4个具有nav-item类的li
  • 在每个nav-item中,都有一个包含nav-link类的链接
  • 至于汉堡菜单,我已在代码中添加了一个具有hamburger类的div,且此div中有3个带bar类的span

以上就是我们需要的HTML代码。

现在让我们添加CSS样式重置代码

(此外,我们将导入所需的字体,并添加一些基本的CSS来重置所有的默认样式。)

@import url(‘https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,500;1,400&display=swap’);

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

html {
font-size: 62.5%;
font-family: ‘Roboto’, sans-serif;
}

li {
list-style: none;
}

a {
text-decoration: none;
}

现在让我们给每个元素逐个添加样式:

header和navbar:

.header{
border-bottom: 1px solid #E2E8F0;
}

.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem 1.5rem;
}


hamburger样式:

.hamburger {
display: none;
}

.bar {
display: block;
width: 25px;
height: 3px;
margin: 5px auto;
-webkit-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
background-color: #101010;
}


其他元素的基本样式:

.nav-menu {
display: flex;
justify-content: space-between;
align-items: center;
}

.nav-item {
margin-left: 5rem;
}

.nav-link{
font-size: 1.6rem;
font-weight: 400;
color: #475569;
}

.nav-link:hover{
color: #482ff7;
}

.nav-logo {
font-size: 2.1rem;
font-weight: 500;
color: #482ff7;
}

完成这些之后,看起来应该是这样的:

但是它不是响应式的,所以我们还需要添加媒体查询css代码。

@media only screen and (max-width: 768px) {
.nav-menu {
position: fixed;
left: -100%;
top: 5rem;
flex-direction: column;
background-color: #fff;
width: 100%;
border-radius: 10px;
text-align: center;
transition: 0.3s;
box-shadow:
0 10px 27px rgba(0, 0, 0, 0.05);
}

.nav-menu.active {
left: 0;
}

.nav-item {
margin: 2.5rem 0;
}

.hamburger {
display: block;
cursor: pointer;
}

}


这里媒体查询就是通过设置position: fixed; left: -100%;来隐藏nav-menu。

此外,我们将hamburger设置为display: block;,所以现在可见。

我们还添加了一个额外的类.nav-menu.active,它在nav-menu上设置left: 0;。现在,到了添加javascript的时候了,以便在我们单击汉堡菜单时,会在nav-menu上添加此active类。

添加JavaScript

const hamburger=document.querySelector(“.hamburger”);
const navMenu=document.querySelector(“.nav-menu”);

hamburger.addEventListener(“click”, mobileMenu);

function mobileMenu() {
hamburger.classList.toggle(“active”);
navMenu.classList.toggle(“active”);
}


此处的函数mobileMenu()在hamburger和nav-menu上也添加了一个active类,从而打开mobile menu。单击hamburger时,我们可以使用hamburger上的active类来创建X动画。现在就开始做吧。

// Inside the Media Query.

.hamburger.active .bar:nth-child(2) {
opacity: 0;
}

.hamburger.active .bar:nth-child(1) {
transform: translateY(8px) rotate(45deg);
}

.hamburger.active .bar:nth-child(3) {
transform: translateY(-8px) rotate(-45deg);
}

现在看起来应该是这样的:

但是有一个问题,当我们单击链接时,汉堡菜单不会关闭。现在让我们解决一下这个问题。

const navLink=document.querySelectorAll(“.nav-link”);

navLink.forEach(n=> n.addEventListener(“click”, closeMenu));

function closeMenu() {
hamburger.classList.remove(“active”);
navMenu.classList.remove(“active”);
}

closeMenu()函数从nav-menu和hamburger中删除active类,从而关闭mobile menu。

H5导航菜单就是这样,实现了用HTML,CSS和javascript构建一个响应式的导航栏菜单。希望你喜欢并分享这篇文章。感谢大家的阅读。

更多干货等着你~ 点赞、分享,关注哦