React实现顶部固定滑动式导航栏(导航条下拉一定像素时显示原导航栏样式)
摘要
基于react的框架开发一个顶部固定滑动式的酷炫导航栏,当导航栏置顶时,导航栏沉浸在背景图片里;当鼠标滑动滚轮时,导航栏固定滑动并展示下拉样式。
JS部分
相关技术栈:react、antd、react-router。详细的技术栈应用请参考官方文档的使用说明。
* 展示主页App.jsx组件代码
import React from 'react';
import './App.css';
import { Link, Route } from 'react-router-dom';
import { Layout } from 'antd';
//引入导航栏相对应的组件
import Home from './components/Home/Home';
import CoreTechnology from './components/Technology/Technology';
import Case from './components/Case/Case';
import About from './components/About/About';
import Join from './components/Join/Join'; const { Content } = Layout; class App extends React.Component {
//在componentDidMount生命周期中添加window的handleScroll滑动监听事件
componentDidMount() {
window.addEventListener('scroll', this.handleScroll);
}
//定义handleScroll事件函数
handleScroll =(e)=>{
var header = document.getElementById('header'); //定义一个dom节点为'header'的header变量
if(window.pageYOffset >= 80){ //if语句判断window页面Y方向的位移是否大于或者等于导航栏的height像素值
header.classList.add('header_bg'); //当Y方向位移大于80px时,定义的变量增加一个新的样式'header_bg'
} else {
header.classList.remove('header_bg'); //否则就移除'header_bg'样式
}
} render() {
return (
<div className="page" id="page">
<Layout>
<div className="header" id="header"> //导航栏div
<div className="brand">
<Link to="/">
<img src={require("./img/dkgw_logo@01.png")} alt="大可logo" width="" height="32"/>
</Link>
</div>
<div className="nav">
<ul>
<li>
<Link to="/technology">
<img src={require("./img/dkgw_hxjs.png")} alt="核心技术" width="32" height="32"/>
核心技术
</Link>
</li>
<li>
<Link to="/case">
<img src={require("./img/dkgw_hyal.png")} alt="行业案例" width="32" height="32"/>
行业案例
</Link>
</li>
<li>
<Link to="/about">
<img src={require("./img/dkgw_gywm.png")} alt="关于DUCK" width="32" height="32"/>
关于DUCK
</Link>
</li>
<li>
<Link to="/join">
<img src={require("./img/dkgw_jrwm.png")} alt="加入我们" width="32" height="32"/>
加入我们
</Link>
</li>
</ul>
</div>
</div> <Content className="content" id="content">
<Route path="/" exact component={ Home }/>
<Route path="/technology" component={ CoreTechnology }/>
<Route path="/case" component={ Case }/>
<Route path="/about" component={ About }/>
<Route path="/join" component={ Join }/>
</Content>
</Layout>
</div>
);
}
} export default App;
CSS部分
为了让导航栏置顶时悬浮在背景图上,需要给导航栏置特定的css样式。
position: fixed;
box-sizing: border-box;
display: flex;
justify-content: space-between;
align-items: center;
transition: 0.5s;
z-index: 1000;
* 主页App.css样式代码
@import '~antd/dist/antd.css'; //引入antd样式
@import url('https://fonts.googleapis.com/css?family=Roboto'); //引入谷歌字体样式 /* {通用样式开始} */
* {
margin: 0;
padding: 0;
font-family: Arial, Helvetica, sans-serif;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
/* {通用样式结束} */ /* {导航栏基础样式} */
.header
{
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 5rem;
padding: 0 10vw;
box-sizing: border-box;
display: flex;
justify-content: space-between;
align-items: center;
transition: 0.5s;
z-index: 1000;
}
{/*{导航栏新样式}*/}
.header.header_bg
{
background: #607d8b;
} .nav ul
{
margin: 0;
padding: 0;
display: flex;
} .nav ul li
{
list-style: none;
} .nav ul li a
{
color: #fff;
padding: 0 20px;
font-size: 1.1em;
text-decoration: none;
font-weight: bold;
} .brand a
{
color: #fff;
font-size: 1.1em;
text-decoration: none;
font-weight: bold;
}
基本效果图
导航栏置顶时:

导航栏下滑一定像素时:

最后
以上就是小编在实战开发中的一个小分享,如有任何说的不对的地方,欢迎大家指指点点!
React实现顶部固定滑动式导航栏(导航条下拉一定像素时显示原导航栏样式)的更多相关文章
- Web前端开发实战6:CSS实现导航菜单结合二级下拉式菜单的简单变换
前面几篇博文都在讲导航菜单和二级下拉式菜单,事实上有非常多方法都能够实现的.详细的情况还要视情况而定. 在后面学习到jQuery框架之后,会有更丰富的动画效果.因为在学习Ajax和jQuery的初步阶 ...
- 织梦DedeCMS v5.7 实现导航条下拉菜单
首先将下面这段代码贴到templets\default\footer.htm文件里(只要在此文件里就行,位置无所谓) <</span>script type='text/javasc ...
- Bootstrap -- 下拉菜单、输入框组、导航菜单
Bootstrap -- 下拉菜单.输入框组.导航菜单 1. 下拉菜单 可以使用带有各种大小按钮的下拉菜单:.btn-lg..btn-sm 或 .btn-xs. 实现下拉菜单: <!DOCTYP ...
- Delphi中,除了应用程序主窗口会显示在任务栏上,其它窗口默认都不会显示在任务栏.
Delphi中,除了应用程序主窗口会显示在任务栏上,其它窗口默认都不会显示在任务栏. Delphi中,除了应用程序主窗口会显示在任务栏上,其它窗口默认都不会显示在任务栏.没有MS开发环境中的ShowI ...
- Bootstrap历练实例:带有下拉菜单的标签和胶囊导航
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- DEDECMS v5.7 完美实现导航条下拉二级菜单
一.引言 好多人都问,织梦的下拉导航怎么做呢?其实很简单!即使你对代码一点也不熟悉,没关系! 按照我的步骤走!记住一步也不能错哦! 二.实现过程 1.首先: 将下面这段代码贴到templets\def ...
- 鼠标移上显示的下拉菜单,和鼠标移上时显示的导航,html,JavaScript代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Bootstrap框架(基础篇)之按钮,网格,导航栏,下拉菜单
一,按钮 注意:虽然在Bootstrap框架中使用任何标签元素都可以实现按钮风格,但个人并不建议这样使用,为了避免浏览器兼容性问题,个人强烈建议使用button或a标签来制作按钮. 框架中提供了基础按 ...
- 20款jquery下拉导航菜单特效代码分享
20款jquery下拉导航菜单特效代码分享 jquery仿京东商城左侧分类导航下拉菜单代码 jQuery企业网站下拉导航菜单代码 jQuery css3黑色的多级导航菜单下拉列表代码 jquery响应 ...
随机推荐
- 解读typescript中 super关键字的用法
解读typescript中 super关键字的用法 传统的js,使用prototype实现父.子类继承.如果父.子类有同名的方法,子类去调用父类的同名方法需要用 “父类.prototype.metho ...
- 8个华丽而实用的Java图表类库
8个华丽而实用的Java图表类库 转 https://www.300168.com/yidong/show-2744.html 核心提示:学习Java的同学注意了!!! 学习过程中遇到什么问题或者 ...
- 简易的CRM系统案例之Struts2+JSP+MySQL版本
对简易的CRM系统案例之Servlet+Jsp+MySQL版本改进 Servlet优化为Struts2 学习 <?xml version="1.0" encoding=&qu ...
- Spring Boot属性配置&自定义属性配置
一.修改默认配置 例1.spring boot 开发web应用的时候,默认tomcat的启动端口为8080,如果需要修改默认的端口,则需要在application.properties 添加以下记录: ...
- VGG网络-ILSVRC-2014亚军
用于大尺度图片识别的非常深的卷积网络 使用一个带有非常小的(3*3)的卷积核的结构去加深深度,该论文的一个十分重要的改进就是它将卷机网络的深度增加到了16-19层,且可以用于比较大224*224的图片 ...
- java读取request中的xml
java读取request中的xml 答: // 读取xml InputStream inputStream; StringBuffer sb = new StringBuffer(); inpu ...
- ABAP ole操作
1.ole 如何保存和退出call method of sheetname 'saves' exporting #1 = filepath #2 = 1. call method of applica ...
- (十四)用session和过滤器方法检验用户是否登录
一.session方法 1.1 编写登录页面文件(index.html) <!doctype html> <html> <head> <title>测试 ...
- 查看php 某个服务的进程数
查看进程就是使用ps命令而已,只不顾ps的参数太多了. 使用php查询的话,必须要开启几个函数(可以执行外部程序的函数),参考官网:http://php.net/manual/zh/book.exec ...
- Node.js ORM框架Sequelize使用示例
示例代码: const Sequelize = require('sequelize'); const sequelize = new Sequelize('database', 'username' ...