douyi1855 2015-08-24 01:37
浏览 86
已采纳

动画锚链接 - Wordpress [复制]

This question already has an answer here:

I have an anchor link set up in my Wordpress site which links from the navigation to the footer.

enter image description here

HTML - footer.php

<footer id="footer-anchor">

  <div class="row"> 

    ...

The when the link is selected the page 'jumps' to the footer. I want it to animate down to the footer. Similar to how pages animated to the top with a 'back to top' button, but in reverse.

</div>
  • 写回答

2条回答 默认 最新

  • dongman5539 2015-08-24 03:03
    关注

    I like to use this code from Karl Swedberg, I typically include it using a <script></script> in my footer.php file just before the </body> tag or you could enqueue it in your functions.php file and have it load in the footer. I like this code because it removes the #hash from the URL once you click on the anchor.

    jQuery(document).ready(function($){
        // From: http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links/
        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;
              // Added line below from previous version
              target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
              if (target) {
                var targetOffset = $target.offset().top;
                $(this).click(function(event) {
                  event.preventDefault();
            $(scrollElem).animate({
                scrollTop: targetOffset
            }, 400);
              return false;
                });
              }
            }
          });
         
          // 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 [];
          }
    });
    

    You can control the speed of the scroll by changing the number in this part of the code:

    $(scrollElem).animate({
        scrollTop: targetOffset
    }, 400);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法