简体   繁体   English

jQuery平滑滚动Onclick

[英]Jquery Smooth Scroll Onclick

I am currently using a smooth scroll script from css tricks. 我目前正在使用来自CSS技巧的平滑滚动脚本。

The problem I'm having is that I have used 's as links with onclick links to anchor elements. 我遇到的问题是,我已经将的和用作与锚元素的onclick链接。

You can see it here . 你可以在这里看到它。 The problem is the script doesn't look for what I need it to, the links at the top (the divs) do not get found by the script so do not smoothly scroll to the anchor. 问题在于脚本没有找到我需要的内容,脚本未找到顶部的链接(div),因此无法平滑滚动至锚点。 Whereas the about, services, contact links (the text ones inside the green thingys) scroll smoothly. 而About,Service,Contact链接(绿色小物件中的文本链接)会平滑滚动。

I am complete jquery and javascript noob and do not know how to alter the script to include the onclick divs or to make a script to make it scroll smoothly to the anchors. 我是完整的jquery和javascript noob,并且不知道如何更改脚本以包括onclick div或制作脚本以使其平滑滚动到锚点。

I need the script to scroll smoothly from both the div links and text links or I need a duplicate script that works with the div links (2 scripts that do text links and div links - The one I'm using atm only does text) 我需要该脚本从div链接和文本链接中平滑滚动,或者我需要一个可与div链接一起使用的重复脚本(2个执行文本链接和div链接的脚本-我正在使用的atm脚本仅执行文本操作)

<script>
$(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');

$('a[href*=#]').each(function() {
  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;
    $(this).click(function(event) {
      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 [];
  }

    });
</script>

I don't know if you use a javascript framework like jQuery. 我不知道您是否使用jQuery之类的JavaScript框架。 For websites like this I always use the jQuery plugin 'Localscroll'. 对于这样的网站,我总是使用jQuery插件“ Localscroll”。 It always works fine cross-browser. 跨浏览器始终有效。

You can see the documentation for the plugin here: http://flesler.blogspot.com/2007/10/jquerylocalscroll-10.html 您可以在此处查看该插件的文档: http : //flesler.blogspot.com/2007/10/jquerylocalscroll-10.html

It's very easy to use. 它很容易使用。

Good luck! 祝好运!

You can use anchors instead of divs in the nav, right now you're doing 您现在可以在导航栏中使用锚点而不是div。

<div id="whatever" onclick="window.location=#portfolio">portfolio</div>

but that's not semantic, and it doesn't work the way you want it to, so that's doubleplus not good. 但这不是语义的,并且不能按您希望的方式工作,因此doubleplus不好。 use 采用

<a id="whatever" href="#portfolio" style="display:block">portfolio</a>

then the code from css tricks should work... if it's clickable it should probably be a button or an anchor. 那么css技巧中的代码应该可以工作...如果可以单击,则可能应该是按钮或锚点。

the anchor tags in your body work as expected, btw... 人体中的锚标签会按预期工作,但...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM