CSS3無首碼指令碼prefixfree.js與Animatable使用

來源:互聯網
上載者:User

標籤:style   class   blog   code   java   http   

現代瀏覽器基本支援CSS3,但是一些舊版本的瀏覽器還是需要添加首碼的。像box-shadow, border-radius這類屬性,目前較新版本的瀏覽器都是不需要首碼的(包括IE9),但是,有些CSS3屬性,例如漸層,首碼少不了,每次都需要像蓋高樓一樣堆疊CSS3代碼,如:

.bg {        width:400px;        height:177px;        margin:70px auto 0;        padding-top:73px;        position:relative;        background-image:-webkit-linear-gradient(top,rgb(255,255,255),rgb(242,242,242));        background-image:-moz-linear-gradient(top,rgb(255,255,255),rgb(242,242,242));        background-image:-ms-linear-gradient(top,rgb(255,255,255),rgb(242,242,242));        background-image:-o-linear-gradient(top,rgb(255,255,255),rgb(242,242,242));        background-image:linear-gradient(top,rgb(255,255,255),rgb(242,242,242));        box-shadow:0 3px 3px rgba(21,62,78,0.8);}

代碼效果如下,點擊這裡查看示範:

 

那麼如何省去CSS3中首碼“-moz”、“-webkit”、“-o”、“-ms”呢,需要使用prefixfree指令碼來實現。

一,下面說說如何使用prefixfree指令碼:

下面是官方網站:

 

那麼如何使用該指令碼呢?下面是截至官網的兩個DEMO,使用環境為Chrome,所以建議下載最新版的Chrome瀏覽器對示範地址進行瀏覽,希望能對使用提供協助:

有首碼官方DEMO,未使用prefixfree指令碼:

