javascript - 具有CSS高度的jQuery调整大小功能

标签 javascript jquery html css resize

我正在尝试将高度设置为其父项所具有的 div。我已经尝试过,当我使用 console.log() 时,它确实有效。但是高度不会在没有刷新的情况下设置.....

这是我的功能

// Set Hight
function fixingHeight() {
    function setHeight() {
        var windowWidth = window.innerWidth;

        var latestPostsWrapperHeight = $('#latestPosts').height();
        var tabPane = $('#latestPosts').find('.tab-pane');
        var tabPaneLists = $('#latestPosts').find('.tab-pane').find('li a');

        var popularPostsWrapperHeight = $('#popularPosts').height();
        var profileWrapper = $('#popularPosts').find('.pofile-wrapper');

        if(windowWidth > 767) {
            $.each(tabPane, function() {
                $(this).height(latestPostsWrapperHeight - 70);
            });

            $.each(tabPaneLists, function() {
                $(this).height((latestPostsWrapperHeight - 70) / 5 - 1);
            });

            $.each(profileWrapper, function() {
                $(this).outerHeight(popularPostsWrapperHeight);
            });
        }

        //console.log(windowWidth);
    }setHeight();

    $(window).resize(function() {
      setHeight();
    });
}fixingHeight();

最佳答案

  • 要处理窗口调整大小事件,请在函数 setHeight() 之外调用函数 $(window).resize
  • 要在第一次加载时调用 setHeight(),请使用 jquery 处理程序 $(document).ready

这是最终代码:

function setHeight() {
    var windowWidth = window.innerWidth;

    var latestPostsWrapperHeight = $('#latestPosts').height();
    var tabPane = $('#latestPosts').find('.tab-pane');
    var tabPaneLists = $('#latestPosts').find('.tab-pane').find('li a');

    var popularPostsWrapperHeight = $('#popularPosts').height();
    var profileWrapper = $('#popularPosts').find('.pofile-wrapper');

    if(windowWidth > 767) {
        $.each(tabPane, function() {
            $(this).height(latestPostsWrapperHeight - 70);
        });

        $.each(tabPaneLists, function() {
            $(this).height((latestPostsWrapperHeight - 70) / 5 - 1);
        });

        $.each(profileWrapper, function() {
            $(this).outerHeight(popularPostsWrapperHeight);
        });
    }
    console.log(windowWidth);
}
$(document).ready(function() {
  setHeight();
});
$(window).resize(function() {
  setHeight();
});

关于javascript - 具有CSS高度的jQuery调整大小功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33541611/

相关文章:

javascript - Backbone 后退按钮滚动问题

javascript - 如何使用 Javascript 制作幻灯片?

jquery - 如何通过 CSS 在滚动时同时更改文本和图像?

javascript - 类型声明适用于对象文字,但不适用于类实现

javascript - 无法处理 jQuery 表单事件

javascript - 根据所选值填充下拉列表

jQuery-File-Upload iframe 后备检测

html - 我应该如何设置文本位置?

java - 引用HTML文件中的servlet以获取Tomcat中的.war文件

javascript - 使用过渡更改宽度时将文本区域 Bootstrap 到视口(viewport)之外