Skip to content

Instantly share code, notes, and snippets.

@hatsumatsu
Created February 17, 2015 17:25
Show Gist options
  • Save hatsumatsu/753097881a64ce73daaf to your computer and use it in GitHub Desktop.
Save hatsumatsu/753097881a64ce73daaf to your computer and use it in GitHub Desktop.
function position() {
var wrapper = $( '.wrapper' );
var image = $( '.image' );
var imageHeight = image.height();
var imageWidth = image.width();
var imageRatio = imageHeight / imageWidth;
var linkHeight = link.height();
var linkWidth = link.width();
var linkRatio = linkHeight / linkWidth;
console.log( 'imageHeight:' + imageHeight );
console.log( 'imageWidth:' + imageWidth );
console.log( 'linkHeight:' + linkHeight );
console.log( 'linkWidth:' + linkWidth );
if( imageRatio > linkRatio ) {
console.log( 'fitWidth' );
var diffRatio = imageWidth / linkWidth;
console.log( 'diffRatio:' + diffRatio );
console.log( 'marginTop: -' + ( ( imageHeight / diffRatio - linkHeight ) / 2 ) ) ;
image
.css( {
'width': linkWidth + 'px',
'height': 'auto',
'marginTop': '-' + ( ( imageHeight / diffRatio - linkHeight ) / 2 ) + 'px',
'marginLeft': ' '
} );
} else {
console.log( 'fitHeight' );
var diffRatio = imageHeight / linkHeight;
image
.css( {
'height': linkHeight + 'px',
'width': 'auto',
'marginTop': ' ',
'marginLeft': '-' + ( ( imageWidth / diffRatio - linkWidth ) / 2 ) + 'px'
} );
}
wrapper
.addClass( 'positioned' );
setTimeout( function() {
image
.css( {
'opacity': 0.999
} )
.css( {
'opacity': ' '
} );
picturefill( {
elements: [ image[0] ],
reevaluate: true
} );
}, 100 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment