繁体   English   中英

动画锚链接-WordPress的[重复]

[英]animating an anchor link - Wordpress [duplicate]

这个问题已经在这里有了答案:

我在Wordpress网站中设置了一个锚链接,该链接从导航到页脚。

在此处输入图片说明

HTML-footer.php

<footer id="footer-anchor">

  <div class="row"> 

    ...

选择链接时,页面将“跳至”页脚。 我希望它动画到页脚。 类似于页面如何使用“返回顶部”按钮动画到顶部,但是相反。

Page-Scroll-To-ID插件如何? 它在我的WordPress网站上运行良好。 我们可以在几分钟之内轻松地在管理页面上进行设置,然后就完成了。 该插件是您要寻找的。 请检查此链接, Page-Scroll-To-ID插件教程

更新:如果您不想使用插件,请按照以下步骤操作。 我们将仅使用纯jQuery插件,而且令人惊讶的是,该代码的工作原理很吸引人,看起来很简单!

1.准备WordPress主题

用一些类来包装您的菜单,我们稍后将使用该类。 例如:

<nav id='scrollNav'>
   <?php wp_nav_menu(array('theme_location'  => 'your-menu-location', 'container'=>false, 'depth'=>1) ?>
</nav>

然后将id添加到您的特定元素,但是您已经将id添加到您的footer元素中,即#footer-anchor

2.对您的JavaScript进行编码,我们将仅使用jQuery

(function($){
   $("#scrollNav").find("a").click(function(){
       var $targetElm = $($(this).attr("href"));
       $('html,body').animate({scrollTop: $targetElm.offset().top},
    'slow');
   });
})(jQuery)

3.排队您的脚本(以WordPress的方式包括您的脚本)

function your_scripts_method() {
    wp_enqueue_script(
        'your-script',
        get_stylesheet_directory_uri() . '/js/your_script.js',
        array( 'jquery' )
    );
}

add_action( 'wp_enqueue_scripts', 'your_scripts_method' );

4.再次运行您的网站

恭喜!!!

我喜欢使用Karl Swedberg的这段代码,通常在</body>标记之前,在我的footer.php文件中使用<script></script>将其包括在内,也可以将其放入您的functions.php文件中并具有它加载在页脚中。 我喜欢这段代码,因为一旦您单击锚点,它就会从URL中删除#hash。

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 [];
      }
});

您可以通过更改代码这一部分中的数字来控制滚动的速度:

$(scrollElem).animate({
    scrollTop: targetOffset
}, 400);

暂无
暂无

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

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