diff --git a/README.md b/README.md index 200c1450..69562d19 100755 --- a/README.md +++ b/README.md @@ -30,19 +30,11 @@ The best way to learn the iSlider is by looking at the demos. In the archive you Before you start, you need to prepare some data for iSlider: ```` -var data = [{ - height: 414, - width: 300, - content: "imgs/1.jpg", -},{ - height: 414, - width: 300, - content: "imgs/2.jpg", -},{ - height: 414, - width: 300, - content: "imgs/3.jpg", -}]; +var data = [ +{content: "imgs/1.jpg"}, +{content: "imgs/2.jpg"}, +{content: "imgs/3.jpg"} +]; ```` HTML structure you only need to prepare is : @@ -80,16 +72,10 @@ Besides the basic ways you can do with iSlider, you can customized the features ```` var data = [{ - 'height' : '100%', - 'width' : '100%', 'content' : '

Home

This is home page

home is pretty awsome

' },{ - 'height' : '100%', - 'width' : '100%', 'content' : '

Page1

This is page1

page1 is pretty awsome

' },{ - 'height' : '100%', - 'width' : '100%', 'content' : '

Page2

This is Page2

Page2 is pretty awsome

' }]; ```` @@ -132,8 +118,6 @@ Here provides a clear description of what options you are able to manipulate: Picture data, for example:
 [{
-	height: 377,
-	width: 600,
 	content:"pics/1.jpg"
 }]
 		
diff --git a/README_Chinese.md b/README_Chinese.md index ae95f3dc..f2925804 100755 --- a/README_Chinese.md +++ b/README_Chinese.md @@ -26,19 +26,11 @@ iSlider是一个表现出众,无任何插件依赖的手机平台javascript滑 使用iSlider只需要准备好数据即可,无论是本地数据还是从接口获取的数据,例如: ``` javascript -var data = [{ - height: 414, - width: 300, - content: "imgs/1.jpg", -},{ - height: 414, - width: 300, - content: "imgs/2.jpg", -},{ - height: 414, - width: 300, - content: "imgs/3.jpg", -}]; +var data = [ +{content: "imgs/1.jpg"}, +{content: "imgs/2.jpg"}, +{content: "imgs/3.jpg"} +]; ``` HTML代码如下: @@ -76,16 +68,10 @@ That's it. ``` javascript var data = [{ - 'height' : '100%', - 'width' : '100%', 'content' : '

Home

This is home page

home is pretty awsome

' },{ - 'height' : '100%', - 'width' : '100%', 'content' : '

Page1

This is page1

page1 is pretty awsome

' },{ - 'height' : '100%', - 'width' : '100%', 'content' : '

Page2

This is Page2

Page2 is pretty awsome

' }]; ``` @@ -126,8 +112,6 @@ var data = [{ 若是图片数据,格式如下:
 [{
-	height: 377,
-	width: 600,
 	content:"pics/1.jpg"
 }]
 		
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/card.html b/demo/picture/card.html index f8986cef..dcad3105 100755 --- a/demo/picture/card.html +++ b/demo/picture/card.html @@ -127,40 +127,14 @@