Three ways to make the site background automatically adapted to each browser size _ reprint

Source: Internet
Author: User

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.

  1. <head>
  2. ....
  3. <script type="Text/javascript" src="Js/jquery.js"></script>
  4. <script type="Text/javascript">
  5. $ (document). Ready (function () {
  6. $ ("Img.source-image"). Hide ();
  7. var $Source = $ ("Img.source-image"). attr ("src");
  8. $ (' #page-body '). CSS ({
  9. ' backgroundimage ': ' url (' + $source + ') ',
  10. ' backgroundrepeat ': ' No-repeat ',
  11. ' backgroundposition ': ' Top center '
  12. });
  13. });
  14. </Script>
  15. </head>
  16. <body id="Page-body">
  17. <img class="source-image" src="images/image.jpg" alt= " />
  18. </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.

  1. <script type="Text/javascript" src="/js/jquery.js"></script>
  2. <script type="Text/javascript" src="/js/jquery.dimensions.js"></ script>
  3. <script type="Text/javascript">
  4. $ (document). Ready (function () {
  5. var $winwidth = $ (window). width ();
  6. $ ("Img.source-image"). attr ({
  7. Width: $winwidth
  8. });
  9. $ (window). Bind ("Resize", function () {
  10. var $winwidth = $ (window). width ();
  11. $ ("Img.source-image"). attr ({
  12. Width: $winwidth
  13. });
  14. });
  15. });
  16. </Script>

To make this picture more like a background image, we set:

    1. 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:

    1. 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.

    1. #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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.