【问题标题】:Should be easy jQuery loop, but can't seem to figure out how to cycle through this应该是简单的 jQuery 循环,但似乎无法弄清楚如何通过这个循环
【发布时间】:2013-09-21 13:14:40
【问题描述】:

我正在网站上构建一个功能,该功能每 5 秒左右显示一个新的“特色顾问”。如果它只是显示一张图片,我会使用 show() 和 hide()。不幸的是,我必须将图像移出然后显示一个带有一些标题文本的 div,然后在 5 秒后删除标题和图像。幸运的是,我已经成功编写了“显示”功能和“隐藏”功能,而且我什至让它在隐藏前等待指定的 5 秒。我的问题是我不知道如何转到下一个“特色顾问”并运行 show-wait-hide 功能。任何建议都会非常感激。这是我的代码供参考:

CSS:

article[role=main] aside li {/*Set up the basic stying & hide them all*/
   list-style: none;
   margin: 0px;
   padding: 0px;
   display: none;
  }

article[role=main] aside li.show { /*Only show one at a time*/
display: block;
} 

HTML:

<ul id="items">
<li class="show">
    <a href="#">
        <div class="caption">
          <h5>Featured Counselor</h5>
            <h3>Courtney Humphrey</h3>
            <h4>Registered Dietician</h4>
        </div><!-- End #caption -->
        <div class="featured-counselor">
            <img src="img/featured_counselor_placeholder.jpg" />
        </div><!-- End #featured-counselor -->
    </a>
</li>
<li>
    <a href="#">
        <div class="caption">
          <h5>Featured Counselor</h5>
            <h3>Test Two Title</h3>
            <h4>Registered Dietician</h4>
        </div><!-- End #caption -->
        <div class="featured-counselor">
            <img src="img/featured_counselor_placeholder.jpg" />
        </div><!-- End #featured-counselor -->
    </a>
</li>

jQuery:

featuredCounselorCarousel(); //Call the function that runs the show

function featuredCounselorCarousel() {
    showCurrentCounselor(); //Show the Counselor First
    setTimeout(function() { //Add a timer (Show for 5 seconds)        
    hideCurrentCounselor() //After 5 seconds, hide the current counselor                    
    }, 5000)
}// End featuredCounselorCarousel


function showCurrentCounselor() { //This is the Function that shows the Counselor       
    $('article[role=main] aside ul li.show #featured-counselor').delay( 100 ).animate({"left": "0px"}, 900, 'easeInOutQuint');//Slide out
    $('article[role=main] aside ul li.show #caption').delay( 1000 ).fadeIn(400);//Display the Caption
}// End showCurrentCounselor 


function hideCurrentCounselor() { //This is the Function that hides the Counselor       
    $('article[role=main] aside ul li.show #featured-counselor').delay( 100 ).animate({"left": "-230px"}, 900, 'easeInOutQuint');//Slide Back In
    $('article[role=main] aside ul li.show #caption').delay( 500 ).fadeOut(400);//Remove the Caption
}// End hideCurrentCounselor

【问题讨论】:

  • 请注意,您的 html 无效:id 属性应该在页面上是唯一的。

标签: jquery html css loops


【解决方案1】:
 function triggerCarousal(){  setTimeout(function() { //Add a timer (Show for 5 seconds)        
     featuredCounselorCarousel();                    
    }, 5000) }

function featuredCounselorCarousel() {
    hideCurrentCounselor();
showCurrentCounselor(); //Show the Counselor First

}// End featuredCounselorCarousel


function showCurrentCounselor() { //This is the Function that shows the Counselor       
    $('article[role=main] aside ul li.show #featured-counselor').delay( 100 ).animate({"left": "0px"}, 900, 'easeInOutQuint');//Slide out
    $('article[role=main] aside ul li.show #caption').delay( 1000 ).fadeIn(400);//Display the Caption
}// End showCurrentCounselor 


function hideCurrentCounselor() { //This is the Function that hides the Counselor       
    $('article[role=main] aside ul li.show #featured-counselor').delay( 100 ).animate({"left": "-230px"}, 900, 'easeInOutQuint');//Slide Back In
    $('article[role=main] aside ul li.show #caption').delay( 500 ).fadeOut(400);//Remove the Caption
current = $('article[role=main] aside ul li.show');
next = $(current).next();
$(next).toggleClass("show");
$(current).toggleClass("show);
}// End hideCurrentCounselor

这可能行得通..

【讨论】:

  • 非常感谢您的帮助,但是此代码等待 5 秒然后执行
  • 感谢您的帮助。尽管我没有使用您提供的所有代码,但我确实使用了设置“下一步”和“当前”的最后一部分。你让我指出了正确的方向,我现在一切正常。感谢您的帮助。
【解决方案2】:

在您的 featuredCounselorCarousel 函数中使用 $(#items li).each 并且您的所有代码都在每个函数中。

您将能够访问当前的 li,并可以在显示和隐藏功能中相应地修改您的选择器,使其与 li 元素相关。

【讨论】:

  • 我正在尝试您的建议,但我无法将计时器放在正确的位置。它不断地同时射击它们
猜你喜欢
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-28
  • 1970-01-01
  • 2013-10-07
相关资源
最近更新 更多