如何淡入淡出一个
使用 jQuery 淡出另一个
在?

发布于 2024-11-04 11:57:01 字数 2885 浏览 3 评论 0 原文

我正在构建我的作品集,我想创建一个画廊来展示我的项目。我选择了七个 div,其中包含形成我网站的选项卡式导航式部分的内容。与其他选项卡式导航一样,任何时候只有一个包含内容的 div 会处于活动状态。然而,与其他选项卡式导航不同的是,该导航是由 PHP 控制的。如果某些内容(当前设置为 file_exists 函数,但很快将由 MySQL 控制)可用,则 div 将被写入导航中,并显示相应的链接。如果不是,那么链接将被隐藏并且div将不会被写入。所需的文件基于 $_GET 调用,并且所需的文件根据 $_GET 变量内的字符串而有所不同。

每个选项卡都有一个唯一的 ID。目前(因为我不是 Javascript 专家),我有一个“重置”选项,可以将所有命名的 div 的样式设置为“隐藏”样式,然后再将我选择的 div 置于“可见”状态。

如果可能的话,我想做的是:

我想从 #div1 转到 #div2。 Divs 1、2、4 和 6(对于本例)处于活动状态。我单击告诉我的页面显示 #div2 的链接(该函数当前仅表示隐藏所有 div,然后显示 #div2,所有其他 div 的隐藏是一个单独的函数,在该函数中调用)。 #div1 当前可见。

  1. #div1 将淡出
  2. #div2 将淡入

Div 4 和 6 不会受到影响。 Divs 3、5 和 7 不会被触及,因为它们(就我的页面而言)不存在。在此示例中,每次淡入淡出可能需要 3 秒。

我隐约意识到 $('#div2').fadeIn(3000);将构成 #div2 的淡入效果,而 fadeOut() 对应部分将构成淡出。如何使用 jQuery(我的网站上有 1.5.2)淡出 #div1 并淡入 #div2,而不影响任何其他 div,或者更容易保留代码,因为它隐藏了我的所有 div 和只是淡入 #div2 吗?请记住,我不是 Javascript 专家,我只是一个初学者,所以请不要侮辱我的脚本的长度或我无法理解我认为如此简单的东西。

请记住,我的导航中最多有七个 div。脚本必须根据单击的链接找到可见的 div 并将其淡出,然后淡入我选择的 div,并且它不得触及任何剩余的 div。

这足够容易理解并获得答案吗?

编辑 @ 01:46 GMT, 30/04/2010

谢谢,但这些看起来不像我想要的。如果只有两个 div,它们看起来就可以工作,但请记住,最多有七个,并且链接应该知道哪个 div 是可见的,哪些是不可见的。

我目前的 PHP 脚本说“如果此文件存在,则创建此 div 并向其应用样式“visibleTab”。否则,创建它,但对其应用样式“hiddenTab”。”我的Javascript(现在是jQuery)代码如下:

  function resetTabs() {
    $("#postersandprint").removeClass("tabs visibleTab").removeClass("hiddenTab").addClass("hiddenTab");
    $("#mobileapp").removeClass("tabs visibleTab").removeClass("hiddenTab").addClass("hiddenTab");
    $("#stylesheets").removeClass("tabs visibleTab").removeClass("hiddenTab").addClass("hiddenTab");
    $("#storyboards").removeClass("tabs visibleTab").removeClass("hiddenTab").addClass("hiddenTab");
    $("#motionpieces").removeClass("tabs visibleTab").removeClass("hiddenTab").addClass("hiddenTab");
    $("#interactives").removeClass("tabs visibleTab").removeClass("hiddenTab").addClass("hiddenTab");
    $("#developmentwork").removeClass("tabs visibleTab").removeClass("hiddenTab").addClass("hiddenTab");
  }

  function showTab(tabname) {
    resetTabs();
    $('#'+tabname).fadeIn(3000);
    $("#"+tabname).removeClass("hiddenTab").addClass("tabs visibleTab");
  }

原理是这样的:

我的链接有一个onclick,它调用我的showTab函数并将适当的div id放在函数内,因此例如:

<a href="javascript:;" onclick="showTab('mobileapp');">Link</a>

该函数隐藏所有div然后淡入淡出在“tabname”调用的那个中,在本例中为“mobileapp”。

我已告诉每个重置函数在添加“hiddenTab”类作为一种“故障安全”方法之前删除任何名为“hiddenTab”的类以及任何名为“visibleTab”和“tabs”的类。我还告诉我的 showTab 函数从我想要可见的选项卡中显式删除“hiddenTab”类,并添加类“tabs”和“visibleTab”。我忘记了为什么我有两种风格,但我相信我会找到原因。

