我正在尝试为图像设置 z-index 但它不起作用
I am trying to set the z-index for an image but it's not working
我正在尝试为图像设置 z-index,但它不起作用。实际上,这个位置是绝对的,而且,我已经用这段代码检查了父级的 z-index 以确保 z-index 是 none.
var el = document.getElementById("bear-moving"); // or use [=10=] in chrome;
do {
var styles = window.getComputedStyle(el);
console.log(styles.zIndex);
} while(el.parentElement && (el = el.parentElement));
我的图像根据滚动条移动,需要居中。如果我设置 z-index,它只会在它开始移动之前起作用。
这段代码表示移动图片的JS。
var container = document.getElementsByClassName('elementor-element-192d237');
var image1 = document.getElementById('bear-moving');
var index = 0;
document.addEventListener("scroll", () => {
div = document.querySelector(".elementor-element-192d237");
if(parseInt(div.getBoundingClientRect().top)-300 < 0 && parseInt(div.getBoundingClientRect().bottom)>430) {
var style_top = 0;
while (style_top<window.innerHeight/2-div.getBoundingClientRect().top-25) {
style_top = style_top+5;
image1.style.top = style_top + 'px';
}
}
})
经过几个小时尝试设置 z-index 并猜测问题出在哪里,我发现它是背景透明图像...
我正在尝试为图像设置 z-index,但它不起作用。实际上,这个位置是绝对的,而且,我已经用这段代码检查了父级的 z-index 以确保 z-index 是 none.
var el = document.getElementById("bear-moving"); // or use [=10=] in chrome;
do {
var styles = window.getComputedStyle(el);
console.log(styles.zIndex);
} while(el.parentElement && (el = el.parentElement));
我的图像根据滚动条移动,需要居中。如果我设置 z-index,它只会在它开始移动之前起作用。
这段代码表示移动图片的JS。
var container = document.getElementsByClassName('elementor-element-192d237');
var image1 = document.getElementById('bear-moving');
var index = 0;
document.addEventListener("scroll", () => {
div = document.querySelector(".elementor-element-192d237");
if(parseInt(div.getBoundingClientRect().top)-300 < 0 && parseInt(div.getBoundingClientRect().bottom)>430) {
var style_top = 0;
while (style_top<window.innerHeight/2-div.getBoundingClientRect().top-25) {
style_top = style_top+5;
image1.style.top = style_top + 'px';
}
}
})
经过几个小时尝试设置 z-index 并猜测问题出在哪里,我发现它是背景透明图像...