如何fadeTo()除一个div之外的所有内容?

发布于 2024-09-14 13:32:12 字数 301 浏览 2 评论 0原文

我正在创建一个教程,并且希望淡出页面上除我希望他们看到的 div 之外的每个 div。我如何使用 fadeTo 来做到这一点?

我目前将其设置为淡化整个身体。我怎样才能将它设置为淡出主体与div id“step2”分开?

代码:

<script type="text/javascript">
    $("body").fadeTo("slow", 0.5, function() {
      // Animation complete.
    });
</script>

I am creating a tutorial and want to fade every div on the page apart from the div I want them to see. How can I do this using fadeTo?

I currently have it set to fade the whole body. How can I set it to fade the body apart from div id "step2"?

Code:

<script type="text/javascript">
    $("body").fadeTo("slow", 0.5, function() {
      // Animation complete.
    });
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一张白纸 2024-09-21 13:32:13

淡入淡出始终会淡化下面的每个元素(包括您使用 jQuery 选择的元素)。除非您想要保持可见的 div 位于 body 的正下方,否则这将很棘手。为了达到您想要的效果,您应该克隆您想要可见的 div,并将其绝对放置在与原始位置相同的位置。这也意味着您需要将隐藏的主要内容放在容器 div 中,因此不要淡化主体,而是淡化容器 div,并将克隆的 div 放在主体正下方的顶层,在容器。

<body>
    <div id="content-to-fade">
        ...<div id="div-i-want-">...</div>
    </div>
    <div id="copy-of-div-i-want">...</div>
</body>

因此,在示例中,您将创建“copy-of-div-i-want”,附加到正文,然后在“content-to-fade”上调用 fadeTo。

这种技术是实现拖放的一种非常常见的方法,因为副本可以轻松有效地绝对定位在屏幕周围。

A fade will always fade every element under and including the one you selected with jQuery. Unless the div you want to stay visible is directly under the body, this will be tricky. To achieve the effect you desire, you should clone the div you want visible, and absolutely position it in the same location as the original. That also means you'll need to put the main content that is getting hidden inside of a container div, so instead of fading the body, fade the container div, and put the cloned div at the top level directly under the body, after the container.

<body>
    <div id="content-to-fade">
        ...<div id="div-i-want-">...</div>
    </div>
    <div id="copy-of-div-i-want">...</div>
</body>

so in the example, you would create "copy-of-div-i-want", append to the body and then call fadeTo on "content-to-fade".

This technique is a pretty common way of implementing drag and drop, because the copy can easily and efficiently be absolutely positioned around the screen.

‘画卷フ 2024-09-21 13:32:13

您可以将 css-selectors 与 JQuery 一起使用:)

$("*:not(div#step2)").fadeTo(...)

You can just use css-selectors with JQuery :)

$("*:not(div#step2)").fadeTo(...)
海未深 2024-09-21 13:32:13

使用您提供的代码执行您所描述的操作基本上是不可能的,因为所有元素都必须包含在 body 标记内,并且由于 opacity 属性继承,一切 页面将变为半透明。哦,覆盖单个 div 上的 opacity 属性也不起作用。

相反,您需要做的可能是将所有内容移动到另一个 div,然后淡入淡出该 div。使用 :not 选择器也可能有效,但这不是一个包罗万象的解决方案 - 请注意您的页面结构。

It is basically impossible to do what you've described with the code you gave, because all elements have to be contained within the body tag, and since the opacity property inherits, everything on the page will turn half transparent. Oh, and overriding the opacity property on that single div also won't work.

Instead, what you need to do is probably to move everything to another div, and fade that one instead. Using the :not selector may also work, but it isn't a catchall solution - be aware of your page structure.

沉睡月亮 2024-09-21 13:32:12

如果没有看到 HTML,很难判断,但如果 step2 的子级,您可以这样做:

尝试一下: < a href="http://jsfiddle.net/SS5Sm/" rel="nofollow noreferrer">http://jsfiddle.net/SS5Sm/

$("body").children(':not(#step2)').fadeTo("slow", 0.5, function() {
  // Animation complete.
});

或者

$("body > :not(#step2)").fadeTo("slow", 0.5, function() {
  // Animation complete.
});

如果嵌套更深,你可以尝试这个。

$("body > :not(:has(#step2))").fadeTo("slow", 0.5, function() {
  // Animation complete.
});​

尽管它对 HTML 结构做出了一些额外的假设。

Hard to tell without seeing your HTML, but if step2 is a child of <body> you could do this:

Try it out: http://jsfiddle.net/SS5Sm/

$("body").children(':not(#step2)').fadeTo("slow", 0.5, function() {
  // Animation complete.
});

or

$("body > :not(#step2)").fadeTo("slow", 0.5, function() {
  // Animation complete.
});

If it is more deeply nested, you could try this.

$("body > :not(:has(#step2))").fadeTo("slow", 0.5, function() {
  // Animation complete.
});​

Although it makes some additional assumptions about your HTML structure.

梦幻的味道 2024-09-21 13:32:12

这听起来像是 非选择器 的工作

This sounds like a job for the not selector

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文