使用 javascript 模数通过 for 循环遍历文本滑块

发布于 2024-09-27 04:45:44 字数 2425 浏览 7 评论 0原文

我是 jQuery 和堆栈溢出的新手,所以我会尝试具体说明,但请耐心等待。我正在尝试从头开始创建一个带有关联链接的文本滑块,使用模数迭代列表并重复。

这是我正在使用的代码:

ul#text { position: relative; margin-bottom: 40px; height: 40px; }
ul#text li { position: absolute; display: none; }
.active { font-weight: bold; }
<ul id="text">
<li id="textBody">Suffering is not a result of physical pain alone. It can be compounded by changes in one's life, and changes in the self. <em>We understand, and we can help.</em></li>
<li id="textFamily">Aggressive assessment of physical symptoms &amp; pain in the body are key to support <em>the best possible quality of life</em>.</li>
<li id="textFunction">Chronic pain &amp; illness may affect your role in your family. We work with you and your family as you confront those changes.</li>
<li id="textPsyche">Chronic pain and illness make even everyday activities challenging. We will help you maintain independence and physical function.</li>
<li id="textSuffering">Changes in the physical body mean changes in the self. We will provide support as you navigate those changes in the psyche.</li>
</ul>
<ul id="vivid_buttons">
<li><a href="#" id="buttonBody">BODY</a></li>
<li><a href="#" id="buttonFamily" class="active">FAMILY</a></li>
<li><a href="#" id="buttonFunction">FUNCTION</a></li>
<li><a href="#" id="buttonPsyche">PSYCHE</a></li>
<li><a href="#" id="buttonSuffering">SUFFERING</a></li>
</ul>
$(document).ready(function () {

    function fadeAndMove() {
        var nextLi = $("#text > li:nth-child(" + i % 5 + ")");
        var nextA = $("#vivid_buttons > li:nth-child(" + i % 5 + ") > a");
        nextLi.fadeIn(1000, function () {
            $("#vivid_buttons > li > a").removeClass("active");
            nextA.addClass("active");
            setTimeout(function () {
                nextLi.fadeOut(1000);
            }, 4000);
        });
    }

    for (i = 1; i < 7; i++) {
        fadeAndMove($("#text"));
    }
});

用简单的语言来说,我想淡入第一个列表中的一个句子,并突出显示底部列表中的相应链接。然后我希望它淡出并移至下一个项目。

我以为我可以使用模数 (%) 和 for 循环来迭代并创建一个无限循环,但是当我将其放入时,它就像一次执行所有内容,而不是对每个项目迭代(淡入和淡出)。

我知道这很令人困惑,但我希望能得到任何帮助。

I'm new to jQuery and stack overflow, so I'll try to be specific, but please bear with me. I'm trying to create a text slider with associated links from scratch, using modulus to iterate through the list and repeat.

Here's the code I'm working with:

ul#text { position: relative; margin-bottom: 40px; height: 40px; }
ul#text li { position: absolute; display: none; }
.active { font-weight: bold; }
<ul id="text">
<li id="textBody">Suffering is not a result of physical pain alone. It can be compounded by changes in one's life, and changes in the self. <em>We understand, and we can help.</em></li>
<li id="textFamily">Aggressive assessment of physical symptoms & pain in the body are key to support <em>the best possible quality of life</em>.</li>
<li id="textFunction">Chronic pain & illness may affect your role in your family. We work with you and your family as you confront those changes.</li>
<li id="textPsyche">Chronic pain and illness make even everyday activities challenging. We will help you maintain independence and physical function.</li>
<li id="textSuffering">Changes in the physical body mean changes in the self. We will provide support as you navigate those changes in the psyche.</li>
</ul>
<ul id="vivid_buttons">
<li><a href="#" id="buttonBody">BODY</a></li>
<li><a href="#" id="buttonFamily" class="active">FAMILY</a></li>
<li><a href="#" id="buttonFunction">FUNCTION</a></li>
<li><a href="#" id="buttonPsyche">PSYCHE</a></li>
<li><a href="#" id="buttonSuffering">SUFFERING</a></li>
</ul>
$(document).ready(function () {

    function fadeAndMove() {
        var nextLi = $("#text > li:nth-child(" + i % 5 + ")");
        var nextA = $("#vivid_buttons > li:nth-child(" + i % 5 + ") > a");
        nextLi.fadeIn(1000, function () {
            $("#vivid_buttons > li > a").removeClass("active");
            nextA.addClass("active");
            setTimeout(function () {
                nextLi.fadeOut(1000);
            }, 4000);
        });
    }

    for (i = 1; i < 7; i++) {
        fadeAndMove($("#text"));
    }
});

In simple language, I want to fade in a sentence from the first list, and highlight the corresponding link on the bottom list. I then want it to fade out and move to the next item.

I thought I could use modulus (%) and a for loop to iterate through and create an infinite loop, but when I put this in it's like it executes everything all at once, not iterating through (fading in and out) for each item.

I know this is confusing, but I'd appreciate any help I could get.

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

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

发布评论

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

评论(3

战皆罪 2024-10-04 04:45:44

摆脱 for 循环,只需让 setTimeout 调用 fadeAndMove() 函数,并传递当前索引。

示例: http://jsfiddle.net/drWhE/

$(document).ready(function () {

       // cache the LI elements
    var $lis = $("#text > li");
    var $aLis = $("#vivid_buttons > li");

    function fadeAndMove( currentIndex ) {
        var nextIndex = (currentIndex + 1) % 5;
        var nextLi = $lis.eq( nextIndex );
        nextLi.fadeIn(1000, function () {
            $aLis.eq( currentIndex ).children('a').removeClass("active");
            $aLis.eq( nextIndex ).children('a').addClass("active");
            setTimeout(function () {
                nextLi.fadeOut(1000, function() {
                      // Call fadeAndMove() passing nextIndex as the new currentIndex
                    fadeAndMove( nextIndex );
                });
            }, 4000);
        });
    }
       // Get it started on index 0
    fadeAndMove( 0 );
});

Get rid of the for loop, and just have the setTimeout call the fadeAndMove() function, passing the current index.

Example: http://jsfiddle.net/drWhE/

$(document).ready(function () {

       // cache the LI elements
    var $lis = $("#text > li");
    var $aLis = $("#vivid_buttons > li");

    function fadeAndMove( currentIndex ) {
        var nextIndex = (currentIndex + 1) % 5;
        var nextLi = $lis.eq( nextIndex );
        nextLi.fadeIn(1000, function () {
            $aLis.eq( currentIndex ).children('a').removeClass("active");
            $aLis.eq( nextIndex ).children('a').addClass("active");
            setTimeout(function () {
                nextLi.fadeOut(1000, function() {
                      // Call fadeAndMove() passing nextIndex as the new currentIndex
                    fadeAndMove( nextIndex );
                });
            }, 4000);
        });
    }
       // Get it started on index 0
    fadeAndMove( 0 );
});
素手挽清风 2024-10-04 04:45:44

一切都会立即动画化,因为您的主循环继续运行,而淡出计时器则等待四秒钟。

大致如下(每行代表一秒):

li1.fadeIn
li2.fadeIn  |
li3.fadeIn  | |
li4.fadeIn  | | |      Timers
li5.fadeIn  V | | |    wait four
li1.fadeOut   V | | |  seconds
li2.fadeOut     V | |
li3.fadeOut       V |
li4.fadeOut         V
li5.fadeOut
li1.fadeIn
li2.fadeIn
.
.
.
etc, etc, ad nauseam.

要解决该问题,您需要在延迟函数中淡出当前项后立即链接对 fadeAndMove() 的下一个调用

nextLi.fadeIn(1000, function () {
    $("#vivid_buttons > li > a").removeClass("active");
    nextA.addClass("active");
    setTimeout(function () {
        nextLi.fadeOut(1000);
        fadeAndMove(i + 1);
    }, 4000);
});

:(由于这是一个延迟调用,因此它不是递归的,无限循环不会破坏堆栈。)

Everything animates at once because your main loop keeps running while your fadeout timers wait four seconds.

Schematically, it goes like this (each line represents one second):

li1.fadeIn
li2.fadeIn  |
li3.fadeIn  | |
li4.fadeIn  | | |      Timers
li5.fadeIn  V | | |    wait four
li1.fadeOut   V | | |  seconds
li2.fadeOut     V | |
li3.fadeOut       V |
li4.fadeOut         V
li5.fadeOut
li1.fadeIn
li2.fadeIn
.
.
.
etc, etc, ad nauseam.

To solve the problem, you need to chain the next call to fadeAndMove() right after fading out the current item in your delayed function:

nextLi.fadeIn(1000, function () {
    $("#vivid_buttons > li > a").removeClass("active");
    nextA.addClass("active");
    setTimeout(function () {
        nextLi.fadeOut(1000);
        fadeAndMove(i + 1);
    }, 4000);
});

(Since that's a delayed call, it's not recursive. The infinite loop won't smash the stack.)

夜唯美灬不弃 2024-10-04 04:45:44

这是获取所需内容的一种方法:

var $sentence_set = $('ul#text > li');
var $link_set = $('ul#vivid_buttons > li');

var highlight = function(which) {
  var el = $sentence_set.eq(which - 1);
  var position = el.prevAll('li').length;
  $link_set.removeClass('active').eq(position).addClass('active');
  $sentence_set.eq(position).siblings().fadeOut().end().fadeIn();
}

var loopcount = 0;
var repeater = setInterval(function() {
  var theList = $('#text > li');
  highlight(++loopcount % $sentence_set.length);
}, 4000);​

这是小提琴

而且......橙色条告诉我帕特里克·DW 击败了我类似的东西。好吧,无论如何,它就在这里。

Here's one way to get what you're looking for:

var $sentence_set = $('ul#text > li');
var $link_set = $('ul#vivid_buttons > li');

var highlight = function(which) {
  var el = $sentence_set.eq(which - 1);
  var position = el.prevAll('li').length;
  $link_set.removeClass('active').eq(position).addClass('active');
  $sentence_set.eq(position).siblings().fadeOut().end().fadeIn();
}

var loopcount = 0;
var repeater = setInterval(function() {
  var theList = $('#text > li');
  highlight(++loopcount % $sentence_set.length);
}, 4000);​

Here's the fiddle.

And... the orange bar tells me patrick dw beat me to something similar. Well, here it is anyway.

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