Created
December 1, 2016 01:11
-
-
Save machida/bf1bfa8ffe5c9c7827a086325ca84235 to your computer and use it in GitHub Desktop.
画像を真ん中にする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ -> | |
$('.js-article-eyecatch-image').on('load', -> | |
eyeCatchWidth = $(this).width() | |
eyeCatchHeight = $(this).height() | |
eyeCatchWrapperWidth = $(this).parent().width() | |
eyeCatchWrapperHeight = $(this).parent().height() | |
minEyeCatchWrapperHeight = eyeCatchWrapperHeight / eyeCatchWrapperWidth | |
minEyeCatchWrapperWidth = eyeCatchHeight / eyeCatchWidth | |
if eyeCatchWidth > eyeCatchHeight | |
# 横長画像 | |
if minEyeCatchWrapperHeight > minEyeCatchWrapperWidth | |
$(this).css('height', '100.1%') | |
else | |
$(this).css('width', '100.1%') | |
else | |
# 縦長画像 | |
if minEyeCatchWrapperWidth > minEyeCatchWrapperHeight | |
$(this).css('width', '100.1%') | |
else | |
$(this).css('height', '100.1%') | |
# thisにcssでサイズ変更しているのでサイズを再取得 | |
fixedEyeCatchWidth = $(this).width() | |
fixedEyeCatchHeight = $(this).height() | |
$(this).css('margin-left', fixedEyeCatchWidth / -2) | |
$(this).css('margin-top', fixedEyeCatchHeight / -2) | |
).each -> | |
if @complete | |
$(this).load() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment