js鼠标滚动自动隐藏导航菜单
时间:2023年10月23日
/来源:网络
/编辑:佚名
html代码:
<header class="header-navigation" id="header">
<nav> <a class="link" href="#" title="Home">首页</a> <a class="link" href="#" title="About">关于我们</a> <a class="link" href="#" title="Contact">联系我们</a> </nav>
</header>
<div class="jq22-container">
<div class="jq22-header">
<h1>网站内容</h1>
</div>
</div>
css代码:
body {
background:#494A5F;
color:#D5D6E2;
font-weight:500;
font-size:1.05em;
font-family:"Microsoft YaHei","Segoe UI","Lucida Grande",Helvetica,Arial,sans-serif;
}
a {
color:rgba(255,255,255,0.6);
outline:none;
text-decoration:none;
-webkit-transition:0.2s;
transition:0.2s;
}
a:hover,a:focus {
color:#74777b;
text-decoration:none;
}
.jq22-container {
margin:0 auto;
}
/* Header */
.jq22-header {
padding:1em 190px 1em;
letter-spacing:-1px;
text-align:center;
}
.jq22-header h1 {
color:#fff;
font-weight:600;
font-size:2em;
line-height:1;
margin-bottom:0;
}
.jq22-header h1 span {
display:block;
font-size:60%;
font-weight:400;
padding:0.8em 0 0.5em 0;
color:#fff;
}
html,* {
margin:0;
padding:0;
}
html {
min-height:300vh;
}
body {
min-height:300vh;
background-image:linear-gradient(120deg,#fccb90 0%,#d57eeb 100%);
}
.header-navigation {
position:fixed;
top:0;
width:100%;
height:60px;
line-height:60px;
background-color:#333;
text-align:center;
box-shadow:0 14px 28px rgba(0,0,0,0.25),0 10px 10px rgba(0,0,0,0.22);
z-index:9999;
}
.link {
color:#fff;
text-decoration:none;
margin:0 30px;
}
h1 {
text-align:center;
margin-top:170px;
margin-bottom:50px;
color:#fff;
}
/* Slide transitions */
.slideUp {
/* -webkit-transform:translateY(-100px);
transform:translateY(-100px);
*/
-webkit-transform:translateY(-100px);
-ms-transform:translateY(-100px);
-o-transform:translateY(-100px);
transform:translateY(-100px);
/*transition:transform .5s ease-out;
*/
-webkit-transition:transform .5s ease-out;
-o-transition:transform .5s ease-out;
transition:transform .5s ease-out;
}
.slideDown {
/*-webkit-transform:translateY(0);
transform:translateY(0);
*/
-webkit-transform:translateY(0);
-ms-transform:translateY(0);
-o-transform:translateY(0);
transform:translateY(0);
/*transition:transform .5s ease-out;
*/
-webkit-transition:transform .5s ease-out;
-o-transition:transform .5s ease-out;
transition:transform .5s ease-out;
}
js代码:
var new_scroll_position = 0;
var last_scroll_position;
var header = document.getElementById("header");
window.addEventListener('scroll', function(e) {
last_scroll_position = window.scrollY;
// Scrolling down
if (new_scroll_position < last_scroll_position && last_scroll_position > 80) {
// header.removeClass('slideDown').addClass('slideUp');
header.classList.remove("slideDown");
header.classList.add("slideUp");
// Scrolling up
} else if (new_scroll_position > last_scroll_position) {
// header.removeClass('slideUp').addClass('slideDown');
header.classList.remove("slideUp");
header.classList.add("slideDown");
}
new_scroll_position = last_scroll_position;
});
<header class="header-navigation" id="header">
<nav> <a class="link" href="#" title="Home">首页</a> <a class="link" href="#" title="About">关于我们</a> <a class="link" href="#" title="Contact">联系我们</a> </nav>
</header>
<div class="jq22-container">
<div class="jq22-header">
<h1>网站内容</h1>
</div>
</div>
css代码:
body {
background:#494A5F;
color:#D5D6E2;
font-weight:500;
font-size:1.05em;
font-family:"Microsoft YaHei","Segoe UI","Lucida Grande",Helvetica,Arial,sans-serif;
}
a {
color:rgba(255,255,255,0.6);
outline:none;
text-decoration:none;
-webkit-transition:0.2s;
transition:0.2s;
}
a:hover,a:focus {
color:#74777b;
text-decoration:none;
}
.jq22-container {
margin:0 auto;
}
/* Header */
.jq22-header {
padding:1em 190px 1em;
letter-spacing:-1px;
text-align:center;
}
.jq22-header h1 {
color:#fff;
font-weight:600;
font-size:2em;
line-height:1;
margin-bottom:0;
}
.jq22-header h1 span {
display:block;
font-size:60%;
font-weight:400;
padding:0.8em 0 0.5em 0;
color:#fff;
}
html,* {
margin:0;
padding:0;
}
html {
min-height:300vh;
}
body {
min-height:300vh;
background-image:linear-gradient(120deg,#fccb90 0%,#d57eeb 100%);
}
.header-navigation {
position:fixed;
top:0;
width:100%;
height:60px;
line-height:60px;
background-color:#333;
text-align:center;
box-shadow:0 14px 28px rgba(0,0,0,0.25),0 10px 10px rgba(0,0,0,0.22);
z-index:9999;
}
.link {
color:#fff;
text-decoration:none;
margin:0 30px;
}
h1 {
text-align:center;
margin-top:170px;
margin-bottom:50px;
color:#fff;
}
/* Slide transitions */
.slideUp {
/* -webkit-transform:translateY(-100px);
transform:translateY(-100px);
*/
-webkit-transform:translateY(-100px);
-ms-transform:translateY(-100px);
-o-transform:translateY(-100px);
transform:translateY(-100px);
/*transition:transform .5s ease-out;
*/
-webkit-transition:transform .5s ease-out;
-o-transition:transform .5s ease-out;
transition:transform .5s ease-out;
}
.slideDown {
/*-webkit-transform:translateY(0);
transform:translateY(0);
*/
-webkit-transform:translateY(0);
-ms-transform:translateY(0);
-o-transform:translateY(0);
transform:translateY(0);
/*transition:transform .5s ease-out;
*/
-webkit-transition:transform .5s ease-out;
-o-transition:transform .5s ease-out;
transition:transform .5s ease-out;
}
js代码:
var new_scroll_position = 0;
var last_scroll_position;
var header = document.getElementById("header");
window.addEventListener('scroll', function(e) {
last_scroll_position = window.scrollY;
// Scrolling down
if (new_scroll_position < last_scroll_position && last_scroll_position > 80) {
// header.removeClass('slideDown').addClass('slideUp');
header.classList.remove("slideDown");
header.classList.add("slideUp");
// Scrolling up
} else if (new_scroll_position > last_scroll_position) {
// header.removeClass('slideUp').addClass('slideDown');
header.classList.remove("slideUp");
header.classList.add("slideDown");
}
new_scroll_position = last_scroll_position;
});
新闻资讯 更多
- 【建站知识】查询nginx日志状态码大于400的请求并打印整行04-03
- 【建站知识】Python中的logger和handler到底是个什么?04-03
- 【建站知识】python3拉勾网爬虫之(您操作太频繁,请稍后访问)04-03
- 【建站知识】xpath 获取meta里的keywords及description的方法04-03
- 【建站知识】python向上取整以50为界04-03
- 【建站知识】scrapy xpath遇见乱码解决04-03
- 【建站知识】scrapy爬取后中文乱码,解决word转为html 时cp1252编码问题04-03
- 【建站知识】scrapy采集—爬取中文乱码,gb2312转为utf-804-03