From 031660abe24299388844e64fd5acbc019a67ca9b Mon Sep 17 00:00:00 2001 From: xieyu33333 Date: Tue, 10 Mar 2015 00:27:38 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9loader,=20=E6=97=A0?= =?UTF-8?q?=E9=9C=80=E4=BC=A0=E5=85=A5height&width?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/islider.js | 28 +++++++++-- build/islider_core.js | 28 +++++++++-- demo/picture/default.html | 87 +++++++++++++++++++--------------- demo/public/js/islider.js | 28 +++++++++-- demo/public/js/islider_core.js | 28 +++++++++-- src/islider_core.js | 31 +++++++++--- 6 files changed, 166 insertions(+), 64 deletions(-) diff --git a/build/islider.js b/build/islider.js index 47161d18..526fb500 100644 --- a/build/islider.js +++ b/build/islider.js @@ -44,9 +44,9 @@ iSlider = function () { this.isOverspread = opts.isOverspread || false; // Play time gap this.duration = opts.duration || 2000; - // start from initIndex or 0 + // start from initIndex or 0 this.initIndex = opts.initIndex || 0; - // touchstart prevent default to fixPage + // touchstart prevent default to fixPage this.fixPage = opts.fixPage || true; if (this.initIndex > this.data.length - 1 || this.initIndex < 0) { this.initIndex = 0; @@ -151,6 +151,11 @@ iSlider = function () { var item; var html; var len = this.data.length; + var self = this; + var insertImg = function () { + html = item.height / item.width > self.ratio ? '' : ''; + el.innerHTML = html; + }; // get the right item of data if (!this.isLooping) { item = this.data[i] || { empty: true }; @@ -170,15 +175,24 @@ iSlider = function () { } if (this.type === 'pic') { if (!this.isOverspread) { - html = item.height / item.width > this.ratio ? '' : ''; + if (item.height & item.width) { + insertImg(); + } else { + var currentImg = new Image(); + currentImg.src = item.content; + currentImg.onload = function () { + item.height = currentImg.height; + item.width = currentImg.width; + insertImg(); + }; + } } else { el.style.background = 'url(' + item.content + ') 50% 50% no-repeat'; el.style.backgroundSize = 'cover'; } } else if (this.type === 'dom') { - html = item.content; + el.innerHTML = item.content; } - html && (el.innerHTML = html); }; /** * render list html @@ -223,6 +237,10 @@ iSlider = function () { if (!self.data[index].loaded) { var preloadImg = new Image(); preloadImg.src = self.data[index].content; + preloadImg.onload = function () { + self.data[index].width = preloadImg.width; + self.data[index].height = preloadImg.height; + }; self.data[index].loaded = 1; } }; diff --git a/build/islider_core.js b/build/islider_core.js index fa4ff45e..cb7ded4f 100644 --- a/build/islider_core.js +++ b/build/islider_core.js @@ -44,9 +44,9 @@ iSlider = function () { this.isOverspread = opts.isOverspread || false; // Play time gap this.duration = opts.duration || 2000; - // start from initIndex or 0 + // start from initIndex or 0 this.initIndex = opts.initIndex || 0; - // touchstart prevent default to fixPage + // touchstart prevent default to fixPage this.fixPage = opts.fixPage || true; if (this.initIndex > this.data.length - 1 || this.initIndex < 0) { this.initIndex = 0; @@ -151,6 +151,11 @@ iSlider = function () { var item; var html; var len = this.data.length; + var self = this; + var insertImg = function () { + html = item.height / item.width > self.ratio ? '' : ''; + el.innerHTML = html; + }; // get the right item of data if (!this.isLooping) { item = this.data[i] || { empty: true }; @@ -170,15 +175,24 @@ iSlider = function () { } if (this.type === 'pic') { if (!this.isOverspread) { - html = item.height / item.width > this.ratio ? '' : ''; + if (item.height & item.width) { + insertImg(); + } else { + var currentImg = new Image(); + currentImg.src = item.content; + currentImg.onload = function () { + item.height = currentImg.height; + item.width = currentImg.width; + insertImg(); + }; + } } else { el.style.background = 'url(' + item.content + ') 50% 50% no-repeat'; el.style.backgroundSize = 'cover'; } } else if (this.type === 'dom') { - html = item.content; + el.innerHTML = item.content; } - html && (el.innerHTML = html); }; /** * render list html @@ -223,6 +237,10 @@ iSlider = function () { if (!self.data[index].loaded) { var preloadImg = new Image(); preloadImg.src = self.data[index].content; + preloadImg.onload = function () { + self.data[index].width = preloadImg.width; + self.data[index].height = preloadImg.height; + }; self.data[index].loaded = 1; } }; diff --git a/demo/picture/default.html b/demo/picture/default.html index c3f8d5bf..866ce4c4 100755 --- a/demo/picture/default.html +++ b/demo/picture/default.html @@ -56,46 +56,57 @@