在两个不同的函数中使用jQuery变量
在两个不同的函数中使用此变量时遇到问题。首先,我计算要查看的元素(leavespace变量)从窗口顶部开始所需的偏移量。然后,我有两个不同的函数需要利用它,但我不能让它工作在两个不同的函数中使用jQuery变量,jquery,Jquery,在两个不同的函数中使用此变量时遇到问题。首先,我计算要查看的元素(leavespace变量)从窗口顶部开始所需的偏移量。然后,我有两个不同的函数需要利用它,但我不能让它工作 jQuery(document).ready(function($) { $(function() { var topbar_o_height = $(".ipro_topbar").outerHeight(); var nav_o_height = $(".nav").outerHeight(); if
jQuery(document).ready(function($) {
$(function() {
var topbar_o_height = $(".ipro_topbar").outerHeight();
var nav_o_height = $(".nav").outerHeight();
if (topbar_o_height > 0) {
if ($('.nav').hasClass('menu-fixed-topbar')) {
var spacing = 10;
} else {
var spacing = 30;
}
} else {
var spacing = 10;
}
var extra_height = topbar_o_height + nav_o_height + spacing;
var leavespace = $(this.hash).offset().top - extra_height; // leave space for top menu
} // end function
$(".link_scroll").click(function(event){
event.preventDefault();
$('html,body').animate({scrollTop:leavespace}, 'slow');
});
if (document.location.href.indexOf('#') > -1 ) {
// will use the leavespace variable here too...
}
}); // End doc ready
您需要更改leavespace变量的scape,尝试在ready函数中声明它:
jQuery(document).ready(function($) {
var leavespace;
$(function() {
var topbar_o_height = $(".ipro_topbar").outerHeight();
var nav_o_height = $(".nav").outerHeight();
if (topbar_o_height > 0) {
if ($('.nav').hasClass('menu-fixed-topbar')) {
var spacing = 10;
} else {
var spacing = 30;
}
} else {
var spacing = 10;
}
var extra_height = topbar_o_height + nav_o_height + spacing;
leavespace = $(this.hash).offset().top - extra_height; // leave space for top menu
} // end function
$(".link_scroll").click(function(event){
event.preventDefault();
// you can now access leavespace here
$('html,body').animate({scrollTop:leavespace}, 'slow');
});
if (document.location.href.indexOf('#') > -1 ) {
// will use the leavespace variable here too...
/// you can now access leavespace here
}
}); // End doc ready
在这种情况下,您主要可以使用两种情况
jQuery(document).ready(function($) {
var leavespace='';
$(function() {
var topbar_o_height = $(".ipro_topbar").outerHeight();
var nav_o_height = $(".nav").outerHeight();
if (topbar_o_height > 0) {
if ($('.nav').hasClass('menu-fixed-topbar')) {
var spacing = 10;
} else {
var spacing = 30;
}
} else {
var spacing = 10;
}
var extra_height = topbar_o_height + nav_o_height + spacing;
leavespace = $(this.hash).offset().top - extra_height; // leave space for top menu
} // end function
$(".link_scroll").click(function(event){
event.preventDefault();
$('html,body').animate({scrollTop:leavespace}, 'slow');
});
if (document.location.href.indexOf('#') > -1 ) {
// will use the leavespace variable here too...
}
}); // End doc ready
或
使用窗口。leavespace
也可以解决您的问题
谢谢
为什么同时具有两个dom就绪函数。您可以保留document.ready并删除$(function(){因为这两个函数都意味着sme,这样,leavespace将具有作用域,并且可以在函数中看到。因此,您的代码应该如下所示
jQuery(document).ready(function($) {
var topbar_o_height = $(".ipro_topbar").outerHeight();
var nav_o_height = $(".nav").outerHeight();
if (topbar_o_height > 0) {
if ($('.nav').hasClass('menu-fixed-topbar')) {
var spacing = 10;
} else {
var spacing = 30;
}
} else {
var spacing = 10;
}
var extra_height = topbar_o_height + nav_o_height + spacing;
var leavespace = $(this.hash).offset().top - extra_height; // leave space for top menu
$(".link_scroll").click(function(event){
event.preventDefault();
$('html,body').animate({scrollTop:leavespace}, 'slow');
});
if (document.location.href.indexOf('#') > -1 ) {
// will use the leavespace variable here too...
}
}); // End doc ready
使用
window.leavespace=
而不是var leavespace=
另一个选项是将函数外部的leavespace声明为紧挨着jQuery(document).ready(函数($)下面的全局变量{
。目前它不在范围内,这就是其他函数无法看到它的原因。可能的重复我尝试了@Aguardietico和delliottg的注释,但都不起作用。请尝试在animate语句之前打印leavespace的值,algo尝试在leavespace滚动顶部:leavespace+“px”之后添加“px”