简体   繁体   English

使用Wordpress 21主题的浮动导航菜单

[英]Floating navigation menu using Wordpress Twenty Eleven theme

I'm currently developing a site using the WordPress Twenty Eleven theme and I would like to have the main navigation bar stick to the top of the screen once it has been scrolled past, just like the paragraph on the right side of this page http://fiddle.jshell.net/zsJAr/show/light/ . 我目前正在开发一个使用WordPress 21主题的网站,并且希望将主导航栏滚动到屏幕顶部之后,就像此页面右侧的段落http: //fiddle.jshell.net/zsJAr/show/light/

So far I have added code in the header to include jQuery just after the opening head tag: 到目前为止,我已经在标题中添加了代码,以在开始head标签之后添加jQuery:

<?php wp_enqueue_script("jquery"); ?>

And later I have included my javascript before the closing head tag: 后来我在关闭head标签之前加入了我的javascript:

<script type="text/javascript"
src="<?php bloginfo("template_url"); ?>/js/moveScroller.js"></script>

The contents of moveScroller.js is: moveScroller.js的内容为:

var $j = jQuery.noConflict();

$j(window).load(function(){
    $j(function() {
      var a = function() {
        var b = $j(window).scrollTop();
        var d = $j("#access-anchor").offset({scroll:false}).top;
        var c=$j("#access");
        if (b>d) {
          c.css({position:"fixed",top:"0px"})
        } else {
          if (b<=d) {
            c.css({position:"relative",top:""})
          }
        }
      };
      $j(window).scroll(a);a()
    });
});

The "access" and "access-anchor" IDs are declared further down in the following block: 在以下块中进一步声明了“ access”和“ access-anchor” ID:

<div id="access-anchor"></div>
<nav id="access" role="navigation">
            <h3 class="assistive-text"><?php _e( 'Main menu', 'twentyeleven' ); ?></h3>
            <?php /*  Allow screen readers / text browsers to skip the navigation menu and get right to the good stuff. */ ?>
            <div class="skip-link"><a class="assistive-text" href="#content" title="<?php esc_attr_e( 'Skip to primary content', 'twentyeleven' ); ?>"><?php _e( 'Skip to primary content', 'twentyeleven' ); ?></a></div>
            <div class="skip-link"><a class="assistive-text" href="#secondary" title="<?php esc_attr_e( 'Skip to secondary content', 'twentyeleven' ); ?>"><?php _e( 'Skip to secondary content', 'twentyeleven' ); ?></a></div>
            <?php /* Our navigation menu.  If one isn't filled out, wp_nav_menu falls back to wp_page_menu. The menu assiged to the primary position is the one used. If none is assigned, the menu with the lowest ID is used. */ ?>

            <?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>

</nav><!-- #access -->

This seems to be having no effect whatsoever and I'm not really not sure how to go about this. 这似乎没有任何作用,我也不十分确定该怎么做。 With fairly little experience using WordPress, I would really appreciate some help on the matter. 以使用WordPress的经验很少,我真的很感谢在此方面的一些帮助。 Does anyone have any idea how to go about this? 有谁知道如何解决这个问题?

If you are using jQuery, you can also animate the scrolling, looks much more funny =) I used this code a weeks ago, it doesn't use position fixed, it uses margin-top, but you can change it easily: 如果您使用的是jQuery,还可以使滚动动画,看起来更有趣=)我在几周前使用了这段代码,它不使用固定位置,它使用margin-top,但是您可以轻松更改它:

var scroll = 0; //initially scroll is 0
var marginTop = 10; //we add an initial  margin
$(window).scroll(function () {
        //once the user scrolls, we calculate the new margin-top
        marginTop = ($(document).scrollTop() - scroll) + marginTop;
        //and we save the new amount of scroll, for the next time
        scroll = $(document).scrollTop();
        $("#divYouWantToMove").animate({"marginTop": marginTop+"px"}, {duration:500,queue:false} );
});

Hope it helps! 希望能帮助到你!

So it seems like this probably isn't possible or it's far too complicated. 因此,似乎这不可能或太复杂了。 Nobody knows about this on WordPress Stack Exchange or the WordPress forums, so I'm going to have to give up on it :( 在WordPress Stack Exchange或WordPress论坛上没人知道这一点,因此我将不得不放弃:(

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

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