修复了在页面顶部滚动时居中导航的问题
centered navigation fixed from scrolling when at the top of the page
我对此感到非常头疼,我一直试图从这里的其他几个帖子中提取,但没有运气。我的问题是我希望我的导航在到达页面顶部时停止滚动。目前它将滚动页面。
小背景,我正在使用wordpress,目前我设置了一个外部脚本文件,它被排队到wordpress中。导航是使用PHP设置的,以便与WordPress一起使用。
您可以在以下位置查看我的实时站点www.greenwellgraphics.com
这是我的标头.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;
}
函数.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 类。 对不起。
当然,你到处找,问题很简单。
检查你的jquery选择器$('navbar')
,你正在抓取一个html元素,而不是一个类或ID元素。因此,将其修复为$('.navbar')
或$('#navbar')
。
编辑:
而不是
$(document).ready(function(){
用
jQuery(document).ready(function($){
更多信息
http://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_Wrappers
相关文章:
- pager.js的导航问题
- winjs应用程序中的导航问题
- 平滑滚动并突出显示导航问题
- 粘性导航-JS Waypoints在浏览器调整大小时出现自定义粘性导航问题
- Javascript/Jquery超级Scrollorama导航问题
- j查询滑块导航问题
- 带有页面导航问题的 HTML5 滑块
- Ionic:带有选项卡和侧菜单导航问题的主详细信息
- Javascript 修复了导航问题
- 使用 Twitter 引导程序 3 的网站出现 IE11 导航问题
- URI 路径导航问题
- 引导侧菜单导航问题
- 如何使用jquery-mobile解决页面导航问题
- JSSOR的项目符号导航问题
- 引导程序滑块旋转木马与缩略图导航问题
- Multiscroll.js的导航问题
- JavaScript ES6侧导航问题时,函数调用
- Backbone.js主从视图,导航问题(包括jsfiddle)
- jQuery AJAX导航问题
- 两个PHP递归导航问题