Skip to content

Instantly share code, notes, and snippets.

@greycode
Created July 14, 2014 02:28
Show Gist options
  • Select an option

  • Save greycode/a04c370f6180ae309d18 to your computer and use it in GitHub Desktop.

Select an option

Save greycode/a04c370f6180ae309d18 to your computer and use it in GitHub Desktop.
根据窗口大小自动改变元素高度
/**
* 将具有 autoheight 属性的 div 元素设置为自动高度
* 用法:给需要的 div 元素添加 autoheight 属性,如:<div autoheight> ... </div>
* 可以修改选择符,如写为 ".autoheight" 或是其它的以匹配需要的元素。
*/
$(function () {
var _jahDivs = $("#textMsg");
if (_jahDivs.length > 0) {
_jahDivs.css("overflow", "auto");
$(window).resize(function () {
var _addHeight = $(window).height() - $("body").outerHeight(true);
var _height = _jahDivs.height();
_jahDivs.height(_height + _addHeight - (_jahDivs.outerHeight(true) - _height) / 2);
}).resize();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment