本文實例講述了JS判斷頁面是否出現滾動條的方法。分享給大家供大家參考。具體如下:
var isScroll = function (el) { // test targets var elems = el ? [el] : [document.documentElement, document.body]; var scrollX = false, scrollY = false; for (var i = 0; i < elems.length; i++) { var o = elems[i]; // test horizontal var sl = o.scrollLeft; o.scrollLeft += (sl > 0) ? -1 : 1; o.scrollLeft !== sl && (scrollX = scrollX || true); o.scrollLeft = sl; // test vertical var st = o.scrollTop; o.scrollTop += (st > 0) ? -1 : 1; o.scrollTop !== st && (scrollY = scrollY || true); o.scrollTop = st; } // ret return { scrollX: scrollX, scrollY: scrollY }; };
希望本文所述對大家的javascript程序設計有所幫助。