jQuery在页面加载时平滑滚动 [英] jQuery Smooth Scrolling on Page Load

查看:80
本文介绍了jQuery在页面加载时平滑滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此jQuery脚本进行平滑滚动(位于CSS-Tricks.com上):

I'm using this jQuery Script to do Smooth Scrolling (Found on CSS-Tricks.com):

/** Smooth Scrolling Functionality **/
jQuery(document).ready(function($) {
  function filterPath(string) {
  return string
    .replace(/^\//,'')
    .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
    .replace(/\/$/,'');
  }
  var locationPath = filterPath(location.pathname);
  var scrollElem = scrollableElement('html', 'body');
  var urlHash = '#' + window.location.href.split("#")[1];

  $('a[href*=#]').each(function() {
    $(this).click(function(event) {
    var thisPath = filterPath(this.pathname) || locationPath;
    if (  locationPath == thisPath
    && (location.hostname == this.hostname || !this.hostname)
    && this.hash.replace(/#/,'') ) {
      var $target = $(this.hash), target = this.hash;
      if (target) {
        var targetOffset = $target.offset().top;
          event.preventDefault();
          $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
            location.hash = target;
          });
      }
    }
   });  
  });

  // use the first element that is "scrollable"
  function scrollableElement(els) {
    for (var i = 0, argLength = arguments.length; i <argLength; i++) {
      var el = arguments[i],
          $scrollElement = $(el);
      if ($scrollElement.scrollTop()> 0) {
        return el;
      } else {
        $scrollElement.scrollTop(1);
        var isScrollable = $scrollElement.scrollTop()> 0;
        $scrollElement.scrollTop(0);
        if (isScrollable) {
          return el;
        }
      }
    }
    return [];
  }

});
/** END SMOOTH SCROLLING FUNCTIONALITY **/

它的工作原理很棒,除了一件事,我希望它可以在有人直接进入url的地方工作,例如http://domain.com/page.html#anchor可以平滑地从页面加载的顶部滚动到该锚点,现在,除非他们单击了锚点,否则它立即转到页面锚点.我希望这是有道理的.

It works fantastic, except for one thing, I want it to work where if someone goes directly to the url e.g. http://domain.com/page.html#anchor it smooth scrolls to that anchor from the top on page load, right now it immediately goes to the page anchor unless they've clicked on an anchor. I hope that makes sense.

推荐答案

如果回答还不算太晚,那么您就可以.... 这是对PSR代码的修改,实际上可以使页面在加载时平滑滚动:

If it is not too late for answer then here you go.... Here is a modification of PSR's code that actually works for smooth scrolling of page on load:

http://jsfiddle.net/9SDLw/1425/

$(function(){
    $('html, body').animate({
        scrollTop: $( $('#anchor1').attr('href') ).offset().top
    }, 2000);
    return false;
});

更好的版本:

http://jsfiddle.net/9SDLw/1432/

$(function(){
    $('html, body').animate({
        scrollTop: $('.myclass').offset().top
    }, 2000);
    return false;
});

此脚本中需要做的就是用要滚动到的页面上的控件的类或ID替换"myclass".

All you need to do in this script is to replace the "myclass" with a class or ID of the control located on the page you want to scroll to.

已空

这篇关于jQuery在页面加载时平滑滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