Vous êtes sur la page 1sur 3

jQuerycheckifhorizontalscrollispresent

BySamDeeringMay19,2012

jQueryfunctiontocheckifhorizontalscrollispresenthasHScrollBar()(orverticalcheckbelowalso,
utilfunctiontocheckifanelementhasascrollbarpresent).

jQueryhasHScrollBar()Function
//utilfunctiontocheckifanelementhasascrollbarpresent

jQuery.fn.hasScrollBar=function(direction)

if(direction=='vertical')

returnthis.get(0).scrollHeight>this.innerHeight();

elseif(direction=='horizontal')

returnthis.get(0).scrollWidth>this.innerWidth();

returnfalse;

//$('#c3.mbcontainercontent').hasScrollBar('horizontal');

Similarfunctions:
//utilfunctiontocheckifanelementhasahorizontalscrollbarpresent
jQuery.fn.hasHScrollBar=function()
{
//log(this.get(0).scrollWidth);
//log(this.width());
//log(this.innerWidth());
returnthis.get(0).scrollWidth>this.innerWidth();
}
$('#c3.mbcontainercontent').hasScrollBar();

//utilfunctiontocheckifanelementhasaverticalscrollbarpresent
jQuery.fn.hasVScrollBar=function()
{
//log(this.get(0).scrollHeight);
//log(this.height());


//log(this.innerHeight());
returnthis.get(0).scrollHeight>this.innerHeight();
}
$('#c3.mbcontainercontent').hasScrollBar();

Anotherversion

functionhasScroll(el,direction){

direction=(direction==='vertical')?'scrollTop':'scrollLeft';

varresult=!!el[direction];

if(!result){

el[direction]=1;

result=!!el[direction];

el[direction]=0;

returnresult;

alert('vertical?'+hasScroll(document.body,'vertical'));

alert('horizontal?'+hasScroll(document.body,'horizontal'));

Vous aimerez peut-être aussi