Table of Contents
1.当前页面高亮显示的导航栏
首页
2.划入自动切换的导航条
3.弹性子菜单实现
Home Web Front-end HTML Tutorial CSS+JQ实现炫酷导航栏_html/css_WEB-ITnose

CSS+JQ实现炫酷导航栏_html/css_WEB-ITnose

Jun 24, 2016 am 11:27 AM

一步一步的学习,后面再做个综合页面

1.当前页面高亮显示的导航栏

首先是HTML代码,很简单,ul+li实现菜单

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>导航栏一</title></head><body>    <header class="header">        <div class="nva">            <ul class="list">                <li><a href="Android.html">Android</a></li>                <li><a href="C++.html">C++</a></li>                <li><a href="IOS.html">IOS</a></li>                <li><a href="Java.html">Java</a></li>                <li><a href="Ruby.html">Ruby</a></li>            </ul>        </div>    </header><h1 id="首页">首页</h1></body></html>
Copy after login

基本效果:

接下来设置CSS属性,这里要注意标签a是行级元素,所以需要用display转成块级元素,这个很常用,还有就是line-height的常见用法

*{ margin:0; padding: 0;}a{ text-decoration: none;}.nva{ width: 100%; height: 40px; margin-top: 70px; background-color: #222;}.list{ width: 80%; height: 40px; margin: 0 auto; list-style-type: none;}.list li{ float: left;}.list li a{ padding: 0 30px; color: #aaa; line-height: 40px; display: block;}.list li a:hover{ background:#333; color:#fff;}.list li a.on{ background:#333; color:#fff;}h1{ margin: 20px auto; text-align: center;}
Copy after login

实现的效果:

最后就是JS动态添加定位效果了
js里面这样考虑,页面跳转就会有链接,根据链接的后缀来匹配属性,匹配则更改样式即可达到想要的效果
需要注意的就是如何获取URL,如何从URL里面查找出href信息

$(function(){    //当前链接以/分割后最后一个元素索引    var index = window.location.href.split("/").length-1;    //最后一个元素前四个字母,防止后面带参数    var href = window.location.href.split("/")[index].substr(0,4);    if(href.length>0){        //如果匹配开头成功则更改样式        $(".list li a[href^='"+href+"']").addClass("on");        //[attribute^=value]:匹配给定的属性是以某些值开始的元素。    }else {        //默认主页高亮        $(".list li a[href^='index']").addClass("on");    }});
Copy after login

效果图

2.划入自动切换的导航条

在1的基础上,修改一下HTMLa标签内容,然后通过CSS设置动画效果

<ul class="list">                <li><a href="index01.html">                <b>首页</b>                <i>Index</i>                </a></li>                <li><a href="Android.html">                    <b>Android</b>                    <i>安卓</i>                </a></li>                <li><a href="C++.html">                    <b>C++</b>                    <i>谁加加</i>                </a></li>                <li><a href="IOS.html">                    <b>IOS</b>                    <i>苹果</i>                </a></li>                <li><a href="Java.html">                    <b>Java</b>                    <i>爪哇</i>                </a></li>                <li><a href="Ruby.html">                    <b>Ruby</b>                    <i>如八一</i>                </a></li>            </ul>
Copy after login

CSS实现动画效果,首先把b和i标签都设置为块级元素,这样的话就可以垂直分布,再给a设置一个transition,所谓的动画,就是划入后改变把a上移,再给a加个边框好观察,看下图

最后想实现效果,就给包裹菜单的div设置一个溢出隐藏属性即可

*{ margin:0; padding: 0;}a{ text-decoration: none;}.nva{ width: 100%; height: 40px; margin-top: 70px; background-color: #222; overflow: hidden;}.list{ width: 80%; height: 40px; margin: 0 auto; list-style-type: none;}.list li{ float: left;}.list li a{ padding: 0 30px; color: #aaa; line-height: 40px; display: block; transition: 0.3s;}                            .list b,.list i{ display: block; }.list li a:hover{ margin-top: -40px; background:#333; color:#fff;}.list li a.on{ background:#333; color:#fff;}h1{ margin: 20px auto; text-align: center;}
Copy after login


也可以采用JQ来实现,代码如下

$(function () {    $(".list a").hover(function () {        //stop是当执行其他动画的时候停止当前的        $(this).stop().animate({            "margin-top": -40        }, 300);    }, function () {        $(this).stop().animate({            "margin-top": 0        }, 300);    });});
Copy after login

3.弹性子菜单实现

首先子菜单使用div包裹,里面都是a标签,如下

                <li><a href="Android.html">                    <b>Android</b>                </a>                    <div class="down">                        <a href="#">子菜单1</a>                        <a href="#">子菜单2</a>                        <a href="#">子菜单3</a>                        <a href="#">子菜单4</a>                    </div>                </li>
Copy after login

接下来设置样式,既然是子菜单,就要脱离文档页面,所以使用absolute属性,使用这个属性那么父容器就要是relative

*{ margin:0; padding: 0;}a{ text-decoration: none;}.nva{ width: 100%; height: 40px; margin-top: 70px; background-color: #222; position: relative;}.list{ width: 80%; height: 40px; margin: 0 auto; list-style-type: none;}.list li{ float: left;}.list li a{ padding: 0 30px; color: #aaa; line-height: 40px; display: block; transition: 0.3s;}.list b{ display: block;}.list li a:hover{ background:#333; color:#fff;}.list li a.on{ background:#333; color:#fff;}.list .down{ position: absolute; top: 40px; background-color: #222; /*display: none;*/}.list .down a{ color: #aaa; padding-left: 30px; display: block;}h1{ margin: 20px auto; text-align: center;}
Copy after login

如下效果:

接下来使用JQ和easing插件来控制动画
find方法一般用来查找操作元素的后代元素的

$(function () {    $(".list li").hover(function () {        //这里使用了easing插件        $(this).find(".down").stop().slideDown({duration:1000,easing:"easeOutBounce"});    }, function () {        $(this).find(".down").stop().slideUp({duration:1000,easing:"easeOutBounce"});    });});
Copy after login

效果,图片录制不太好,实际上都是弹性动画的

Statement of this Website
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

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

See all articles