效果如下图所示
完整例子
代码如下 | 复制代码 |
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>演示:超酷的页面滚动效果</title> <link rel="stylesheet" type="text/css" href="css/main.css" /> <style type="text/css"> .nav{width:160px; position:fixed; margin-left:-170px; top:20%; z-index:1999; background:#f0f0f0; border: 1px solid #ccc; -webkit-border-radius: 6px;-moz-border-radius: 6px;border-radius: 6px; -webkit-box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);-moz-box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);} .nav li{height:36px; line-height:36px; border-bottom:1px solid #d3d3d3; text-align:center; cursor:pointer} .nav li:last-child{border-bottom:none} .nav li a{display:block; width:98%; height:34px;font-size:16px;} .nav li a:hover{background:#f9f9f9; text-decoration:none} .nav li a.cur{background:#ffc} .box{height:600px; margin: 0 10px} .box h3{height:32px; line-height:32px; padding-left:20px; font-size:14px} .box p{line-height:30px; margin:20px; text-align:center; font-size:28px} .box p span{margin:10px} #pro,#ser{background:url(img/bg.jpg)} #news,#con{background:url(img/bg2.gif)} </style> <script type="text/javascript" src="1.7.2/jquery.min.js"></script> <script type="text/javascript"> $(function(){ navpos(); var pro_top = $("#pro").offset().top; var news_top = $("#news").offset().top; var ser_top = $("#ser").offset().top; var con_top = $("#con").offset().top; var job_top = $("#job").offset().top; //alert(tops); $(window).scroll(function(){ var scroH = $(this).scrollTop(); if(scroH>=job_top){ set_cur(".job"); }else if(scroH>=con_top){ set_cur(".con"); }else if(scroH>=ser_top){ set_cur(".ser"); }else if(scroH>=news_top){ set_cur(".news"); }else if(scroH>=pro_top){ set_cur(".pro"); } }); $(".nav li a").click(function() { var el = $(this).attr('class'); $('html, body').animate({ scrollTop: $("#"+el).offset().top }, 300); $(this).addClass("cur").parent().siblings().find("a").removeClass("cur"); }); }); $(window).resize(function(){ navpos(); }); function navpos(){ var offset = $("#main").offset().left; var nav_w = $(".nav").outerWidth(); var left = offset-nav_w; //alert(left); if(left>10){ $(".nav").css("margin-left","-170px"); }else{ $(".nav").css("margin-left",-(160+left)+"px"); } } function set_cur(n){ if($(".nav a").hasClass("cur")){ $(".nav a").removeClass("cur"); } $(".nav a"+n).addClass("cur"); } </script> </head> <body>
<div id="main"> </body> |