我希望我的 jQuery 脚本知道哪个 div 是可见的,哪个 div 不在七个选项中。 #div1 和 #div2 是一个示例,但它可以是所选内容中的任何 div。之前,当我使用 document.getElementById 函数时,它运行得很好,我只是想在脚本上添加淡入淡出以使其看起来更好。现在,它可以工作,但只有当我循环遍历 div 一次时。之后,它只是将我的 div 附加到可见的 div 下方,不会隐藏它们。我知道我错过了一些东西,或者我在某个地方搞砸了,但是我错过了什么?我哪里搞砸了?

I am building my portfolio and I want to create a gallery to display my projects. I have a selection of seven divs containing content that form a tabbed navigation-esque section of my website. As with other tabbed navigations, only one div containing content will be active at any one time. However, unlike other tabbed navigations, this one is controlled by PHP. If certain content (currently set to a file_exists function but will soon be MySQL controlled) is available, the div will be written into the navigation and the appropriate link will be displayed. If it is not, then the link will be hidden and the div will not be written. The files required are based on a $_GET call, and the files required vary depending on the string inside my $_GET variable.

Each tab has a unique id. Currently (since I am no Javascript expert), I have a "reset" option that sets the style of all named divs to a "hidden" style, before bringing my chosen div to a "visible" state.

What I want to do, if it is possible, is this:

I want to go from #div1 to #div2. Divs 1, 2, 4 and 6 (for this example) are active. I click the link that tells my page to display #div2 (the function currently only says to hide ALL divs and then display #div2, the hiding of all other divs is a separate function, which is called within this function). #div1 is currently visible.

  1. #div1 will fade out
  2. #div2 will fade in

Divs 4 and 6 will not be affected. Divs 3, 5 and 7 will not be touched since they (as far as my page is concerned) do not exist. Each fade can take 3 seconds for this example.

I am vaguely aware that $('#div2').fadeIn(3000); would constitute a fade in effect for #div2 and the fadeOut() counterpart would constitute a fade out. How do I use jQuery (I have 1.5.2 on my site) to fade #div1 out and fade #div2 in WITHOUT affecting any other div, or is it easier to keep the code as it is where it hides all of my divs and just fade #div2 in? Please remember, I am not a Javascript expert, I'm barely a beginner, so please do not insult the length of my script or my inability to understand something which I guess would be so simple.

Please remember that I have up to seven divs in my navigation. The script must find the div that is visible based on the link that is clicked and fade it out, then fade in my chosen div, and it must not touch any of the remaining divs.

Is that easy enough to understand and gain an answer?

EDIT @ 01:46 GMT, 30/04/2010

Thanks, but these don't look like what I want. They look like they would work if there were only two divs, but remember, there are up to seven, and the link should know which div is visible and which are not accounted for.

I currently have my PHP script to say "If this file exists, then make this div and apply the style 'visibleTab' to it. Otherwise, make it, but apply the style 'hiddenTab' to it." My Javascript (now jQuery) code is as follows:

  function resetTabs() {
    $("#postersandprint").removeClass("tabs visibleTab").removeClass("hiddenTab").addClass("hiddenTab");
    $("#mobileapp").removeClass("tabs visibleTab").removeClass("hiddenTab").addClass("hiddenTab");
    $("#stylesheets").removeClass("tabs visibleTab").removeClass("hiddenTab").addClass("hiddenTab");
    $("#storyboards").removeClass("tabs visibleTab").removeClass("hiddenTab").addClass("hiddenTab");
    $("#motionpieces").removeClass("tabs visibleTab").removeClass("hiddenTab").addClass("hiddenTab");
    $("#interactives").removeClass("tabs visibleTab").removeClass("hiddenTab").addClass("hiddenTab");
    $("#developmentwork").removeClass("tabs visibleTab").removeClass("hiddenTab").addClass("hiddenTab");
  }

  function showTab(tabname) {
    resetTabs();
    $('#'+tabname).fadeIn(3000);
    $("#"+tabname).removeClass("hiddenTab").addClass("tabs visibleTab");
  }

The principle is this:

My link has an onclick that calls my showTab function and places the appropriate div id inside the function, so for example:

<a href="javascript:;" onclick="showTab('mobileapp');">Link</a>

The function hides all of the divs and then fades in the one called by 'tabname', in this case, "mobileapp".

I have told each reset function to remove any class called 'hiddenTab' as well as any class called 'visibleTab' and 'tabs' before adding the 'hiddenTab' class as a kind of "fail safe" approach. I have also told my showTab function to explicitly remove the "hiddenTab" class from my tab that I want visible and to add the classes "tabs" and "visibleTab". I forget why I have two styles, but i am sure the reason will come to me.

