search
HomeWeb Front-endHTML TutorialCSS3单页面垂直固定导航_html/css_WEB-ITnose

越来越多的开发者喜欢将单页面尤其像产品介绍页面设计成用一个简单的导航条关联的页面各个重要部分(节点)。本例中,一个页面由多个重要节点组成,在页面一侧会垂直展示多个简单的导航远点,滑上圆点会展示对应节点部分的名称,点击页面会滚动到对应的节点部分,而导航条位置固定不变。

查看演示 下载源码

当屏幕足够小时(如手机移动设备),会在屏幕右下角展示一个图标,触控图标,则会展开导航菜单,点击菜单页面会滚动到对应的节点,效果非常酷。

HTML

我们的导航条是一个无序列表ul,包含在nav.cd-vertical-nav内,通过连接a#section1关联到对应的节点。元素button.cd-nav-trigger是用来作为小屏幕设备上的用来触发菜单的按钮。section.cd-section就是用来对应导航节点的内容。

<nav class="cd-vertical-nav">	<ul>		<li><a href="#section1" class="active"><span class="label">Intro</span></a></li>		<li><a href="#section2"><span class="label">Events</span></a></li>		<!-- additional navigation items here -->	</ul></nav> <button class="cd-nav-trigger cd-image-replace">Open navigation<span aria-hidden="true"></span></button> <section id="section1" class="cd-section">	<div class="content-wrapper">		<h1 id="垂直固定导航">垂直固定导航</h1>		<a href="#section2" class="cd-scroll-down cd-image-replace">scroll down</a>	</div></section> <section id="section2" class="cd-section">	<div class="content-wrapper">		<!-- section content here -->	</div></section>

CSS

HTML结构部署好后,我们要为页面添加必要的CSS样式。在非常小的屏幕下(视图宽度小于800px),我们将.cd-nav-trigger和

.cd-nav-trigger {  display: block;  position: fixed;  z-index: 2;  bottom: 30px;  right: 5%;} .cd-vertical-nav {  position: fixed;  z-index: 1;  right: 5%;  bottom: 30px;  transform: scale(0);  transform-origin: right bottom;  transition: transform 0.2s;}.cd-vertical-nav.open {  transform: scale(1);}

那么在大的屏幕设备上,我们首先是要 Modernizr 来检测当前使用的设备是否支持触屏,在非触控屏上会有鼠标滑上的效果。我们将右侧的垂直导航条设置为固定的高度和宽度,并将其固定在页面右侧。默认右侧导航之显示几个圆点,当鼠标滑上圆点时,导航条会打开,可以选择导航菜单。

@media only screen and (min-width: 800px) {  .cd-vertical-nav {    right: 0;    top: 0;    height: 100vh;    width: 90px;  }  .cd-vertical-nav::before {    /* this is the navigation background */    content: '';    position: absolute;    top: 0;    left: 0;    width: 100%;    height: 100%;    background: rgba(0, 0, 0, 0.8);    transform: translateX(100%);    transition: transform 0.4s;  }  .no-touch .cd-vertical-nav:hover::before,   .touch .cd-vertical-nav::before {    transform: translateX(0);  }  .cd-vertical-nav .label {    display: block;    transform: translateX(100%);    transition: transform 0.4s;  }  .no-touch .cd-vertical-nav:hover .label,   .touch .cd-vertical-nav .label {    transform: translateX(0);  }}

为了创建圆点和图标,可以使用::after和::before来为a元素添加伪类。

@media only screen and (min-width: 800px) {  .cd-vertical-nav a {    position: relative;    padding: 3em 0 0;    margin: 1.4em auto;  }  .cd-vertical-nav a::before,   .cd-vertical-nav a::after {    /* used to create the filled circle and the background icon */    content: '';    position: absolute;    left: 50%;    transition: transform 0.4s 0s;  }  .cd-vertical-nav a::before {    /* filled circle */    top: 0;    height: 32px;    width: 32px;    border-radius: 50%;    background: #eaf2e3;    transform: translateX(-50%) scale(0.25);  }  .cd-vertical-nav a::after {    /* icon */    top: 8px;    height: 16px;    width: 16px;    background: url(../img/cd-nav-icons.svg) no-repeat;    transform: translateX(-50%) scale(0);  }  .no-touch .cd-vertical-nav:hover a::before,   .no-touch .cd-vertical-nav:hover a::after,   .touch .cd-vertical-nav li:nth-of-type(n) a::before,   .touch .cd-vertical-nav li:nth-of-type(n) a::after {    transform: translateX(-50%) scale(1);  }}

当然到这里效果已经实现了,但是要实现点击导航菜单时页面做平滑滚动效果则需要借助jQuery来实现滚动动画。本例在下载源码包里已经打包了包含的全部jQuery代码,请大家下载后查看,本文不再描述。

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: Evolution and TrendsThe Future of HTML: Evolution and TrendsMay 13, 2025 am 12:01 AM

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

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.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.