預(yù)加載頁(yè)無(wú)效
為什么我的加載頁(yè)沒(méi)效果,直接重0就跳過(guò)去了,調(diào)試看數(shù)組也遍歷過(guò)了,就是沒(méi)執(zhí)行觸發(fā)load事件后的代碼
<!DOCTYPE?html> <html> <head> ????<meta?charset="UTF-8"> ????<title>圖片預(yù)加載之無(wú)序加載</title> ????<script?src="../js/jquery.min.1.7.js"></script> ????<style> ????????html,body{ ????????????height:?100%; ????????} ????????.box{ ????????????text-align:?center; ????????} ????????.btn,.btn:link{ ????????????display:?inline-block; ????????????text-decoration:?none; ????????????height:?30px; ????????????line-height:?30px; ????????????border:?1px?#cccccc?solid; ????????????background:?#ffffff; ????????????padding:?0px?10px; ????????????margin-right:?50px; ????????????color:?#333333; ????????} ????????.btn:hover{ ????????????background:?#dddddd; ????????} ????????.loading{ ????????????position:?fixed; ????????????top:0; ????????????left:?0px; ????????????width:?100%; ????????????height:?100%; ????????????background:?#eeeeee; ????????????text-align:?center; ????????????font-size:?30px; ????????} ????????.progress{ ????????????margin-top:?300px; ????????} ????</style> </head> <body> ????<div> ????????<p>0</p> ????</div> ????<div> ????????<img?id="img"?src="../imgs/1.jpg"?alt="pic"?height="800"?width="960"/> ????????<p> ????????????<a?href="#"?data-control="prev">上一頁(yè)</a> ????????????<a?href="#"?data-control="next">下一頁(yè)</a> ????????</p> ????</div> ????<script> ????????var?imgs?=?['../imgs/1.jpg','../imgs/2.jpg','../imgs/3.jpg','../imgs/4.jpg','../imgs/5.jpg']; ????????var?now?=?0; ????????var?len?=?imgs.length; ????????var?count?=?0; ????????$progress?=?$('.progress'); ????????$('.btn').on('click',function?()?{ ????????????if($(this).data('control')?==='prev'){ ????????????????/*now先自減1再和0比較?若小于0則賦給now*/ ???????????????now?=?Math.max(0,--now); ????????????}else?if($(this).data('control')?==='next'){ ????????????????now?=?Math.min(len-1,++now); ????????????} ????????????document.title?=?(now+1)+"/"+len ????????????$('#img').attr('src',imgs[now]); ????????}); ????????$.each(imgs,function?(i,src)?{ ????????????/*圖片加載完會(huì)觸發(fā)load事件,出錯(cuò)觸發(fā)error事件*/ ????????????var?imgObj?=?new?Image(); ????????????/*綁定load事件*/ ????????????$(imgObj).on('load',function?()?{ ????????????????$progress.html(Math.round(count+1/len?*?100)+'%'); ????????????????if(count>=len-1){ ????????????????????$('.loading').hide(); ????????????????????document.title='1/'+len; ????????????????} ????????????????count++; ????????????}); ????????????imgObj.src?=?src; ????????}); ????</script> </body> </html>
2017-05-10
$('#img').attr('src',imgs[now]);和imgObj.src?=?src; ? 前者是從數(shù)組中獲取圖片的路徑,后者又是干什么啦……
2017-04-27
問(wèn)題差不多想清楚了,我的圖片太大,有的瀏覽器不顯示,有的從緩存拿還需要一段時(shí)間