Original address: http://www.daimajiayuan.com/sitejs-12661-1.html
Due to the different display resolution, the same background image may appear in different resolutions with only part or part of the blank.
Effect: No matter how you change the size of the browser window, the background map will always automatically adjust the size.
1. The first type:
The first idea is to use a CSS background map, and if the picture is large enough, it will fill the entire screen. When the browser window size does not have a large picture, it will automatically hide the extra parts.
- <head>
- ....
- <script type="Text/javascript" src="Js/jquery.js"></script>
- <script type="Text/javascript">
- $ (document). Ready (function () {
- $ ("Img.source-image"). Hide ();
- var $Source = $ ("Img.source-image"). attr ("src");
- $ (' #page-body '). CSS ({
- ' backgroundimage ': ' url (' + $source + ') ',
- ' backgroundrepeat ': ' No-repeat ',
- ' backgroundposition ': ' Top center '
- });
- });
- </Script>
- </head>
- <body id="Page-body">
- <img class="source-image" src="images/image.jpg" alt= " />
- </body>
The above code can automatically hide the extra portions of the picture (image.jpg) that are outside the browser window section, without causing the browser to produce scrollbars.
2. The second type:
The effect of the first experiment did not meet the requirements, not really make the background image to fit the window size, you should use the setting "width" and "height" property to control the size of the picture, if we can get the browser window display pixels, we can use this number to control the image size, while maintaining proportions.
These parameters can be obtained using jquery and dimensions plugin.
- <script type="Text/javascript" src="/js/jquery.js"></script>
- <script type="Text/javascript" src="/js/jquery.dimensions.js"></ script>
- <script type="Text/javascript">
- $ (document). Ready (function () {
- var $winwidth = $ (window). width ();
- $ ("Img.source-image"). attr ({
- Width: $winwidth
- });
- $ (window). Bind ("Resize", function () {
- var $winwidth = $ (window). width ();
- $ ("Img.source-image"). attr ({
- Width: $winwidth
- });
- });
- });
- </Script>
To make this picture more like a background image, we set:
- Img.source-image {Position: absolute;top: 0;left: 0;}
Because the background image with the positioning code, that should be added to the background image of anything needs to be positioned, if your background image is vertical bar (particularly high), and your browser window is particularly wide, it is easy to cause the background image height beyond the height of your window, in order to prevent this situation, You need to set the hidden portion of the excess:
- Body {overflow: hidden;}
3. Third time Method (best):
Stu Nicholls version gives the best method (see NetEase's homepage is also doing so), this processing method does not need any JavaScript can be perfect to achieve the purpose.
- #img. source-image {width:100%;p osition:absolute;top:0;left:0;}
The effect of this method is to set the percentage of the background image width directly in the CSS. This method is the best, and does not require any JavaScript.
In the background map settings, select the last method, the code is as follows:
<div id= "Layer1" style= "Position:absolute; width:100%; height:100%; Top:0;left:0;z-index:-1 ">
</div>
Three ways to make the site background automatically adapted to each browser size _ reprint