我正在做一些尝试,并且设置了以下挑战,而现在我无法解决。我正在为Flyspray错误跟踪器创建用户脚本。在问题列表中,有一列指示完成百分比:
该系统相当陈旧,因此具有一定的简单性。这是进度的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="https://i.stack.imgur.com/lymku.png" /></span>
上述解决方案的主要问题:包装器的高度与<img>
高度不匹配,这意味着背景尺寸必须精确设置为像素。
是否可以在没有任何包装元素的情况下完成此操作?
请注意,这是一个练习/学习的问题,通过其他方式解决整个问题的解决方案对我没有用。
答案 0 :(得分:3)
请勿使用具有透明颜色的渐变。仅使用纯色并控制background-size
,然后只需制作img
块元素即可避免空白问题:
不确定是否没有包装器是可能的,因为您将需要一个元素来应用渐变:
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="https://i.stack.imgur.com/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>