SoFunction
Updated on 2025-04-10

JS method to determine whether a scrollbar appears on the page

This article describes the method of JS to determine whether a scroll bar appears on the page. Share it for your reference. The details are as follows:

var isScroll = function (el) {
   // test targets
   var elems = el ? [el] : [, ];
   var scrollX = false, scrollY = false;
   for (var i = 0; i < ; i++) {
     var o = elems[i];
     // test horizontal
     var sl = ;
      += (sl > 0) ? -1 : 1;
      !== sl && (scrollX = scrollX || true);
      = sl;
     // test vertical
     var st = ;
      += (st > 0) ? -1 : 1;
      !== st && (scrollY = scrollY || true);
      = st;
   }
   // ret
   return {
     scrollX: scrollX,
     scrollY: scrollY
   };
 };

I hope this article will be helpful to everyone's JavaScript programming.