Home > Web Front-end > JS Tutorial > body text

A very simple piece of js to determine the browser's core_javascript skills

WBOY
Release: 2016-05-16 16:39:31
Original
1207 people have browsed it

Everyone should still remember how to write JavaScript inline styles, right? (Looks like I’m talking nonsense!)

During the front-end development process, sometimes we need to determine the browser’s kernel prefix and handle different browsers differently, so we can do this.

 alert(element.style.webkitTransition); This is to get the transition value prefixed by webkit. But if the browser is not prefixed by webkit, undefined will be returned. And we can enumerate all the kernel prefixes, and then get the value of one of its CSS to make a judgment. The code is as follows:

function getVendorPrefix() {
  // 使用body是为了避免在还需要传入元素
  var body = document.body || document.documentElement,
    style = body.style,
    vendor = ['webkit', 'khtml', 'moz', 'ms', 'o'],
    i = 0;

  while (i < vendor.length) {
    // 此处进行判断是否有对应的内核前缀
    if (typeof style[vendor[i] + 'Transition'] === 'string') {
      return vendor[i];
    }
    i++;
  }
}

Copy after login

Then you only need to call getVendorPrefix() to know the browser’s kernel prefix. If undefined is returned, it proves that the browser does not support CSS3 attributes, that is, there is no kernel prefix.

Everyone should know that when we write code, we can write CSS instead of JavaScript. After all, the performance of CSS will be higher than writing JS by ourselves. Therefore, we will use transition in developing some practical applications. , for example, for a simple image carousel, we can use CSS3 transition, jQuery animate or write native code ourselves, but the performance of CSS3 will definitely be higher, so we can write two sets of codes. For browsers that support CSS3 If it is not supported, use animation, and if it is not supported, use timer or animate. In this way, a better user experience can be obtained.

The above is the plug-in experience of jquery.slides.js. If there is a better method, please inform the author.

Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!