简体   繁体   English

如何使用 JavaScript 来查找元素是否固定了 position?

[英]How can I use JavaScript to find if an element has position fixed?

Using JavaScript I need to find an element that has position fixed and set its top attribute to 60px.使用 JavaScript 我需要找到一个固定了 position 的元素并将其 top 属性设置为 60px。

const myDiv = document.querySelector('.my-div')
if(myDiv && myDiv.style.position == 'fixed') {
    myDiv.style.top = '60px';
}

But it doesn't work.但它不起作用。 What is wrong in my code?我的代码有什么问题?

The element.style.prop deals with the inline styles of an element, only. element.style.prop仅处理元素的inline styles。 It is not affected by the changing of embedded or external styles (more information here ).它不受embeddedexternal styles 更改的影响(此处有更多信息)。 Thus, you cannot use it to get the value of a non-inline property.因此,您不能使用它来获取non-inline属性的值。

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 .从这里引用它们: getComputedStylegetPropertyValue

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';
}

You can use window.getComputedStyle() .您可以使用window.getComputedStyle()

It will return an object with all styles, even if not explicitly defined by you, unlike the style property which only holds the style values which you explicitly set.它会返回一个 object 和所有styles,即使您没有明确定义,这与仅包含您明确设置的样式值的style属性不同。

For your code, it would look like this:对于您的代码,它看起来像这样:

const myDiv = document.querySelector('.my-div')
if (!myDiv) return; // Early return guard to avoid calling getComputedStyle with null
const style = window.getComputedStyle(myDiv)

if (style.position === 'fixed') {
    myDiv.style.top = '60px';
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript:如何找到元素在多维数组中的位置 - Javascript:How can I find the position of element in the multi-dimensional array 如何使用javascript获取“固定”定位元素的包含块? - How can I get the containing block of a "fixed" positioned element with javascript? 我如何获得具有由Javascript设置的固定位置的DIV,以使其保留在包含的DIV中? - How do I get this DIV which has fixed position set by Javascript to stay within the containing DIV? 如何使用具有“position:fixed”元素的代码段中的代码? 我无法让它留在我的内心 <div> 身高(父母) - how can I use a code from a snippet which have a “position:fixed” element ? I can't make it stay inside my <div>'s height (parent) 如何找出容器元素内的文字位置? - How can I find out text position inside container element? 如何使用jQuery在元素页面中找到位置? - How can I find the position in the page of an element using jQuery? 如何将JavaScript与mdl-layout--fixed-tabs结合使用 - How can I use JavaScript with mdl-layout--fixed-tabs 如何在中心元素旁边保持位置:固定元素 - How do I keep a position:fixed element next to the center element 如果我的javascript数组的值包含两个元素,如何使用一个元素而不使用另一个元素? - If my javascript array has values that have two elements how can I use one element without the other? 如何使用JavaScript查找被单击的元素(或具有背景图像的最近的子元素或父元素)的背景图像? - How can I find the background image of a clicked element (or the nearest child or parent element that has a background image) using JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM