頁面效果是很重要的一環,好的頁面效果,能夠讓用戶感受很舒服,進而能吸引更多的用戶。
既然是頁面效果,那麼動畫確定不可或缺,那麼這篇先說下 transition 這個 css3 屬性。css
transitions 提供了一種在更改 CSS 屬性時控制動畫速度的方法。其能夠讓屬性變化成爲一個持續一段時間的過程,而不是當即生效 的。(摘自 MDN)
其寫法(爲了兼容,要加廠商前綴,這裏不寫咯):html
transition: <property> <duration> <timing-function> <delay>
也能夠用逗號隔開,多寫幾個做用於不用的 property(屬性)css3
transition: <property> <duration> <timing-function> <delay>, <property> <duration> <timing-function> <delay>
@ | 值 | 默認值 | 做用 |
---|---|---|---|
property | none / all / property | all | 做用於須要過渡的屬性 |
duration | time | 0 | 過渡所需時間 |
timing-function | linear / ease / ease-in / ease-out / ease-in-out / cubic-bezier(n,n,n,n) | ease | 規定過渡效果的速度曲線 |
delay | time (同duration) | 0 | 延遲多久纔開始過渡 |
1s
)或毫秒(如1000
)計。// css #box { width: 100px; height: 100px; background-color: aqua; transition: all 1s; -webkit-transition: all 1s; -moz-transition: all 1s; -ms-transition: all 1s; -o-transition: all 1s; } #box:hover { width: 500px; margin-top: 100px; } // html <div id="box"></div>
#box
的 style 裏有transition
,要觸發它,則須要改變上面所說的 property 。div
時,width
和 marin-top
發生了變化,觸發了 transition
。duration
。過渡所需時間。// css * { margin: 0; padding: 0; } #box { margin: 0 auto; width: 100px; height: 100px; overflow: hidden; } #img_container { position: relative; width: 400px; transition: all .7s linear; -webkit-transition: all .7s linear; -moz-transition: all .7s linear; -ms-transition: all .7s linear; -o-transition: all .7s linear; } #img_container span { display: inline-block; width: 100px; height: 100px; text-align: center; line-height: 100px; background-color: yellow; } #img_container span:nth-of-type(even) { background-color: red; } ul { text-align: center; } li { padding: 10px; cursor: pointer; list-style: none; display: inline-block; } // html <div id="box"> <section id="img_container"> <span>1</span><span>2</span><span>3</span><span>4</span> </section> </div> <ul> <li>圖一</li> <li>圖二</li> <li>圖三</li> <li>圖四</li> </ul> // js ~function () { let lis = document.getElementsByTagName('li'); let imgBox = document.getElementById('img_container'); let width = +document.querySelector('span').clientWidth; for (let j = 0;j < lis.length;j++) { lis[j].addEventListener('click',function() { imgBox.style.marginLeft = `-${ width * j }px`; }); } }()
transition
。// css * { margin: 0; padding: 0; } body { height: 1500px; } #box { height: 1200px; text-align: center; } #animation { width: 100px; height: 100px; background-color: red; transition: transform 1s cubic-bezier(.1,1.92,.71,.53); -webkit-transition: transform 1s cubic-bezier(.1,1.92,.71,.53); -moz-transition: transform 1s cubic-bezier(.1,1.92,.71,.53); -ms-transition: transform 1s cubic-bezier(.1,1.92,.71,.53); -o-transition: transform 1s cubic-bezier(.1,1.92,.71,.53); } // html <div id="box"> 請往下滾動到有動畫的地方,或者點擊 <a href="#here">這裏</a> </div> <span id="here"></span> <div id="animation"></div> // js let animation = function () { let animationBox = document.getElementById('animation'); function show () { animationBox.style.transform = 'translateX(600px) scale(3,3) rotate(360deg)'; } function init () { animationBox.style.transform = 'translateX(0) scale(1,1) rotate(-360deg)'; } return { show, init }; }() window.onscroll = function () { let scrollTop = +window.scrollY; if (scrollTop > 650) { animation.show(); } else { animation.init(); } }
transition
要經過 transform
的變化來觸發。同時我也用到了 cubic-bezier 。這裏不深刻講解 cubic-bezier ,由於我看原理也是頭暈。。。。 transition
還能夠實現不少功能,淡出淡入 , 手風琴效果 等等。等着大家去發現。
其實動畫不是特別難的,只要你有顆 騷動(好奇)的心。git