Determining whether the browser supports CSS3 has initially explored JS inertia loading

Source: Internet
Author: User

Determining whether the browser supports CSS3 has initially explored JS inertia loading

Share a function that determines whether the browser supports it, And then involves the concept of lazy loading.

 

var iscss3=(function(){            var _style=document.createElement(div).style;            return 'transition' in _style||'mozTransition' in _style||'webkitTransition' in _style;        })()


What is inert loading? Like JavaScript and CSS, many browser attributes and methods are incompatible and require a large number of branches for compatibility.

For example, bind common events:

 

function addEvent (type, element, fun) {    if (element.addEventListener) {        element.addEventListener(type, fun, false);    }    else if(element.attachEvent){        element.attachEvent('on' + type, fun);    }    else{        element['on' + type] = fun;    }}

Every time we run it, we need to make branch judgments, so the performance will not be high. If this is the case, use an anonymous function in combination with a function expression. It will become lazy.

 

 

var addEvent = (function () {    if (document.addEventListener) {        return function (type, element, fun) {            element.addEventListener(type, fun, false);        }    }    else if (document.attachEvent) {        return function (type, element, fun) {            element.attachEvent('on' + type, fun);        }    }    else {        return function (type, element, fun) {            element['on' + type] = fun;        }    }})();

In this way, the correct function will be assigned to the variable once it is executed.

 

 

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.