@-webkit-keyframes rotate {    from { -webkit-transform: rotate(15deg) }    to { -webkit-transform: rotate(375deg) }    }.download {   position: absolute;   top: 2em;   left: 1em;   width: 6em;   height: 6em;   padding: 1em 0;   background: #80A060;   background-image: linear-gradient(90deg, transparent, rgba(10,0,0,.3)),linear-gradient(transparent, transparent);   color: white;   line-height: 1;   font-size: 140%;   text-align: center;   text-decoration: initial;   text-shadow: .08em .08em .2em rgba(0,0,0,.6);   border-radius: 50%;   box-shadow: .1em .2em .4em -.2em black;   box-sizing: border-box;   -webkit-transform: rotate(15deg);   -webkit-animation: rotate;   cursor: -webkit-zoom-in;}:read-write {}

在Chrome中的效果如, 如你使用的是最新版的Chrome瀏覽器也可以點擊這裡,查看示範頁面:

 

 

 

 

無首碼官方DEMO,使用prefixfree.js指令碼後能達到同意的效果:

<script src="prefixfree.min.js" type="text/javascript"></script><!--引入prefixfree指令碼--><style>@keyframes rotate {    from { transform: rotate(15deg) }    to { transform: rotate(375deg) }    }.download {   position: absolute;   top: 2em;   left:1em;   width: 6em;   height: 6em;   padding: 1em 0;   background: #80A060;   background-image: linear-gradient(90deg, transparent, rgba(10,0,0,.3)),linear-gradient(transparent, transparent);   color: white;   line-height: 1;   font-size: 140%;   text-align: center;   text-decoration: initial;   text-shadow: .08em .08em .2em rgba(0,0,0,.6);   border-radius: 50%;   box-shadow: .1em .2em .4em -.2em black;   box-sizing: border-box;   transform: rotate(15deg);   animation: rotate;   cursor: zoom-in;}:read-write {}</style>

在Chrome中的效果如, 能達到使用首碼同樣的效果,如你使用的是最新版的Chrome瀏覽器也可以點擊這裡,查看示範頁面:

 

  二,下面介紹Animatable 動畫效果應用,該應用需與prefixfree指令碼一起使用:

話不多說直接上吧:

 

是不是有種百花齊放的感覺呢!

下面是官方示範地址:

http://leaverou.github.io/animatable/

如網頁響應慢的話,可以查看下面的示範的:

http://www.li-cheng.cn/demo/animatable-master/index.html

 

Animatable項目頁面動畫效果的批量實現原理如下:
1. 遍曆頁面上每個含有data-property屬性的a元素;
2. 根據dom元素上的data-property值,form值和to值組裝無首碼的CSS3 animate樣式代碼;
3. 以style節點元素的形式將組裝的CSS代碼append到head標籤中,因頁面調用了prefixfree.js指令碼,於是各個瀏覽器下的animate CSS被渲染,效果呈現。

 

以下是源碼介紹,:

 

function $(expr, con) { return (con || document).querySelector(expr); }function $$(expr, con) { return [].slice.call((con || document).querySelectorAll(expr)); }var css = [];//遍曆頁面中 a標籤中含有 data-property 屬性的所有元素$$(‘a[data-property]‘).forEach(function(el, i){    var property = el.getAttribute(‘data-property‘),        from = el.getAttribute(‘data-from‘),        to = el.getAttribute(‘data-to‘);        var id = property, i = 1;        while(document.getElementById(id)) {        id = property + ‘/‘ + ++i;    }        el.id = id;    el.href = ‘#‘ + id;        el.title = property + ‘ from ‘ + from + ‘ to ‘ + to;        var selector = ‘#‘ + id.replace(/([^\w-])/g, ‘\\$1‘),        ident = id.replace(/([^\w-])/g, ‘-‘);        //建立css3樣式代碼,push到css數組中    css.push(‘@keyframes ‘ + ident + ‘{‘,            ‘from{‘ + property + ‘:‘ + from + ‘}‘,            ‘to{‘ + property + ‘:‘ + to + ‘}}‘,            selector + ‘ { animation: ‘ + ident + ‘ 1s infinite alternate;‘ + property + ‘:‘ + from + ‘}‘);});var style = document.createElement(‘style‘);style.textContent = css.join(‘\r\n‘);StyleFix.styleElement(style);document.head.appendChild(style);  //在頁面中添加 style標籤 setTimeout(onhashchange = function() {    var target = location.hash? $(location.hash.replace(/([^\w-#])/g, ‘\\$1‘)) : null;        if(!target) {        document.body.className = ‘home‘;        return;    }        document.body.className = ‘in-page‘;        var info = $(‘#info‘),        previous = target.previousElementSibling,        next = target.nextElementSibling,        author = target.getAttribute(‘data-author‘) || ‘leaverou‘;        $(‘h1‘, info).innerHTML = target.getAttribute(‘data-property‘);    $(‘dd:first-of-type‘, info).innerHTML = target.getAttribute(‘data-from‘);    $(‘dd:nth-of-type(2)‘, info).innerHTML = target.getAttribute(‘data-to‘);    $(‘dd:nth-of-type(3)‘, info).innerHTML = ‘<a href="http://twitter.com/‘ + author + ‘" target="_blank"><img src="http://twitter.com//api/users/profile_image?screen_name=‘ + author + ‘&size=mini"/>@‘ + author + ‘</a>‘;        $(‘a[title="Previous"]‘, info).setAttribute(‘href‘, ‘#‘ + (previous? previous.id : ‘‘));    $(‘a[title="Next"]‘, info).setAttribute(‘href‘, ‘#‘ + (next? next.id : ‘‘));        setTimeout(function() {        if(2*target.offsetLeft + target.offsetWidth < innerWidth) {            info.style.left = target.offsetLeft + target.offsetWidth + 30 + ‘px‘;        }        else {            info.style.left = target.offsetLeft - info.offsetWidth - 30 + ‘px‘;        }        info.style.top = target.offsetTop + ‘px‘;    }, 10);}, 50);onkeyup = function(evt) {    var key = evt.keyCode;        switch (key) {        case 27:            location.hash = ‘‘;            break;        case 37:        case 38:            location.hash = location.hash? $(‘a[title="Previous"]‘).hash : $(‘a[data-property]:last-child‘).hash;            break;        case 39:        case 40:            location.hash = location.hash? $(‘a[title="Next"]‘).hash : $(‘a[data-property]:first-child‘).hash;    }};

 

文章參考了如下地址,有興趣的可以點擊查看:

http://www.zhangxinxu.com/wordpress/2011/11/css3-prefixfree-js-animatable/

prefixfree官方地址:

http://leaverou.github.io/prefixfree/

Animatable官方地址:

http://leaverou.github.io/animatable/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.