0

我对此感到非常头疼,我一直试图从这里的其他几个帖子中撤出,但没有运气。我的问题是我希望我的导航在到达页面顶部时停止滚动。目前它将滚动页面。

背景很少,我正在使用wordpress,目前我设置了一个外部脚本文件,它被排入wordpress。使用 php 设置导航以与 wordpress 一起使用。

您可以在www.greenwellgraphics.com查看我的实时网站

这是我的 Header.php 文件:

    <?php
/**
 * The Header template for our theme
 *
 * Displays all of the <head> section and everything up till <div id="main">
 *
 * @package WordPress
 * @subpackage Twenty_Thirteen
 * @since Twenty Thirteen 1.0
 */
?><!DOCTYPE html>
<!--[if IE 7]>
<html class="ie ie7" <?php language_attributes(); ?>>
<![endif]-->
<!--[if IE 8]>
<html class="ie ie8" <?php language_attributes(); ?>>
<![endif]-->
<!--[if !(IE 7) | !(IE 8)  ]><!-->
<html <?php language_attributes(); ?>>
<!--<![endif]-->
<head>
    <meta charset="<?php bloginfo( 'charset' ); ?>">
    <meta name="viewport" content="width=device-width">
    <title><?php wp_title( '|', true, 'right' ); ?></title>
    <link rel="profile" href="http://gmpg.org/xfn/11">
    <link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">
    <!--[if lt IE 9]>
    <script src="<?php echo get_template_directory_uri(); ?>/js/html5.js"></script>
    <![endif]-->
    <?php wp_head(); ?>


</head>

<body <?php body_class(); ?>>
    <div id="page" class="hfeed site">
        <header id="masthead" class="site-header" role="banner">
            <a class="home-link" href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home">
                <h1 class="site-title"><?php bloginfo( 'name' ); ?></h1>
                <h2 class="site-description"><?php bloginfo( 'description' ); ?></h2>
            </a>

            <div id="navbar" class="navbar">
                <nav id="site-navigation" class="navigation main-navigation" role="navigation">
                    <h3 class="menu-toggle"><?php _e( 'Menu', 'twentythirteen' ); ?></h3>
                    <a class="screen-reader-text skip-link" href="#content" title="<?php esc_attr_e( 'Skip to content', 'twentythirteen' ); ?>"><?php _e( 'Skip to content', 'twentythirteen' ); ?></a>
                    <?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu' ) ); ?>
                </nav><!-- #site-navigation -->
            </div><!-- #navbar -->
        </header><!-- #masthead -->

        <div id="main" class="site-main">

这是我正在使用的javascript:

var num = 125; //number of pixels before modifying styles

$(window).bind('scroll', function () {
    if ($(window).scrollTop() > num) {
        $('.navbar').addClass('fixed');
    } else {
        $('.navbar').removeClass('fixed');
    }
});

和CSS:

.navbar {
    background-color: #ffffff;
    border-bottom:1px dashed #CCCCCC;
    border-top:1px dashed #CCCCCC;
    margin: 0 auto;
    max-width: 800px;
    width: 100%;
    text-align:center;
}

.fixed {
    position:fixed;
    top:0;
}

Functions.php 显示我在哪里/如何实现脚本。

function get_Fixed_header() {

    wp_enqueue_script('jquery');

    wp_register_script( 'add-GF-js', get_template_directory_uri() . '/js/test.js'); 

    wp_enqueue_script('add-GF-js');
}

add_action( 'wp_enqueue_scripts' , 'get_Fixed_header' ); //hook to the enqueue function built into wordpress

*编辑了 java 脚本以显示我正在调用 div 类。对不起。

4

1 回答 1

0

当然,您到处寻找,问题就这么简单。

检查您的 jquery 选择器$('navbar'),您正在抓取一个 html 元素,而不是一个 class'ed 或 ID'd 元素。因此,将其修复为$('.navbar')$('#navbar')

编辑:

代替

$(文档).ready(函数(){

利用

jQuery(文档).ready(函数($){

更多信息

http://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_Wrappers

于 2013-11-12T20:46:43.940 回答