【问题标题】:Swap two Divs out at once and swap them for a different two and keep looping for a set amount of seconds一次交换两个 Div 并将它们交换为不同的两个并保持循环一定的秒数
【发布时间】:2013-11-29 14:32:22
【问题描述】:

我一直在开发一个函数,它的功能有点像我想让它做的事情,如上所示,但是我遇到了一个问题,即一次交换两个 div 并用不同的两个替换,任何人都指出我正确的方向是不知道该怎么做,以下是我到目前为止所得到的:

$(function () {

var counter = 0,
    divs = $('#budgetSpent,#budgetTitle,#staticBudget,#staticTitle');

function showDiv () {
    divs.hide() // hide all divs
        .filter(function (index) { return index == counter % 3; }) // figure out correct div to show
        .show('fast'); // and show it

    counter++;
}; // function to loop through divs and show correct div

showDiv(); // show first div    

setInterval(function () {
    showDiv(); // show next div
}, 5 * 1000); // do this every 10 seconds    

});

简短说明: 所以几乎我有两个计数器,每 10 秒显示一次 div,其余的被隐藏,现在它确实交换了,但是旁边有标题的两个 div id,我不能让它们与各自的计数器一起交换所以说示例: £0000 Budget1(显示 10 秒)然后切换到 £11111 Budget2,它应该循环运行,到目前为止,列出的 4 个 div 一直在每个 setInterval 一个接一个地交换,但我需要一次显示和隐藏 2 个 div而不是一个一个

【问题讨论】:

  • 不知道为什么我被否决了...我试图解决这个问题。
  • 也不确定,但我读了几遍,我不明白交换的东西,或者你实际上想要做什么
  • 这么多我有两个计数器,每 10 秒显示一次 div,其余的被隐藏,现在它确实交换了,但是旁边有标题的两个 div id,我不能让它们交换连同相应的计数器,例如:0000 英镑预算 1(显示 10 秒)然后切换到 11111 英镑预算 2,它应该来回添加上面的内容以尝试提供更好的概览,这有帮助吗?每次显示一个 Div 时,其余部分都被隐藏。
  • 你能试着做一个jsFiddle吗?
  • jsfiddle.net/A8R7T 我在工作,所以我看不出这是否在 jsFiddle 中工作,但从未真正使用过它!它在这里是这样的; stackoverflow.com/questions/914951/… 一次只改变两个而不是一个

标签: javascript jquery


【解决方案1】:

首先,您必须保持代码简单。尤其是在 SO 上要求某些东西时。我们不想知道你的 div 中有什么,但我们只需要它们的 HTML 标记:

<div id='budgetTitle'>budgetTitle div</div>
<div id='staticBudget'>staticBudget div</div>
<div id='staticTitle'>staticTitle div</div>
<div id='budgetSpent'>BudgetSpent div</div>

这样调试起来更加简洁和快速。然后,您不应该在这里使用 id,而是使用特定的类来显示/隐藏它们:

<div id='budgetTitle'>budgetTitle div</div>
<div id='staticBudget' class="hide">staticBudget div</div>
<div id='staticTitle'>staticTitle div</div>
<div id='budgetSpent' class="hide">BudgetSpent div</div>

隐藏 div 的 CSS 代码:

div.hide {display: none;}

现在您将只看到 #budgetTitle#staticTitle div。好吧,现在让我们开始“交换”部分:
您只需为其他 div 提供 .hide 类并从其他 div 中删除该类。有一个jQuery 方法:jQuery.toggleClass()

现在你只需要这样做来交换 div:

// This will give all divs that don't have the "hide" class the class "hide"
// and remove the class "hide" from all divs that have the "hide" class 
$('div').toggleClass('hide');

最终代码:

function swapDivs () {
    $('div').toggleClass('hide');
}

$(function () {
   setInterval(
       function () {
           swapDivs(); // show next div
       },
       3 * 1000
    ); // do this every 3 seconds    
});

Demo jsFiddle

如果您的代码中有其他不想切换的 div,则应在这四个 div 中添加 toggle 类:

<div id='budgetTitle' class="toggle">budgetTitle div</div>
<div id='staticBudget' class="toggle hide">staticBudget div</div>
<div id='staticTitle' class="toggle">staticTitle div</div>
<div id='budgetSpent' class="toggle hide">BudgetSpent div</div>

然后只需在js 中进行一些更改:

function swapDivs () {
    $('div.toggle').toggleClass('hide');
}

如果您无法使其工作,请尝试从头开始(不使用您的 javascript、css 和 html),然后尝试在 div 中添加您自己的 html 代码。

【讨论】:

  • 不错的答案,+1。但也许你应该添加一个类来切换 div,以免隐藏他页面上的每个 div。
  • 在这种情况下,只有四个 div 直接在正文中。但是,是的,你是对的。我会做一个小编辑
  • 这是一个非常好的答案,我知道您正在尝试做什么,现在只是摆弄它以使其正常工作,谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-12
  • 1970-01-01
  • 2012-09-13
  • 2016-02-23
  • 2011-10-18
  • 2012-05-29
  • 2021-08-04
相关资源
最近更新 更多