上一家公司主要是作些微信的場景頁啥的,而後須要快速開發,因此第一時間選擇了swiper,畢竟開發快速,比較方便而後api多(雖然並無用不少),用這個的朋友。缺點的話有時候用了就曉得,下面開始講講,第一次寫,有啥不對的請你們指正,虛心學習。javascript
這邊swiper的使用發法能夠去官網看看,傳送門:http://www.swiper.com.cnhtml
通常來講,H5場景開始有個loading吧,就建一個div,全屏的大小,能夠加個loading動畫啥的,而後這就用到了預加載,您能夠選擇一個合適的預加載js,好比relLoader.js。也能夠本身寫一個方法java
function _LoadImg(b, e) {
var c = 0,
a = {},
d = 0;
for (src in b) {
d++;
}
for (src in b) {
a[src] = new Image();
a[src].onload = function() {
if (++c >= d) {
e(a)
}
};
a[src].src = b[src];
}
}android
_LoadImg([
'images/1-beijing.jpg',
'images/2-beijing.jpg',
], ios
function() {
var loader = document.getElementById('loading');
document.body.removeChild(loader);
});web
下方是swiper動畫的js,官網拷貝下來的api
<script>微信
var mySwiper = new Swiper ('.swiper-container', { iphone
onInit: function(swiper){ //Swiper2.x的初始化是onFirstInitide
swiperAnimateCache(swiper); //隱藏動畫元素
swiperAnimate(swiper); //初始化完成開始動畫 },
onSlideChangeEnd: function(swiper){
swiperAnimate(swiper); //每一個slide切換結束時也運行當前slide動畫
}
})
</script>
接下來你發現loading已經結束了,可是加在首屏的動畫爲啥都不動了,其實人家在你loading的時候已經加載完成了,因此動畫已經執行了。
解決辦法很簡單的
setTimeout(function(){
swiperAnimate(swiper);
},1000);//初始化完成開始動畫
設置一個延遲,看看效果,這回就能夠了。
解決辦法呢:
就是預加載的時候加載少許的圖片
開啓swiper的懶加載lazyLoading設爲true啊,這時html裏面也要修改<img data-src="path/to/picture-1.jpg" class="swiper-lazy">
swiper無法加兩個動畫,舉個栗子:你要div入場一個動畫,而後入場動畫執行完了這個div再循環執行一個小動畫,這個swiper無法幫你哦,你要這個有個啥swiper.animate-twice.min.js的,能夠去嘗試一下,但我以爲很差使。
解決辦法:事件監聽,監聽這個div,第一個動畫執行完成,而後給加上一個循環的動畫,因此有這個方法
var youxiu = document.getElementById("youxiu");
function setAni(obj,type,duration,delay,cName){
obj.addEventListener("webkitAnimationEnd",function(){
this.setAttribute("swiper-animate-effect",type);
this.setAttribute("swiper-animate-duration",duration);
this.setAttribute("swiper-animate-delay",delay);
this.className = cName;
swiperAnimate(mySwiper);
},false);
}
setAni(youxiu,"pulses","2s","0.3s","full swiper-lazy ani");
還有不少問題能夠一塊兒討論,發現寫這個也不是件簡單的事情,水平有限哈,請多多指教。祝你們工做順利!!