Skip to content

Instantly share code, notes, and snippets.

@machida
Created December 1, 2016 01:11
Show Gist options
  • Save machida/bf1bfa8ffe5c9c7827a086325ca84235 to your computer and use it in GitHub Desktop.
Save machida/bf1bfa8ffe5c9c7827a086325ca84235 to your computer and use it in GitHub Desktop.
画像を真ん中にする
$ ->
$('.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