jQuery设置可变高度动画

jQuery设置可变高度动画,jquery,variables,Jquery,Variables,我想在折叠上方有一个按钮,点击时会显示“向下滚动!”。我希望它能使整个浏览器的高度向下移动。因为人们有不同的屏幕分辨率。我想使用一个变量。然而,这是完全不可能的 下面的代码可以工作,但仅在设置高度为800px时有效 相反,我需要它来处理var hhheight 我整晚都在搜索stackoverflow,只找到了几条线索,没有真正的解决方案 这是有效的: function jqUpdateSize(){ // Get the dimensions of the viewport v

我想在折叠上方有一个按钮,点击时会显示“向下滚动!”。我希望它能使整个浏览器的高度向下移动。因为人们有不同的屏幕分辨率。我想使用一个变量。然而,这是完全不可能的

下面的代码可以工作,但仅在设置高度为800px时有效

相反,我需要它来处理
var hhheight

我整晚都在搜索stackoverflow,只找到了几条线索,没有真正的解决方案

这是有效的:

function jqUpdateSize(){
    // Get the dimensions of the viewport
    var width = jQuery(window).width();
    var hheight = jQuery(window).height();

    jQuery('#jqWidth').html(width);
    jQuery('#jqHeight').html(hheight);
};
jQuery(document).ready(jqUpdateSize);    // When the page first loads
jQuery(window).resize(jqUpdateSize);     // When the browser changes size

jQuery(document).ready(function() {
    jQuery(".my-btn").click(function(event){
    jQuery('html, body').animate({scrollTop:'+=' + 800 + 'px'}, '800');
    });
});
以下内容不起作用:

jQuery('html, body').animate({scrollTop:'+=' + hheight+ 'px'}, '800');

希望有人知道如何处理此问题。

您需要在
ready()
resize()中使用匿名函数。

所以

否则,将永远不会调用
jqUpdateSize
函数,并且变量
hhheight
中没有任何内容

最佳做法是将其全部放在
文档中。ready()

像这样:

jQuery(document).ready(function() {
    jqUpdateSize();
    jQuery(window).resize(function(){jqUpdateSize();});
    jQuery(".my-btn").click(function(event){
        jQuery('html, body').animate({scrollTop:'+=' + hheight + 'px'}, '800');
    });
});
这工作很好--

尝试移动这个
jQuery(窗口)。调整大小(jqUpdateSize)
ready

应该是这样的

jQuery(document).ready(function() {
    jQuery(window).resize(jqUpdateSize);
    jQuery(".my-btn").click(function(event){
    jQuery('html, body').animate({scrollTop:'+=' + 800 + 'px'}, '800');
    });
});

嗨,谢谢你们都是对的,今天早上在我的程序员的帮助下,我们想到了这个:啊。。无法在注释中粘贴代码,但他看到我的var不是全局的。因此是空的。与parseInt(HHHeight)一起,我们使其工作:)您可以在www.onlinebrand.dk上有效地使用该功能
var hheight = jQuery(window).height();
jQuery('html, body').animate({scrollTop:'+=' + hheight+ 'px'}, '800');
jQuery(document).ready(function() {
    jQuery(window).resize(jqUpdateSize);
    jQuery(".my-btn").click(function(event){
    jQuery('html, body').animate({scrollTop:'+=' + 800 + 'px'}, '800');
    });
});