p:not(:hover) 只有在另一个对象被悬停时激活

huangapple go评论78阅读模式
英文:

p:not(:hover) only active when another object is hovered

问题

在上面的代码中,您想要实现的效果是降低没有被悬停的对象的不透明度。您可以尝试以下方法来实现这个效果:

/* 初始状态下,所有 <p> 元素的不透明度为 0.6 */
p {
    opacity: 0.6;
}

/* 当鼠标悬停在 <p> 元素上时,将其不透明度恢复为 1 */
p:hover {
    opacity: 1;
}

这将使所有的 <p> 元素在初始状态下都具有较低的不透明度,当鼠标悬停在它们上面时,它们的不透明度会恢复为 1,实现了您所需的效果。

至于您提到的与编程相关的问题,如果您需要进一步的帮助,可以随时提出具体的问题,我会尽力提供解答。

英文:

in:

p:not(:hover) {
    opacity: 0.6;
}

p {
    opacity: 1;
}

Which would only highlight what is hovered, but I want to achieve an effect that would lower opacity of objects that aren't being hovered on. how do I do it?

the only "person" I could reach out to as someone new to programming was chatGPT that has failed me after many tries, or maybe it's me who can't spell my requirements correctly.

答案1

得分: 0

你正在过于复杂化代码。你想要做的比看起来要简单得多。从我的理解来看,你想要设置opacity: 1只在鼠标悬停在段落上时才会生效,其余情况下设置为opacity: .6

CSS

p {
    opacity: .6;
}

p:hover {
    opacity: 1;
}

使用这段代码,默认情况下所有段落的不透明度都是0.6,当你将鼠标悬停在它们上面时,特定的段落将不透明度设置为1。

英文:

You're overcomplicating the code. What you want to do is much simpler than it seems. From what I understand you want to set opacity: 1 only to the paragraph with the hover, and the rest set with opacity: .6

CSS

p {
    opacity: .6;
}

p:hover {
    opacity: 1;
}

With this code, by default all paragraphs will have an opacity of 0.6, and when you hover over them, that particular paragraph will have an opacity of 1.

答案2

得分: 0

你不必使用JavaScript。要使所有项目的效果为opacity: 0.6,除了当前项目外,您需要为父项目分配悬停,并同时从具有悬停的父级分配效果opacity: 1 给当前项目:div:hover p:hover {}

p {
    background: green;
}

div:hover p {
    opacity: 0.6;
}

div:hover p:hover {
    opacity: 1;
}
<div>
    <p>文本</p>
    <p>文本</p>
    <p>文本</p>
</div>
英文:

You don't have to use JavaScript. To make the effect opacity: 0.6 for all items except the current item, you need to assign a hover to the parent item, and simultaneously assign the effect opacity: 1 from the parent with the hover to the current item: div:hover p:hover {}

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

p {
    background: green;
}

div:hover p {
    opacity: 0.6;
}

div:hover p:hover {
    opacity: 1;
}

<!-- language: lang-html -->

&lt;div&gt;
    &lt;p&gt;text&lt;/p&gt;
    &lt;p&gt;text&lt;/p&gt;
    &lt;p&gt;text&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: -2

以下是翻译好的内容:

const boxItems = Array.from(document.querySelectorAll(".box"))

const setBoxOpacity = () => boxItems.forEach(item => item.classList.add("not-hovered"))
const removeBoxOpacity = () => boxItems.forEach(item => item.classList.remove("not-hovered"))

boxItems.forEach(box => {
    box.addEventListener("mouseover", (e) => {
      setBoxOpacity()
      e.target.classList.remove("not-hovered")
  })
})

boxItems.forEach(box => box.addEventListener("mouseout", removeBoxOpacity))
.boxes {
  display: flex;
  gap: 15px;
}
.box {
  border: 1px solid black;
  width: 200px;
  height: 150px;
  background: red;
  opacity: 1;
}

.box.not-hovered {
  opacity: 0.5;
}
<div class="boxes">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</div>
英文:

There would be one simple way of doing it with JavaScript.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const boxItems = Array.from(document.querySelectorAll(&quot;.box&quot;))

const setBoxOpacity = () =&gt; boxItems.forEach(item =&gt; item.classList.add(&quot;not-hovered&quot;))
const removeBoxOpacity = () =&gt; boxItems.forEach(item =&gt; item.classList.remove(&quot;not-hovered&quot;))

boxItems.forEach(box =&gt; {
    box.addEventListener(&quot;mouseover&quot;, (e) =&gt; {
      setBoxOpacity()
      e.target.classList.remove(&quot;not-hovered&quot;)
  })
})

boxItems.forEach(box =&gt; box.addEventListener(&quot;mouseout&quot;, removeBoxOpacity))

<!-- language: lang-css -->

.boxes {
  display: flex;
  gap: 15px;
}
.box {
  border: 1px solid black;
  width: 200px;
  height: 150px;
  background: red;
  opacity: 1;
}

.box.not-hovered {
  opacity: 0.5;
}

<!-- language: lang-html -->

&lt;div class=&quot;boxes&quot;&gt;
  &lt;div class=&quot;box&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;box&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;box&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;box&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月8日 23:36:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定