例1 ,防百度百科页面滚动效果
代码如下 | 复制代码 |
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>jquery防百度百科页面滚动效果</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="http://ajax.googleapis.com/ajax/libs/jquery/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> |
例2,防淘宝网右边滚动效果
代码如下 | 复制代码 |
<!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:50px; /*position:fixed; right:5px;*/ position:fixed; margin-left:920px; top:30%; 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:50px; line-height:50px; border-bottom:1px solid #d3d3d3; background:url(img/icon.png) no-repeat; text-align:center; cursor:pointer} .nav li:hover{background-color:#f9f9f9; text-decoration:none} .nav li:last-child{border-bottom:none} .nav li.cur{background-color:#ffc} .nav li.pro{background-position:0 0} .nav li.news{background-position:0 -55px} .nav li.ser{background-position:0 -105px} .nav li.con{background-position:0 -155px} .nav li.job{background-position:0 -207px} .box{height:500px; 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="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script type="text/javascript" src="jquery.easing.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").click(function() { var el = $(this).attr('class'); $('html, body').animate({ scrollTop: $("#"+el).offset().top },{ easing: 'easeOutSine', duration: 300, complete:function(){ } }); }); }); $(window).resize(function(){ navpos(); }); function navpos(){ var page_w = $(document).width(); var offset = $("#main").offset().left; var main_w = $("#main").outerWidth(); var right = page_w-offset-main_w-50; //alert(right); if(right>10){ $(".nav").css('right',right-10); }else{ $(".nav").css('right',10); } } function set_cur(n){ if($(".nav li").hasClass("cur")){ $(".nav li").removeClass("cur"); } $(".nav li"+n).addClass("cur"); } </script> </head> <body> <div id="main"> </body> |
例3,固定在页面底部效果
代码如下 | 复制代码 |
<!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:410px; height:36px; position:fixed; bottom:1px; margin-left:250px; z-index:1999; background:#f0f0f0; border: 1px solid #ccc; -webkit-border-radius: 6px;-moz-border-radius: 6px;border-radius: 6px; -webkit-box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.2);-moz-box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.2);box-shadow: 2px 2px 5px 2px rgba(0, 0, 0, 0.2);} .nav li{height:36px; line-height:36px; float:left; padding:0 8px; border-right:1px solid #d3d3d3; text-align:center; cursor:pointer} .nav li a{display:block; height:34px;font-size:16px;} .nav li a:hover{background:#f9f9f9; text-decoration:none} .nav li:last-child{border-right:none} .nav li a.cur{color:#f60} .box{height:800px; 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="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script type="text/javascript" src="jquery.easing.min.js"></script> <script type="text/javascript"> $(function(){ 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 },{ easing: 'easeOutSine', duration: 300, complete:function(){ } }); }); }); function set_cur(n){ <body> <div id="main"> </body> |
源码下载地址:http://file.111cn.net/upload/2013/10/pageScroll.rar