The element.style.prop
deals with the inline
styles of an element, only. It is not affected by the changing of embedded
or external
styles (more information here). Thus, you cannot use it to get the value of a non-inline
property.
Therefore, you need to use this :-
const myDiv = document.querySelector('.my-div');
if (myDiv && window.getComputedStyle(myDiv).getPropertyValue('position') == 'fixed') {
myDiv.style.top = '60px';
}
Refer them from here: getComputedStyle
and getPropertyValue
.
Or even more simply, you could simply use the .
or the []
notation to get the values. You can check out the differences here.
const myDiv = document.querySelector('.my-div');
if (myDiv && window.getComputedStyle(myDiv).position == 'fixed') {
myDiv.style.top = '60px';
}