gpt4 book ai didi

javascript - 为什么此文本不更改为完全不透明?

转载 作者:行者123 更新时间:2023-11-28 03:55:13 25 4
gpt4 key购买 nike

我在这个 HTML 文档上写了一个故事,我有一个 Javascript 应该将鼠标悬停在上面的任何文本变成完全不透明,但它似乎只是根据起始不透明度将它推到完全不透明的一小部分. hovering 类设置为 1 的不透明度,所以我不明白为什么当文本更改为该类时它不会变得完全不透明。

$(document).ready(function() {

var exceptions = ["lorem", "ipsum", "consectetur", "pharetra"];

$("p").each(function() { //for all paragraphs
var txt = $(this).text() //get text, split it up, add spans where necessary, put it back together
.split(" ")
.map(function(x) {
return exceptions.includes(x.toLowerCase()) ? x : "<span class='hover'>" + x + "</span>"}).join(" ");
$(this).html(txt); //set the text to our newly manipulated text
}).on("mouseover", ".hover", function() {
$(this).addClass("hovering"); //set opacity to 100%
}).on("mouseout", ".hovering", function() {
$(this).attr("class", ""); //set opacity to 0%, remove "hover" events
});
});
.hover {
opacity: .2;
}

.hovering {
opacity: 1;
}

span {
transition: opacity 0.5s;
opacity: 0;
}

p {
cursor: default;
}
<!DOCTYPE html>
<html>
<head>
<link rel = "stylesheet" href = "NarrativeTheory.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type = "text/javascript" src="NarrativeTheory.js"></script>
<h1>
Narrative Theory Project
</h1>
</head>

<body>
hover around in the area below...
<br>
<br>

<p class = "hover">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In pharetra risus nec maximus rutrum. Vestibulum vulputate, elit ac euismod gravida, felis erat eleifend felis, vel blandit lorem ex sit amet est. Cras luctus bibendum dolor, vel consequat magna.
Morbi pellentesque turpis metus. Pellentesque sit amet erat ex. Integer et nisi nisl. Quisque ornare mollis velit, id elementum elit pharetra at. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur
cursus cursus dolor, eu laoreet mauris dapibus vitae. Nunc ac ipsum sit amet diam suscipit lobortis. Nam nec vehicula augue. Cras nec sapien vitae leo gravida vestibulum.
</p>
<p class = "hover">
Etiam viverra bibendum aliquet. Aenean erat ligula, commodo id aliquet vel, eleifend ac orci. Aliquam blandit libero feugiat augue tincidunt, id fringilla lectus aliquam. Nulla ut nisl sit amet nulla feugiat porta. Curabitur euismod, mi vitae luctus facilisis,
est risus ornare erat, sed efficitur justo lorem nec urna. Cras in fringilla dolor. Aliquam faucibus scelerisque nunc, et rutrum quam pharetra ac. Vestibulum velit enim, consequat id nisi in, laoreet feugiat turpis. Phasellus auctor pharetra ultrices.
In ut condimentum lectus. Integer at dui egestas, ultrices metus pulvinar, venenatis mi. Donec vitae mauris viverra, convallis urna sit amet, posuere sapien. Sed quis magna odio. Vivamus mauris ipsum, euismod non sagittis eu, pretium et neque. Nunc
consequat ipsum eget magna facilisis mattis. Nulla eu lorem id tortor faucibus placerat.
</p>

</body>
</html>

最佳答案

这是因为您在两个不同级别的同一元素上设置了两次不透明度:

每个<p class="hover">设置为 opacity: .2;

每个<span class="hover">transition: opacity 0.5s; opacity: 0;

子元素的不透明度与其父元素相关。

所以如果 <p>元素的不透明度为 0.2,并且它有一个子元素 <span>不透明度 1.0,然后 <span> 的不透明度是 1.0 * 0.2 = 0.2。

这很容易通过删除 hover 来解决。来自 <p> 的类(class)元素,然后添加/删除 <span> 上的类仅 .

这是一个 jsFiddle:https://jsfiddle.net/07fpueyb/

或者,由于您已经在使用 jQuery,您可以使用 fadeTo() 函数大大简化此类情况:https://api.jquery.com/fadeTo/

https://jsfiddle.net/cyr9575e/ 查看 jsFiddle

编辑:您可以通过在 CSS 中使用 :hover 伪类再次更轻松地完成此操作:https://jsfiddle.net/8z7581jb/1/

关于javascript - 为什么此文本不更改为完全不透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43508051/

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