在EPUB.js中,若是须要实现自动缩放,经过添加smartimages.js就能够实现图片自动缩放了,通过研究smartimages.js,发现,他能够是实现图片的缩放,但只能实现图片比须要显示的空间高时才会把图片缩小,所以,对于比较宽的图片就会出现截断的问题,通过对smartimages.js的修改我实现了经过smartimages.js实现EPUB.js电子书阅读的图片自动缩放。git
EPUB.js的图片缩放是经过EPUB.js的Hooks功能实现的,smartimages.js的第一行代码就是:github
EPUBJS.Hooks.register("beforeChapterDisplay").smartimages
EPUB.js会在每一章数据加载并开始显示是调用该函数。他原来的实现以下:浏览器
EPUBJS.Hooks.register("beforeChapterDisplay").smartimages = function(callback, renderer){ var images = renderer.contents.querySelectorAll('img'), items = Array.prototype.slice.call(images), iheight = renderer.height,//chapter.bodyEl.clientHeight,//chapter.doc.body.getBoundingClientRect().height, oheight; if(renderer.layoutSettings.layout != "reflowable") { callback(); return; //-- Only adjust images for reflowable text } items.forEach(function(item){ function size() { var itemRect = item.getBoundingClientRect(), rectHeight = itemRect.height, top = itemRect.top, oHeight = item.getAttribute('data-height'), height = oHeight || rectHeight, newHeight, fontSize = Number(getComputedStyle(item, "").fontSize.match(/(\d*(\.\d*)?)px/)[1]), fontAdjust = fontSize ? fontSize / 2 : 0; iheight = renderer.contents.clientHeight; if(top < 0) top = 0; if(height + top >= iheight) { if(top < iheight/2) { // Remove top and half font-size from height to keep container from overflowing newHeight = iheight - top - fontAdjust; item.style.maxHeight = newHeight + "px"; item.style.width= "auto"; }else{ if(height > iheight) { item.style.maxHeight = iheight + "px"; item.style.width= "auto"; itemRect = item.getBoundingClientRect(); height = itemRect.height; } item.style.display = "block"; item.style["WebkitColumnBreakBefore"] = "always"; item.style["breakBefore"] = "column"; } item.setAttribute('data-height', newHeight); }else{ item.style.removeProperty('max-height'); item.style.removeProperty('margin-top'); } } item.addEventListener('load', size, false); renderer.on("renderer:resized", size); renderer.on("renderer:chapterUnloaded", function(){ item.removeEventListener('load', size); renderer.off("renderer:resized", size); }); size(); }); if(callback) callback(); }
从上面的代码能够看出,他只计算了高度,若是高度大于iheight,就修改图片的高度样式为特定的高度,并无对高度进行处理。函数
通过分析,EPUB.js能够对图文进行分栏显示,若是我要对宽度超出的显示宽度的图片进行缩放,就须要知道显示区域的宽度。通过浏览器的调试工具和代码搜索,终于发现,EPUB.js的数据显示是经过一个叫作cloumnWidth的样式来决定显示区域的宽度的。工具
var columnWidth = parseInt(item.ownerDocument.documentElement.style[EPUBJS.core.prefixed('columnWidth')]);
这个columnWidth具体的名称是有EPUBJS的一个前缀决定的,多是考虑到浏览器兼容的关系。为了获得图片的document对象,能够经过对象的ownerDocument得到文档对象,因为EPUB.js的显示是经过在内嵌的iframe组件显示的,因此须要获得显示的根文档对象,经过documentElement得到文档的元素,再取得其样式,这个样式经过prefixed的属性取得的,经过上面的代码就能够取到时间显示区域的宽度。spa
如下是代码修改部分(只贴了相关部分):prototype
var columnWidth = parseInt(item.ownerDocument.documentElement.style[EPUBJS.core.prefixed('columnWidth')]); if (item.clientWidth > item.clientHeight){ //land image if (item.clientWidth > columnWidth){ item.style.width = "100%"; item.style.height = "auto"; // recheck clientHeight if (item.clientHeight > top + iheight){ item.style.width = "auto"; item.style.height = "100%"; item.align = "middle"; } }else if (item.clientHeight > top + iheight){ item.style.width = "auto"; item.style.height = "100%"; }else{ item.style.width = "auto"; item.style.height = "auto"; } }else{ // port image if (item.clientHeight > top + iheight){ item.style.width = "auto"; item.style.height = "100%"; //recheck clientWidth if (item.clientWidth > columnWidth){ item.style.width = "100%"; item.style.height = "auto"; } }else if (item.clientWidth > columnWidth){ item.style.width = "100%"; item.style.height = "auto"; }else{ item.style.width = "auto"; item.style.height = "auto"; } } item.style.display = "block"; item.style.marginLeft = "auto"; item.style.marginRight = "auto";
上面的代码就是监测img组件的clientWidth和clientHeight来比较现实区域的高度和宽度的,在经过设定对象的宽度和高度样式。这里须要注意两点:调试
1. 要实现图像的缩放,其实很简单,好比,若是须要把一个图片的宽度设定到区域的100%,那么他的宽度就是显示区域的宽度,而高度设置为auto便可,这样图片的比例就能够获得保证,相反也是能够得,为了美观,我设置了图片居中,,把图片放在中间会比较好看些。注意:设置align为middle是无效的,须要设置display为block,再把marginLeft, marginRight样式修改成auto才能够然图片居中。code
2. EPUB.js在调用该函数的时候,前面一次调用,img的clientWidth和clientHeight为0,后面一次的调用才会是图片的实际大小,为此,我在size函数的前面加上了一句:对象
function size() { // return while item has not client position if (item.clientWidth == 0 && item.clientHeight == 0) return; //... }
为此完美解决了EPUB.js在图片显示时图片裁剪的问题