【问题标题】:Why is my resize event not firing?为什么我的调整大小事件没有触发?
【发布时间】:2014-11-08 21:13:00
【问题描述】:

对于高度大于 920 像素的窗口,我的 DIV #sequence 是浏览器窗口的全高。当它的高度大于 920 像素时,我想启动一个插件,对于小于 920 像素的窗口大小,我希望 #sequence div 成为我的 CSS 文件中设置的高度作为后备。这在页面加载时有效,但是我无法触发 resize 事件,它没有响应并且我在控制台中没有看到任何错误。

这是我的代码主要取自这篇帖子Do something if screen width is less than 960 px

var eventFired = 0;

if ($(window).height() < 920) {

} else {
    fitSequence();  
    eventFired = 1;
}

$(window).on('resize', function() {
    if (!eventFired == 1) {
        if ($(window).height() < 920) {

        } else {
            $(window).resize(function(){ fitSequence(); });
        }
    }
});

仅供参考,这里是我引用的 fitSequence 插件:

function fitSequence(){
var height=$(window).height();

$('#sequence').css('height',height-100+"px");
};

【问题讨论】:

    标签: javascript jquery css resize height


    【解决方案1】:

    如果条件不正确:

    if (!eventFired == 1) {
    

    应该是:

    if (eventFired != 1) {
    

    在函数部分,这是代码模棱两可的部分:

    $('#sequence').css('height',height-100+"px");
    

    应该是:

    $('#sequence').css('height',(height-100)+"px");
                               ^^ add brackets 
    

    【讨论】:

    • 解决了大部分问题,感谢您的指点!当您将窗口大小调整回较小的高度时,我忘记了在不常见的情况下重置 div 的高度。我通过在我的 styles.css 底部使用媒体查询来解决这个问题:@media screen and (max-height : 920px) { #sequence { height: 600px !important; } }
    猜你喜欢
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 2012-01-11
    • 1970-01-01
    相关资源
    最近更新 更多