1. 程式人生 > >實現 “返回頂部”功能

實現 “返回頂部”功能

<!DOCTYPE html>  
<html>  
<head>  
    <meta charset='utf-8'>  
    <title>js2</title> 
    <style>
    	.returnTop{
    		position:fixed;
    		width:50;
    		height:60px;
    		right:20px;
    		bottom:20px;
    		background-color:red;
    		color:white;
    	}
    	.hide{
    		display:none;
    	}
    </style> 
<body>
	<div id='return_top' class='returnTop hide' onclick='Go();'>返回頂部</div>
	<div style='height:3000px;'></div>
	<script src='js/jquery-1.8.2.js'></script>
	<script src='js/oldboy.js'></script>
	<script type='text/javascript'>   
		$(windw).scroll(function(){
			var height = $(window).scrollTop();
			if(height > 0){
				$('#return_top').removeClass('hide')
			}	
			else{
				$('return_top').addClass('hide')
			}
			});
			function Go(){
				$(window).scrollTop(0);
			}
	</script>
</body>  
</head>  
</html>  



==================方法2====================
<!DOCTYPE html>  
<html>  
<head>  
    <meta charset='utf-8'>  
    <title>js2</title> 
    <style>
    	.returnTop{
    		position:fixed;
    		width:50;
    		height:60px;
    		right:20px;
    		bottom:20px;
    		background-color:red;
    		color:white;
    	}
    	.hide{
    		display:none;
    	}
    </style> 
<body>
	<div id='return_top' class='returnTop hide'>返回頂部</div>
	<div style='height:3000px;'>asd</div>
	<script src='js/jquery-1.8.2.js'></script>
	<script src='js/oldboy.js'></script>
	<script type='text/javascript'> 
		$(function(){
			$('#return_top').click(function(){
				$(window).scrollTop(0);
			})
		})  
		$(windw).scroll(function(){
			var height = $(window).scrollTop();
			if(height > 0){
				$('#return_top').removeClass('hide')
			}	
			else{
				$('return_top').addClass('hide')
			}
			});
	</script>
</body>  
</head>  
</html>