diff --git a/jquery.visible.js b/jquery.visible.js index 745956a..de59394 100644 --- a/jquery.visible.js +++ b/jquery.visible.js @@ -11,7 +11,7 @@ * only accounts for vertical position, not horizontal. */ var $w = $(window); - $.fn.visible = function(partial,hidden,direction){ + $.fn.visible = function(partial,hidden,direction,pv){ if (this.length < 1) return; @@ -21,16 +21,24 @@ vpWidth = $w.width(), vpHeight = $w.height(), direction = (direction) ? direction : 'both', - clientSize = hidden === true ? t.offsetWidth * t.offsetHeight : true; + clientSize = hidden === true ? t.offsetWidth * t.offsetHeight : true, + pvh = 0, + pvw = 0; + + if (pv > 0) { + pvh = $t.height()*pv/100; + pvw = $t.width()*pv/100; + } + if (typeof t.getBoundingClientRect === 'function'){ // Use this native browser method, if available. var rec = t.getBoundingClientRect(), - tViz = rec.top >= 0 && rec.top < vpHeight, - bViz = rec.bottom > 0 && rec.bottom <= vpHeight, - lViz = rec.left >= 0 && rec.left < vpWidth, - rViz = rec.right > 0 && rec.right <= vpWidth, + tViz = (rec.top + pvh) >= 0 && (rec.top + pvh) < vpHeight, + bViz = (rec.bottom - pvh) > 0 && (rec.bottom - pvh) <= vpHeight, + lViz = (rec.left + pvw) >= 0 && (rec.left + pvw) < vpWidth, + rViz = (rec.right - pvw) > 0 && (rec.right - pvw) <= vpWidth, vVisible = partial ? tViz || bViz : tViz && bViz, hVisible = partial ? lViz || rViz : lViz && rViz; @@ -56,6 +64,11 @@ compareLeft = partial === true ? _right : _left, compareRight = partial === true ? _left : _right; + compareTop = compareTop + pvh; + compareBottom = compareBottom - pvh; + compareRight = compareRight - pvw; + compareLeft = compareLeft + pvw; + if(direction === 'both') return !!clientSize && ((compareBottom <= viewBottom) && (compareTop >= viewTop)) && ((compareRight <= viewRight) && (compareLeft >= viewLeft)); else if(direction === 'vertical')