Javascript jQuery和scroll需要重新加载才能工作
出于某种原因,当我们开始滚动时,应该添加到我的网站的类在我点击“刷新”或右键单击>检查元素之前不起作用 我猜这与我的JavaScript代码(下面的代码)有关,但我不确定出了什么问题 我的测试页面可在以下位置获得:Javascript jQuery和scroll需要重新加载才能工作,javascript,wordpress,Javascript,Wordpress,出于某种原因,当我们开始滚动时,应该添加到我的网站的类在我点击“刷新”或右键单击>检查元素之前不起作用 我猜这与我的JavaScript代码(下面的代码)有关,但我不确定出了什么问题 我的测试页面可在以下位置获得: 当页面登陆时执行滚动事件时,window\u width值为undefined 试着使用这个修改过的代码 jQuery(document).ready(function($) { /* window */ var window_height;
当页面登陆时执行滚动事件时,
window\u width
值为undefined
试着使用这个修改过的代码
jQuery(document).ready(function($) {
/* window */
var window_height;
/* admin bar */
var adminbar = $('#wpadminbar');
var adminbar_height = 0;
/* header menu */
var header = $('#site-navigation');
var header_top = 0;
/* scroll status */
var scroll_status = '';
/**
* window load event.
*/
$(window).on('load', function() {
/** current window height */
window_height = $(window).height();
/* get admin bar height */
adminbar_height = adminbar.length > 0 ? adminbar.outerHeight(true) : 0 ;
/* get top header menu */
header_top = header.length > 0 ? header.offset().top - adminbar_height : 0 ;
/* check sticky menu. */
sticky_menu();
});
/**
* resize event.
*/
$(window).on('resize', function(event, ui) {
/** current window height */
window_height = $(window).height();
/* check sticky menu. */
sticky_menu();
});
/**
* scroll event.
*/
$(window).on('scroll', function() {
/* check sticky menu. */
sticky_menu();
});
/**
* Sticky menu
*/
function sticky_menu( window_width ) {
scroll_top = $(window).scrollTop();
window_width = $(window).width();
console.log(window_width);
console.log(scroll_top);
if ( $('#site-navigation').hasClass('main-navigation') && scroll_top > 0 && window_width > 991) {
header.addClass('header-fixed');
} else {
header.removeClass('header-fixed');
}
}
});
我跟踪了代码,发现在页面登陆时执行滚动事件时,
window\u width
值是undefined
,因此请确保在sticky\u menu()
函数中获取此变量的值。
jQuery(document).ready(function($) {
/* window */
var window_height;
/* admin bar */
var adminbar = $('#wpadminbar');
var adminbar_height = 0;
/* header menu */
var header = $('#site-navigation');
var header_top = 0;
/* scroll status */
var scroll_status = '';
/**
* window load event.
*/
$(window).on('load', function() {
/** current window height */
window_height = $(window).height();
/* get admin bar height */
adminbar_height = adminbar.length > 0 ? adminbar.outerHeight(true) : 0 ;
/* get top header menu */
header_top = header.length > 0 ? header.offset().top - adminbar_height : 0 ;
/* check sticky menu. */
sticky_menu();
});
/**
* resize event.
*/
$(window).on('resize', function(event, ui) {
/** current window height */
window_height = $(window).height();
/* check sticky menu. */
sticky_menu();
});
/**
* scroll event.
*/
$(window).on('scroll', function() {
/* check sticky menu. */
sticky_menu();
});
/**
* Sticky menu
*/
function sticky_menu( window_width ) {
scroll_top = $(window).scrollTop();
window_width = $(window).width();
console.log(window_width);
console.log(scroll_top);
if ( $('#site-navigation').hasClass('main-navigation') && scroll_top > 0 && window_width > 991) {
header.addClass('header-fixed');
} else {
header.removeClass('header-fixed');
}
}
});