gpt4 book ai didi

javascript - 使前景透明,但背景不透明

转载 作者:太空宇宙 更新时间:2023-11-04 15:46:58 25 4
gpt4 key购买 nike

我正在做一些实验,我设置了以下挑战,但我现在无法解决。我正在为 Flyspray 错误跟踪器制作用户脚本。在问题列表中,有一列指示完成百分比:

enter image description here

该系统相当古老,这赋予了它一定的简洁之美。这是进度的 HTML:

<td class="task_progress">
<img src="/themes/Bluey/percent-70.png" alt="70%">
</td>

简洁明了。现在我想做的是将鼠标移动的进度移到 <img> 上方, 无需更改 HTML。如果我可以使 ` 前景半透明并设置 CSS 渐变背景,就可以做到这一点。

我可以通过包装 <img> 来解决这个问题在<span> , 但它非常笨拙。

const wrapped = document.querySelector("#wrapped");
const img = wrapped.querySelector("img");
const box = img.getBoundingClientRect();
//wrapped.style.height = (box.bottom-box.top)+"px";

const bg = "linear-gradient(to right, rgba(0,38,114,1) 0%,rgba(0,38,114,1) 50%,rgba(0,38,114,0) 51%,rgba(0,38,114,0) 100%)";
console.log(wrapped);
wrapped.addEventListener("mousemove", (e)=>{
const box = wrapped.getBoundingClientRect();
const mousePos = e.clientX - box.left;
const max = box.right-box.left;
const perc = 100*(mousePos/max);

const gradient = bg
.replace("50%", (perc-0.5)+"%")
.replace("51%", (perc+0.5)+"%");
//console.log(gradient);
///console.log(perc);
wrapped.style.backgroundImage = gradient;
wrapped.style.color = "red";
}, {capture: false});
#wrapped {
display: inline-block;
margin: 0px;
padding: -1px;
border-width: 1px;
border-color:transparent;
border-style: solid;

position: relative;

background-position: center center;

background-repeat: no-repeat;

background-size: 0px 0px;
}
#wrapped img {
margin: 0px;
padding: 0px;

position: relative;

top:0px;
left: 0px;
}
#wrapped:hover img {
opacity: 0.3;
}
#wrapped:hover {
/*border-color:#002672;*/
background-size: 100% 8.82px;
}
<span id="wrapped"><img src="/image/lymku.png" /></span>

上述解决方案的主要问题:包装器的高度与 <img> 不匹配高度,这意味着背景尺寸必须精确设置为一个像素。

enter image description here

有没有办法做到这一点没有任何包装元素?

请注意,这是一个练习/学习类的问题,通过其他方式解决整个问题的解决方案对我没有用。

最佳答案

不要使用透明色的渐变。仅使用纯色并控制 background-size 然后简单地制作 img block 元素以避免空白问题:

不确定没有包装器是否可行,因为您将需要一个必须应用渐变的元素:

const wrapped = document.querySelector("#wrapped");
const img = wrapped.querySelector("img");
const box = img.getBoundingClientRect();
//wrapped.style.height = (box.bottom-box.top)+"px";

const bg = "linear-gradient(rgba(0,38,114,1),rgba(0,38,114,1))";
console.log(wrapped);
wrapped.addEventListener("mousemove", (e)=>{
const box = wrapped.getBoundingClientRect();
const mousePos = e.clientX - box.left;
const max = box.right-box.left;
const perc = 100*(mousePos/max);

//console.log(gradient);
///console.log(perc);
wrapped.style.backgroundSize =perc+"% 100%";
wrapped.style.color = "red";
}, {capture: false});
#wrapped {
display: inline-block;
margin: 0px;
/*padding: -1px; there is no negative padding */
border-width: 1px;
border-color:transparent;
border-style: solid;

background-position:left;
background-repeat: no-repeat;
}
#wrapped img {
display:block;
}
#wrapped:hover img {
opacity: 0.3;
}
#wrapped:hover {
/*border-color:#002672;*/
background-image:linear-gradient(rgba(0,38,114,1),rgba(0,38,114,1));
background-size: 50% 100%;
}
<span id="wrapped"><img src="/image/lymku.png" /></span>

但是您可能会考虑一个具有多个背景的想法来避免图像,并在末尾使用一个元素来替换图像:

const wrapped = document.querySelector("#wrapped");


wrapped.addEventListener("mousemove", (e)=>{
const box = wrapped.getBoundingClientRect();
const mousePos = e.clientX - box.left;
const max = box.right-box.left;
const perc = 100*(mousePos/max);

wrapped.style.backgroundSize =perc+"% 100%, 60% 100%";
}, {capture: false});
#wrapped {
display: inline-block;
border-width: 1px;
border-style: solid;
border-color:rgba(0,38,114,1);
height:10px;
width:100px;
background-image:
linear-gradient(transparent,transparent),
linear-gradient(rgba(0,38,114,1),rgba(0,38,114,1));
background-size: 60% 100%;
background-position:left;
background-repeat: no-repeat;
}
#wrapped:hover {
border-color:rgba(0,38,114,0.5);
background-image:
linear-gradient(rgba(0,38,114,1),rgba(0,38,114,1)),
linear-gradient(rgba(0,38,114,0.5),rgba(0,38,114,0.5));
}
<span id="wrapped"></span>

关于javascript - 使前景透明,但背景不透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54651016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com