Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  <div>
    <div id="frm">
      <div id="mask">
        <img class="imgage" src="http://wp-content.webtrunghieu.info/uploads/2013/03/WTH-MS-Charmbar-destop.jpg" />
      </div>
      
    </div>
  </div>
</body>
</html>
 
#mask {
  background: rgba(0, 0, 0, 0.5);
  border: 2px solid red;
  position: relative;
}
#mask img {
  position: absolute;
}
 
function scaleimage(img){
    img.style.height = 'auto';
    img.style.width = 'auto';
   //Getting the image hight and width
    var imgW = img.clientWidth;
    var imgH = img.clientHeight;
   //Getting the mask hight and width
    var maskH = 400;
    var maskW = 600;
    var scaleH = maskH / imgH;
    var scaleW = maskW / imgW;
   // Scaling
    if (scaleH < scaleW) {
        img.style.height = maskH + "px";
        var newW = Math.round(imgW * scaleH);
        img.style.width = newW + "px";
      img.style.left = Math.round((maskW - newW) / 2) + "px";
    } else {
        img.style.width = maskW + "px";
      var newH = Math.round(imgH * scaleW);
        img.style.height = newH + "px";
      img.style.top = Math.round((maskH - newH) / 2) + 'px';
}
    
}
$(document).ready(function(){
  $('#mask').width(600).height(400);
  scaleimage($('#mask img')[0]);
});
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
trunghieulepro
0viewers