javascript - Center and crop img tag -
just wondering possible use
<div><img src="xxx.jpg" /></div> - to center image in browser
- list item maintain in center when reduce browser width responsive layout
- when browser smaller image "not" reduce image size
- instead crop image (overflow:hidden) scrollbar still maintain image center...
is possible using img tag this?? know can perform background image using css.
but since i'm working on cms site , image uploaded client side, background image not solution....
i think point 3 , 4 make impossible css have set scroll position using css, best can come using css is: jsfiddle , fullscreen demo.
body { text-align: center; } div { max-width: 100%; } img { margin: 0 -100% 0 -100%; } this approach keeps image in center @ times, , kind of doesn't reduce image size, because i've had use negative margins, can't scroll invisible parts view.
instead going js approach way: jsfiddle , fullscreen demo.
css
body { text-align: center; } div { width: 100%; overflow-x: auto; } js
$(window).resize(function () { $("div").scrollleft(($("img").width() / 2) - ($(this).width() / 2)) }); this approach uses jquery ensure center of image scrolls view whenever browser window re-sized.
Comments
Post a Comment