Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all elements with `position:fixed` in an HTML page?

Reason for doing that: I'm debugging css of my webpage.. some elements appeared and they're not supposed to appear. I suspect it is the issue with element positioning.. therefore I want to find these positioned element and check one by one.

like image 273
songyy Avatar asked Jan 28 '16 07:01

songyy


People also ask

How do I keep my position fixed in HTML?

To create a fixed top menu, use position:fixed and top:0 . Note that the fixed menu will overlay your other content. To fix this, add a margin-top (to the content) that is equal or larger than the height of your menu.

How do you do position relative and fixed?

Set everything up as you would if you want to position: absolute inside a position: relative container, and then create a new fixed position div inside the div with position: absolute , but do not set its top and left properties. It will then be fixed wherever you want it, relative to the container.

How do I keep my div position fixed?

A pinned-down menu. The interesting rule here is the ' position: fixed ', that makes the DIV stay fixed on the screen. The ' top: 50% ' and ' right: 0 ' determine where the DIV is displayed, in this case: 50% down from the top of the window, and a constant 0px from the right.


2 Answers

This one is using jQuery. I hope you are find with it.

 var find = $('*').filter(function () { 
        return $(this).css('position') == 'fixed';
    });

I think this one works using a pure javascript:

var elems = document.body.getElementsByTagName("*");
var len = elems.length

for (var i=0;i<len;i++) {

    if (window.getComputedStyle(elems[i],null).getPropertyValue('position') == 'fixed') {
        console.log(elems[i])
    }

}
like image 126
geckob Avatar answered Sep 23 '22 05:09

geckob


Here is an ES6 version that gives you an array of these elements for further processing:

let fixedElements = [...document.body.getElementsByTagName("*")].filter(
    x => getComputedStyle(x, null).getPropertyValue("position") === "fixed"
);
like image 40
Shadow Avatar answered Sep 21 '22 05:09

Shadow