<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<div class="photo-card-image-wrapper"><img src="http://th00.deviantart.net/fs71/PRE/i/2013/226/0/6/codepro_poster_by_thrones-d6i66t9.png"></div>
<img src="http://th00.deviantart.net/fs71/PRE/i/2013/226/0/6/codepro_poster_by_thrones-d6i66t9.png">
</body>
</html>
.photo-card-image-wrapper {
overflow: hidden;
}
function cropAndCenterImage(el, size) {
//el = img wrapper
//img = img element
if (size === undefined || size === null) {
size = 154;
}
var $el = $(el);
var $img = $(el + " img");
//console.log($el);
//console.log($img);
$img.width("auto").height("auto");
var imgWidth = $img.width();
var imgHeight = $img.height();
//console.log(imgHeight, imgWidth);
//hopefully that returns the img to it's default height and width by making the inline style to empty quotes
if( imgWidth > imgHeight ) {
//Crop image
//console.log("width greater than height");
if ( imgHeight >= size ) {
//console.log("hit if");
$img.height(size);
imgHeight = $img.height();
imgWidth = $img.width();
$el.height(imgHeight).width(imgHeight);
} else {
//console.log("hit else");
$el.height(imgHeight).width(imgHeight);
$img.height(imgHeight).width(imgWidth);
}
//Center image horizontally
var leftInt = (imgWidth - $el.width()) / 2;
$img.css({ "margin-left": "-" + leftInt + "px", "margin-top": "0" });
} else {
//Crop image
//console.log("height greater than width");
if ( imgWidth >= size ) {
$img.width(size);
imgHeight = $img.height();
imgWidth = $img.width();
$el.height(imgWidth).width(imgWidth);
} else {
$el.height(imgWidth).width(imgWidth);
$img.height(imgHeight).width(imgWidth);
}
imgHeight = $img.height();
//Center image vertically
var topInt = (imgHeight - $el.height()) / 2;
//console.log(topInt);
$img.css({ "margin-top": "-" + topInt + "px", "margin-left": "0"});
}
}
$(function() {
cropAndCenterImage(".photo-card-image-wrapper");
});
Output
300px
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |