Skip to content

Instantly share code, notes, and snippets.

@hitsujixgit
Last active August 29, 2015 14:05
Show Gist options
  • Save hitsujixgit/afa7a5dbded4f956798d to your computer and use it in GitHub Desktop.
Save hitsujixgit/afa7a5dbded4f956798d to your computer and use it in GitHub Desktop.
Adjusting line height according to window width.
$(function($) {
// 投稿用スクリプト. 見出しにぬこクラス追加
$(".post-content h3").addClass("cat-header1").wrapInner("<span></span>");
// 行数カウントして、ライン幅を補正する
function main() {
// ぬこクラス
$("div.post-content .cat-header1 span").css("line-height", "1em");
$('div.post-content .cat-header1 span').each(function(index) {
var header_height = $(this)[0].offsetHeight;
var font_size_str = $(".cat-header1 span").css("font-size");
var rep = parseInt(font_size_str);
var font_size = 25;
if ( rep && rep !== 0 ) font_size = rep;
if ( header_height > font_size+8 ) { //値はCSS設定に合わせる(padding-top+bottom)
$(this).css("line-height", "1.2em");
} else {
$(this).css("line-height", "50px");
}
});
}
// windowサイズが変わったらmain()実行
$( window ).resize(function(){
main();
});
// Page表示時にmain()実行
main();
});
.cat-header1 span {
display: block;
background: url(img/cat_stand_1x.png) right bottom no-repeat;
background: -webkit-image-set( url(img/cat_stand_1x.png) 1x, url(img/cat_stand_2x.png) 2x ) right bottom no-repeat;
padding-right: 50px;
padding-bottom: 8px;
font-size: 25px;
color: #555;
}
.cat-header1 {
background: url(img/green_bar_1x.png) no-repeat bottom left;
background: -webkit-image-set( url(img/green_bar_1x.png) 1x, url(img/green_bar_2x.png) 2x ) no-repeat bottom left;
background-size: 100% 6px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment