[关闭]
@52fhy 2015-09-25T20:27:56.000000Z 字数 3499 阅读 552

ScrollTo:平滑滚动到页面指定位置

JavaScript


ScrollTo是一款基于jQuery的滚动插件,当点击页面的链接时,可以平滑地滚动到页面指定的位置。

适用在一些页面内容比较多,页面长度有好几屏的场合,例如城市选择侧边的快速定位。

查看演示

使用

1、准备jQuery库和scrollTo.js插件。

  1. <script type="text/javascript" src="js/jquery.js"></script>
  2. <script type="text/javascript" src="js/jquery.scrollTo.js"></script>

2、HTML

  1. <ul class="nav">
  2. <li><a href="#" class="nav_pro">产品展示</a></li>
  3. <li><a href="#" class="nav_news">新闻中心</a></li>
  4. <li><a href="#" class="nav_ser">服务支持</a></li>
  5. <li><a href="#" class="nav_con">联系我们</a></li>
  6. <li><a href="#" class="nav_job">人才招聘</a></li>
  7. </ul>
  8. <div id="pro" class="box">
  9. <h3>产品展示</h3>
  10. </div>
  11. <div id="news" class="box">
  12. <h3>新闻中心</h3>
  13. </div>
  14. <div id="ser" class="box">
  15. <h3>服务支持</h3>
  16. </div>
  17. <div id="con" class="box">
  18. <h3>联系我们</h3>
  19. </div>
  20. <div id="job" class="box">
  21. <h3>人才招聘</h3>
  22. </div>

我们用一个页面展示导航和导航对应的每个模块。

3、CSS

  1. .nav{width:500px;margin:20px auto;}
  2. .nav li{float:left; width:100px; height:24px; line-height:24px}
  3. .box{height:500px}
  4. .box h3{height:32px; line-height:32px; padding-left:20px; font-size:14px}
  5. #pro,#ser{background:url(img/bg.jpg)}
  6. #news,#con{background:url(img/bg2.gif)}

4、使用scrollTo.js插件

  1. $(function(){
  2. $(".nav_pro").click(function(){
  3. $.scrollTo('#pro',500);
  4. });
  5. $(".nav_news").click(function(){
  6. $.scrollTo('#news',800);
  7. });
  8. $(".nav_ser").click(function(){
  9. $.scrollTo('#ser',1000);
  10. });
  11. $(".nav_con").click(function(){
  12. $.scrollTo('#con',1200);
  13. });
  14. $(".nav_job").click(function(){
  15. $.scrollTo('#job',1500);
  16. });
  17. });

当点击导航按钮时,触发scrollTo方法,$.scrollTo带有两个参数,第一个是指定要滚动的目的ID,第二个参数是滚动时间间隔,以毫秒为单位。在本例中,只应用了scrollTo的基本方法。

其实scrollTo还可以指定横向纵向滚动,传冲效果等,具体可以参照官方网站的例子:http://demos.flesler.com/jquery/scrollTo/

API

  1. $(element).scrollTo(target[,duration][,settings]);

element

This must be a scrollable element, to scroll the whole window use $(window).

target

This defines the position to where element must be scrolled. The plugin supports all these formats:
* A number with a fixed position: 250
* A string with a fixed position with px: "250px"
* A string with a percentage (of container's size): "50%"
* A string with a relative step: "+=50px"
* An object with left and top containining any of the aforementioned: {left:250, top:"50px"}
* The string "max" to scroll to the end.
* A string selector that will be relative to the element to scroll: ".section:eq(2)"
* A DOM element, probably a child of the element to scroll: document.getElementById("top")
* A jQuery object with a DOM element: $("#top")

settings

The duration parameter is a shortcut to the setting with the same name.
These are the supported settings:
* axis: The axes to animate: xy (default), x, y, yx
* interrupt: If true will cancel the animation if the user scrolls. Default is false
* limit: If true the plugin will not scroll beyond the container's size. Default is true
* margin: If true, subtracts the margin and border of the target element. Default is false
* offset: Added to the final position, can be a number or an object with left and top
* over: Adds a % of the target dimensions: {left:0.5, top:0.5}
* queue: If true will scroll one axis and then the other. Default is false
* onAfter(target, settings): A callback triggered when the animation ends (jQuery's complete())
* onAfterFirst(target, settings): A callback triggered after the first axis scrolls when queueing

You can add any setting supported by $().animate() as well:

window shorthand

You can use $.scrollTo(...) as a shorthand for $(window).scrollTo(...).

Changing the default settings

As with most plugins, the default settings are exposed so they can be changed.

  1. $.extend($.scrollTo.defaults, {
  2. axis: 'y',
  3. duration: 800
  4. });
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注