javascript - Center and crop img tag -


just wondering possible use

<div><img src="xxx.jpg" /></div> 
  1. to center image in browser
  2. list item maintain in center when reduce browser width responsive layout
  3. when browser smaller image "not" reduce image size
  4. 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

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -