锚链接不会纠正锚标签

时间:2013-12-04 19:35:31

标签: jquery html scroll anchor

在网站DanielLmusic.com上,单页滚动网站,锚标签没有正确滚动,我在解决原因时遇到了一些困难。只有生物部分滚动到正确。听取和联系部分关闭半页...我正在使用以下平滑滚动代码:

<script type="text/javascript">
$(document).ready(function() {
    $('li.internal a').click(function (e) { 
        e.preventDefault(); //prevents screen from blinking when link is clicked
    });
  function filterPath(string) {
    return string
      .replace(/^\//,'')
      .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
      .replace(/\/$/,'');
  }
  $('a[href*=#]').each(function() { /*<----changed "('a[href" to ('li a[href" to avoid conflict with isotope*/
    if ( filterPath(location.pathname) == filterPath(this.pathname)
    && location.hostname == this.hostname
    && this.hash.replace(/#/,'') ) {
      var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
      var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
       if ($target) {
         var targetOffset = $target.offset().top;
         $(this).click(function() {
           $('html, body').animate({scrollTop: targetOffset}, 1000)
           var d = document.createElement("div");
        d.style.height = "101%";
        d.style.overflow = "hidden";
        document.body.appendChild(d);
        window.scrollTo(0,scrollToM);
        setTimeout(function() {
        d.parentNode.removeChild(d);
            }, 10);
           return false;
         });
      }
    }
  });
});
/*! Smooth Scroll - v1.4.5 - 2012-07-22
* Copyright (c) 2012 Karl Swedberg; Licensed MIT, GPL */
</script>

我在html中使用单独的锚ID如下:

<div id="pages">
        <a id="bioanchor"></a>
        <div id="biopage">

...

<div id="listenpage">
            <a id="listenanchor"></a>
            <h1>Listen</h1>
            <div id="soundsystem">

...

        <div id="contactpage">

            <a id="contactanchor"></a> 

如果有人能就此提出建议,我将非常感谢!谢谢。

1 个答案:

答案 0 :(得分:0)

从第一眼看,似乎变量scrollToM在行window.scrollTo(0,scrollToM);

中未定义