I want my jQuery script to know which div is visible and which one is not out of the selection of seven. The #div1 and #div2 was an example, but it could be any div from the selection. Before, when I used the document.getElementById function, it worked perfectly, I just wanted to add a fade on to the script to make it look better. Now, it works, but only when I cycle through the divs once. Afterwards, it just appends my div underneath the visible one, it doesn't hide them. I know I have missed something, or I have messed up somewhere, but what have I missed? Where have I messed up?

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

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

发布评论

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

评论(5

篱下浅笙歌 2024-11-11 11:57:01

虽然没有在选项卡中设置,但希望这个 jsfiddle 示例会对您有所帮助。 [编辑,现在总共添加了 4 个 div,在单击下一步按钮之前等待动画完成]

基本上,它使用 :visible 选择器

.eq(0) 表示仅从选择器返回的集合中获取第一个可见元素。如果没有可见元素,它只选择第一个元素。

然后它选择要显示的 id。

同时调用两个动画:

vis.fadeOut(speed);
next.fadeIn(speed);

如果您想等待一个动画淡出后再淡出下一个动画,请使用其他答案中提到的回调。

这在所有新的浏览器上都可以正常工作,但在 IE7 或更低版本上会有点卡顿。

或者,您可以使用 :hidden 获取隐藏元素的集合

如果您想要更直接的示例,可以发布您的 html,以便我们可以帮助您找到最适合的确切选择器。

While not set up in tabs hopefully this jsfiddle example will help you a bit. [Edit, added total 4 divs now, wait for animation to finish before clicking the next button]

Basically it searches the container div for an element that is visible using the :visible selector

The .eq(0) says to only grab the first visible element out of the collection the selector returns. If there are no visible elements it just selects the first element.

Then it selects the id to show.

Calls out both animations at the same time with:

vis.fadeOut(speed);
next.fadeIn(speed);

If you want to wait for one to fade out before fading the next in use the callback mentioned in the other answers.

This works fine on all new-ish browsers but chokes a bit on IE7 or less.

Alternatively you can get a collection of hidden elements with :hidden

If you want a more direct example you can post your html so that we could help with the exact selectors that are best suited.

源来凯始玺欢你 2024-11-11 11:57:01

也许这就是你想要的?

  $('#div1').fadeOut('3000', function() {
       $('#div2').fadeIn();
  });

Maybe this is what you want?

  $('#div1').fadeOut('3000', function() {
       $('#div2').fadeIn();
  });
酒废 2024-11-11 11:57:01

当您调用任何 jQuery 效果函数时,其中一个参数是动画完成时调用的回调函数。你只需要做这样的事情

$("#div1").fadeOut(1000, function(){
    $("#div2").fadeIn(1000);
});

这是一个演练

$("#div1").fadeOut(1000, function(){

这表示当 id 为“div1”的 div 将面朝外 1000 毫秒(1 秒),然后它将调用该函数。

$("#div2").fadeIn(1000);

这只会让您的新想法另一个想法完全淡出后淡入。

});

只需关闭我们打开的所有内容即可。

我认为你担心的是,

$("div").fadeOut(1000);

会淡出页面上的所有 div,但放置一个散列标记和一个 id,你可以识别特定的一个,我的 id 就像 "#div1"

另外,你设置它带有 id 属性:

<div id = "div1"></div>

When you call the any of the jQuery effect functions, one of the parameters is a callback function which is called when the animation is complete. You simply need to do something like this

$("#div1").fadeOut(1000, function(){
    $("#div2").fadeIn(1000);
});

Here is a walkthough

$("#div1").fadeOut(1000, function(){

This says that when the div with an id of "div1" will face out for 1000 milliseconds (1 second) and after it will call the function.

$("#div2").fadeIn(1000);

This simply makes you new idea fade in after the other one has completely finished fading out.

});

Simply closes up everything that we opened.

I think what you are worried about is that

$("div").fadeOut(1000);

Would fade out all divs on the page but putting a hash mark and an id you can identify a specific one my it's id like "#div1"

Also, you set it with the id attribute:

<div id = "div1"></div>
哭泣的笑容 2024-11-11 11:57:01

我找到了这个 jShowOff:Erik Kallevig 的 jQuery 内容旋转器插件 - http://bit.ly/NgLRdb 给它一看,似乎很有用......

目前正在尝试将其作为我的 joomla 网站上的模块

I found this jShowOff: a jQuery Content Rotator Plugin by Erik Kallevig - http://bit.ly/NgLRdb give it a look, seems pretty useful....

currently trying to put it as a module on my joomla site

又怨 2024-11-11 11:57:01
$("#button").click(function()
{
$("#div1").fadeOut("fast");
$("#div2").fadeIn("slow");
});

此代码将像单击按钮一样淡出 div1,然后慢慢淡入另一个 div 作为 div2。

$("#button").click(function()
{
$("#div1").fadeOut("fast");
$("#div2").fadeIn("slow");
});

This code will work as button clicks which will fade out div1 and then slowly fade in an another div as div2.

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