javascript - Viewport always centralized on a bigger background image -
it possible make viewport behave css and/or javascript/jquery:
currently, i'm using css below make background cover whole viewport:
#background { width: 100%; height: 100%; top:0; left:0; position: absolute; -webkit-background-size: cover !important; -moz-background-size: cover !important; -o-background-size: cover !important; background-size: cover !important; overflow: hidden; }
but that's not need. using way, on low screen resolutions background image distort , looks real bad.
edit clear, i'm not concerned mobile users. that's not purpose of system i'm working @ at first. desktop system.
thanks everybody.
try this...
#background { background: url('image/myimage.jpg') center center; }
or
body { background: url('image/myimage.jpg') center center; }
if can set ground @ body level there no need #background element.
but setting image using css not scale image , "center center" center image vertically , horizontally.
regardless of image size center it. however, have provide image larger largest resolution support "over-scan" style have represented.
however, if wanted stretch background image can following. require use of javascript , jquery library.
html:
<div id="divbackwrapper"> <div id="divbackcenter"> <img src="http://www.abc.net.au/radionational/image/4220698-3x2-700x467.jpg" width="700" height="467" /> </div> </div>
css:
html, body { width: 100%; height: 100%; padding:0; margin:0; } #divbackwrapper, #divbackcenter { position: absolute; width: 100%; height: 100%; overflow: hidden; } #divbackcenter { overflow: visible; top: 50%; left: 50%; } #divbackwrapper img { position: absolute; width: 100%; /* change auto allow width set height, 1 must 100% */ height: auto; /* change 100% allow height stretching */ min-width: 700px; min-height: 467px; top: -50%; /* half height */ left: -50%; /* half width */ }
js:
jquery(function() { $(window).resize(function() { var $window = $(window); var $img = $("#divbackcenter img"); if ( $img.width() <= $window.width() ) { $("#divbackcenter").removeattr("style"); $("#divbackcenter img").removeattr("style"); return; } else { var css = {}; css.top = -($img.height() - $window.height()) / 2; css.left = -($img.width() - $window.width()) / 2; $("#divbackcenter").css({top:0,left:0}); $("#divbackcenter img").css(css); } }); });
Comments
Post a Comment