通过HTML+CSS+JavaScript实现鼠标移动到页面顶部导航栏出现,如果移出导航栏3秒又隐藏起来,而且不受滚动条影响(二)

通过HTML+CSS+JavaScript实现鼠标移动到页面顶部导航栏出现,如果移出导航栏3秒又隐藏起来,而且不受滚动条影响(二)

效果:默认一直隐藏导航栏,当滚动条滚到超过300px按钮出现,点击回到顶部按钮回到顶部,并隐藏按钮,鼠标移动到顶部导航栏出现,鼠标移出导航栏后3秒后再次隐藏(导航栏和按钮都是固定定位)

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript_test</title>
<style>
body {
	margin: 0;
	padding: 0;
	height: 3000px;    /*让滚动条出现*/
}
.DW {
	display: flex;           /*固定定位*/
	justify-content: center;  /*让文字水平居中*/
	align-items: center;      /*让文字垂直居中*/
	width: 100%;
	height: 80px;
	background-color: green;   /*背景颜色绿色*/
	color: aliceblue;
	font-size: 2em;
	transition: top .5s linear;  /*导航栏过渡出现和隐藏*/
	position: fixed;   /* 绝对定位,不管滚动条上下滚动都在相应的位置*/
	top: -80px;        /*隐藏导航栏*/
	left: 0;
}
.goTop {
	width: 50px;
	height: 50px;
	background-color: aquamarine;
	font-size: 20px;
	text-align: center;
	line-height: 25px;
	color: azure;
	position: fixed;    /*固定定位*/
	bottom: 50px;
	right: 50px;
	display: none;   /*隐藏按钮*/
}
.fusu {
	position: fixed;    /*固定定位*/
	width: 100%;
	height: 10px;
	left: 0;
	top: 0;
}
</style>
</head>

<body>
<div class="fusu divTop" id="divtop"></div>
<div class="DW topdhl divdhl" id="dhl">导航栏</div>
<buttion class="goTop" id="gotop">回到顶部</buttion>
<script>
  
	//获取导航栏、按量、顶部div的元素
	var topw=document.getElementById("dhl")
	var goTop=document.getElementById("gotop")
	var divTop=document.getElementById("divtop")

	
	  //滚动滚动条触发事件
	    window.onscroll=function(){
		  //获取滚动条位置
		var jhlheight=document.documentElement.scrollTop||document.body.scrollTop
		//判断滚动条位置
		if(jhlheight>=300){
		goTop.style.display="block"	 //显示按钮
		}else{
		goTop.style.display="none"	//隐藏按钮	 
   }
		
	}
	  
	 //点击按钮事件
	  goTop.onclick=function(){
		
		window.scrollTo({     //设置滚动条位置
			top:0,            //回到顶部
			behavior:"smooth"   //平滑过渡
		})		
	}  
	  
    //鼠标移入div后触发的事件
	var ss=divTop.onmouseover=function(){
		var tar=topw.style.top="0px"		
	} 
    
    //鼠标移入导航栏触发的事件
    topw.onmouseover=function(){
	var tar=topw.style.top="0px"
				
	} 
	
	//鼠标移出导航栏后触发的事件
	topw.onmouseout=function(){
    //定时器函数
	setTimeout(function(){
    topw.style.top="-80px"
     },3000)			
	}   
	
	</script>
</body>
</html>

posted @   永恒之月TEL  阅读(565)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开
  1. 1 Welcome Home REOL
  2. 2 Everywhere We Go REOL
  3. 3 凄美地 REOL
  4. 4 最后一页 REOL
Welcome Home - REOL
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Benjamin Cooper

作曲 : Benjamin Cooper

Sleep don't visit, so I choke on sun

And the days blur into one

And the backs of my eyes hum with things I've never done

Sheets are swaying from an old clothesline

[01:08.0]Like a row of captured ghosts over old dead grass

Was never much but we made the most

Was never much but we made the most

Welcome home

Ships are launching from my chest

Ships are launching from my chest

Some have names but most do not

Some have names but most do not

you find If one, please let me know what piece I've lost

Heal the scars from off my back

I don't need them anymore

I don't need them anymore

You can throw them out or keep them in your mason jars

I've come home

I've come home

All my nightmares escaped my head

All my nightmares escaped my head

Bar the door, please don't let them in

Bar the door, please don't let them in

You were never supposed to leave

You were never supposed to leave

Now my head's splitting at the seams

Now my head's splitting at the seams

And I don't know if I can

And I don't know if I can

Here, beneath my lungs

Here, beneath my lungs

I feel your thumbs press into my skin again