JavaScript detection Browser zoom State to implement code _JAVASCRIPT skills

Source: Internet
Author: User

Scaling here refers not to the size of the browser, but to the percentage of the content of the browser's Web page (press CTRL and + key or-the zoom of the number key).
There is a way to detect this scaling, QQ space all through flash to detect whether the browser is in the zoom. This provides a JavaScript way to detect browser scaling.
For IE6, it is simply ignored because IE6 can only scale the text.
First of all, the browser provides a standard instrumentation interface, Window.devicepixelratio is the device on the physical pixel and device independent pixel ratio, this property can be used to detect whether the Web page has been scaled. On a normal PC browser, the default value of 1 is the default without scaling. At present, Firefox, Chrome and so on have been very good support.
OK, then let's talk about IE's processing methods. IE provides Window.screen.deviceXDPI and Window.screen.logicalXDPI two attributes, devicexdpi is the physical pixel on the corresponding device, and logicalxdpi is the ratio of independent pixels of the device. The estimated standard detection interface is also only an improvement based on the IE method. The default values for these two properties on systems above Windows xp+ are 96, because the system defaults to 96dpi.
For browsers that are not supported by either of these two properties, you can also take advantage of both Window.outerwidth and window.innerwidth. Outerwidth returns the external actual width of the window element, Innerwidth returns the internal actual width of the window element, which contains the width of the scroll bar.
With these attributes, you can basically handle the browsers that are common on PC browsers. The implementation code is as follows:

The return value of the Detectzoom function, if 100 is the default zoom level, greater than 100 is magnified, and less than 100 is reduced.

function Detectzoom () { 
  var ratio = 0, screen
    = Window.screen,
    ua = Navigator.userAgent.toLowerCase ();

   if (window.devicepixelratio!== undefined) {
      ratio = Window.devicepixelratio;
  }
  else if (~ua.indexof (' MSIE ')) {  
    if (screen.devicexdpi && screen.logicalxdpi) {
      ratio = screen.devicexdpi/screen.logicalxdpi
    }
  }
  else if (window.outerwidth!== undefined && window.innerwidth!== undefined) {
    ratio = Window.outerwidth/wi ndow.innerwidth;
  }
  
   if (ratio) {
    ratio = Math.Round (ratio *);
  }
  
   return ratio;
};

original articles, reproduced please specify: reprinted from the front-end development

Related Article

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.