Javascript 居中导航固定从滚动时在页面顶部
我对这件事感到非常头痛,我一直在尝试从其他几个帖子上撤下来,但运气不好。我的问题是,我希望我的导航在到达页面顶部时停止滚动。当前它将从页面上滚动 小背景,, 我正在使用wordpress,目前我已经设置了一个外部脚本文件,并将其排入wordpress。导航是使用php与wordpress一起设置的 您可以在以下位置查看我的实时站点: 这是我的Header.php文件:Javascript 居中导航固定从滚动时在页面顶部,javascript,php,html,css,wordpress,Javascript,Php,Html,Css,Wordpress,我对这件事感到非常头痛,我一直在尝试从其他几个帖子上撤下来,但运气不好。我的问题是,我希望我的导航在到达页面顶部时停止滚动。当前它将从页面上滚动 小背景,, 我正在使用wordpress,目前我已经设置了一个外部脚本文件,并将其排入wordpress。导航是使用php与wordpress一起设置的 您可以在以下位置查看我的实时站点: 这是我的Header.php文件: <?php /** * The Header template for our theme * * Dis
<?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">
和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类。对不起。当然你到处都在找,问题很简单 检查jquery选择器
$('navbar')
,您将获取一个html元素,而不是class'ed或ID'd元素。因此,将其固定到$('.navbar')
或$('#navbar')
编辑:
而不是
$(文档).ready(函数(){
使用
jQuery(文档).ready(函数($){
更多信息
您是否正在其他地方加载jQuery,因为它似乎没有包含在PHP文件中?我正在使用wordpress,它内置了jQuery,我正在functions.PHP文件中排队,我将用该代码和到live站点的链接更新主要帖子。我尝试了,但没有成功,不管是什么原因,它仍然会滚动浏览导航栏元素。感谢您的快速回复。您是否有网站的实时URL,以便我们可以查看?看,可能有很多问题。您的js代码中可能有另一个错误,或者您没有使用“无冲突wordpress jqury包装器”:-D,等等…是的,我在上面添加了它,还有更多的代码,显示了我是如何调用jquery的。这个网站是成功的!但是现在我遇到了一个问题,它跳到页面的左侧而不是居中…有什么想法吗?我不确定它为什么会丢失边距样式…你也没有设置水平位置w在css中使用
右
或左
。好的做法是,设置左:50%
和左:菜单栏宽度的一半%
,也可以使用右关键字
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