1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-04 15:09:16 +02:00
epub.js/hooks/default/smartimages.js
2013-07-08 22:24:51 -07:00

56 lines
1.4 KiB
JavaScript

EPUBJS.Hooks.register("beforeChapterDisplay").smartimages = function(callback, chapter){
var images = chapter.doc.querySelectorAll('img'),
items = Array.prototype.slice.call(images),
iheight = chapter.height(),//chapter.doc.body.getBoundingClientRect().height,
oheight;
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;
iheight = chapter.height();
if(top < 0) top = 0;
if(height + top >= iheight) {
if(top < iheight/2) {
newHeight = iheight - top;
item.style.maxHeight = newHeight + "px";
item.style.width= "auto";
}else{
height = (height < iheight ? height : iheight);
item.style.maxHeight = newHeight + "px";
item.style.marginTop = iheight - top + "px";
item.style.width= "auto";
}
item.setAttribute('data-height', newHeight);
}else{
item.style.removeProperty('max-height');
item.style.removeProperty('margin-top');
}
}
item.addEventListener('load', size, false);
chapter.on("book:resized", size);
chapter.on("book:chapterDestroyed", function(){
item.removeEventListener('load', size);
chapter.off("book:resized", size);
});
size();
});
if(callback) callback();
}