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条)

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败