Find element that is causing the showing of horizontal scrollbar in Google Chrome

后端 未结 5 1492
天命终不由人
天命终不由人 2020-12-22 22:46

When I size my Chrome window to 328 x 455 pixels I still see a horizontal scrollbar. How can I find out which element is causing this? I\'ve been looking elements via the de

5条回答
  •  半阙折子戏
    2020-12-22 23:23

    There is an excellent article by Chris Coyier which explain everything you need about this problem.

    after reading this article, I personally use this code in my console to find out which element cause vertical scroll:

    press F12 in your Browser then choose console and paste the below code there and press enter:

    var all = document.getElementsByTagName("*"), i = 0, rect, docWidth = document.documentElement.offsetWidth;
    for (; i < all.length; i++) {
        rect = all[i].getBoundingClientRect();
        if (rect.right > docWidth || rect.left < 0){
            console.log(all[i]);
            all[i].style.borderTop = '1px solid red';
        }
    }
    

    Update:
    if the above code didn't work it might be an element inside an iframe that make the page to vertically scroll. in this case you can search within the iframes using this code:

    var frames = document.getElementsByTagName("iframe");
    for(var i=0; i < frames.length; i++){
       var frame = frames[i];
       frame = (frame.contentWindow || frame.contentDocument);
       var all = frame.document.getElementsByTagName("*"),rect,
           docWidth = document.documentElement.offsetWidth;
       for (var j =0; j < all.length; j++) {
           rect = all[j].getBoundingClientRect();
           if (rect.right > docWidth || rect.left < 0){
               console.log(all[j]);
               all[j].style.borderTop = '1px solid red';
           }
       }
    }
    

提交回复
热议问题