javascript - How to Crop image and set with aspect ratio with jquery? -
i have site 2 panel
one left panel menus(left panel size 370px)
and
one right panel content(mostly images) padding left 370px
in right panel want display full images in section
right doing jquery set image height & width on doc ready. it's got images stretch.
so how set images crop , aspect ratio without stretch.
here js code right doc ready
jquery(document).ready(function(){ var windowwidth = $(window).width(); //retrieve current window width var windowheight = $(window).height(); //retrieve current window height var finalwidth = windowwidth - 370; jquery('.rightside > img').css({'height':windowheight,'width':finalwidth}); jquery('.rightside').css('height',windowheight); });
html structure
<div class="rightside"> <img class="project_image" src="" /> <img class="project_image" src="" /> <img class="project_image" src="" /> <img class="project_image" src="" /> <img class="project_image" src="" /> </div>
right using 1920*1080 resolution
and getting right side panel current width & height 1533*924
it sounds can solve pure css - no scripts needed. main trick use div
background-image
style, , background-size: cover
. handles cropping , resizing (no matter viewport size) , maintains aspect ratio.
without seeing more of code, can't give guaranteed solution. if you've got "right panel" filling full viewport height, following work:
<style> .image-container { height: 100%; background-size: cover; background-position: center; } </style> <div class="right-panel"> <div class="image-container" style="background-image: url('/some/image.png');"></div> </div>
note background-image
style added html, not style - way can reuse class on many different images.
Comments
Post a Comment