JavaScript---Mobile--page adaptive

Source: Internet
Author: User

In the company newly learned a page adaptive way:

//equal ratio scaling!(function (Doc, win) {varTimer, Docele= Doc.documentelement,//Get BodyEVT ="Onorientationchange" inchWindow?"Orientationchange":"Resize",//Get phone rotation eventsSetfontsize =function () {varwidth =docele.getboundingclientrect (). width; Width&& (docEle.style.fontSize = -* (Width/ the) +"px");    };        Win.addeventlistener (evt, function () {cleartimeout (timer); Timer= SetTimeout (Setfontsize, -); }, false); Doc.addeventlistener ("domcontentloaded", Setfontsize,false);//to be compatible with Firefox, this event is executed after all DOM has been loaded} (document, window));

Looking carefully, a bunch of things do not understand, so do a few searches, now listed below:

1.document.documentelement (citation: http://www.cnblogs.com/scy251147/archive/2011/04/10/2011420.html)

Document.body and Document.documentelement's district.

The location of the fixed layer may often be used when designing the page, which requires the coordinates of some HTML objects to be more flexible in setting the coordinates of the target layer, which may be used Document.body.scrollTop attributes, but this property is in the XHTML standard Web page or, more simply, with <! DOCTYPE. The result of the > label page is 0, if you do not have this tag is all right, then how to get the body coordinates of the XHTML page, of course there is a way-use document.documentelement to replace Document.body, Can write like this
Example:
var top = Document.documentElement.scrollTop | | document.body.scrollTop;
in JavaScript | | is a good thing , in addition to can be used in the if and other conditional judgment, but also can be used in variable assignment. Then the example above is equivalent to the following example.
Example:
var top = document.documentElement.scrollTop? document.documentElement.scrollTop:document.body.scrollTop;
This can be very good compatibility.

Conversely, if you don't make a statement, DOCUMENT.DOCUMENTELEMENT.SCROLLTOP will show up as 0 instead.

Save this picture by the way, it's always hard to remember:

  

2.Onorientationchange (citation: Http://zhidao.baidu.com/link?url=UhRfuvKnkaGAy-c9Neke_bwZ5hn_ 4S5MUQA5OP5IRKCUKRMWS4CRZLMJ5KYSZMPH7CMUOUIQVG4Q90NQZA-JPQ)

Get mobile screen rotation events, according to many of the online data I see, most mobile browsers support this event, if not supported, you can use the original resize

 <script type= text/javascript   " >var  evt =  " onorientationchange   " in  window?  " orientationchange  : "  resize        ; Window.addeventlistener (evt, function () {alert (   screen rotation    false  );  </script> 

3. Width && (docEle.style.fontSize = * (width/320) + "px");

It means:

if (width) {
Docele.style ...
}
That is, width! = null or undefined or 0

... Back add

JavaScript---Mobile--page adaptive

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.