【问题标题】:How can I remove arrows from my slideshow when the list hits the end or beginning当列表到达结尾或开头时,如何从幻灯片中删除箭头
【发布时间】:2014-12-11 05:40:31
【问题描述】:

我的页面上有幻灯片。有五个项目。我有两个箭头循环穿过这些项目。当页面加载时,我使用 css 隐藏左箭头,当幻灯片播放结束时,我想隐藏右箭头。我试图做一些技巧,比如检测父母“margin-left:0”来从页面中删除左箭头,但到目前为止我还没有成功。我也根本无法删除右箭头。下面是我的代码。我知道有更简单的方法可以做到这一点,但我对这类问题没有经验。

<div id="tips-navbar">
    <ul class="slides">
      <li id="the-best-mashed-potatoes" class="selected">
        <div class="st-label">The Best Mashed Potatoes</div>
        <div class="underline"></div>
      </li>
      <li id="fully-loaded-mashed-potatoes">
        <div class="st-label">Fully Loaded Mashed Potatoes</div>
        <div class="underline"></div>
      </li>
      <li id="perfect-mashed-sweet-potatoes">
        <div class="st-label">Perfect Mashed Sweet Potatoes</div>
        <div class="underline"></div>
      </li>
      <li id="how-to-make-mash-tinis">
        <div class="st-label">How to Make Mash-tinis</div>
        <div class="underline"></div>
      </li>
      <li id="simple-smashed-potatoes">
        <div class="st-label">Simple Smashed Potatoes</div>
        <div class="underline"></div>
      </li>            
    </ul>
        <a class="st-brownBg st-left-m">
            <div class="st-arrowVideoBack"></div>
        </a>

        <a class="st-brownBg  st-right-m">
            <div class="st-arrowVideoNext"></div>
        </a>
  </div>

    $(function vNext(){
        $( ".st-right-m" ).click(function() {
            $( "#tips-navbar ul" ).animate({
                marginLeft: "-=431"
            }, 500);
            $( ".st-left-m" ).css('display','block');
            if($('.st-right-m'.click() >= 4)){
                $('.st-right-m').hide()
            }
        });
    });

    $(function vPrev(){
        $( ".st-left-m" ).click(function() {
            $( "#tips-navbar ul" ).animate({
                marginLeft: "+=431"
            }, 500);
            if($($this).parents('.slides').css('marginLeft' === 0 + 'px')){
                $(this).hide();
            }
        });

    });

沙盒:http://jsfiddle.net/eapo/oq46u3wf/1/

【问题讨论】:

  • 考虑先更新你的HTML,因为此时它是无效的。如果没有li,您不能将a 标签直接放在ul 中。
  • 谢谢,我改了好几次代码都忘了放回去。
  • 我没有看到与 Bootstrap 滑块/轮播相关的代码....

标签: jquery twitter-bootstrap jquery-animate slideshow carousel


【解决方案1】:

您可以使用您的 JS 代码执行以下操作:

$.current = 1;

$.updateArrows = function () {
    if ($.current <= 1) {
        if ($( ".st-left-m" ).is(':visible')) {
            $( ".st-left-m" ).hide();
        }
    } else {
        $( ".st-left-m" ).show();
    }
    if ($.current >= 5) {
        if ($( ".st-right-m" ).is(':visible')) {
            $( ".st-right-m" ).hide();
        }
    } else {
        $( ".st-right-m" ).show();
    }
};

$(function vNext(){
    $( ".st-right-m" ).click(function() {
        $( "#tips-navbar ul" ).animate({
            marginLeft: "-=431"
        }, 500);
        $.current++;
        $.updateArrows();
    });
});

$(function vPrev(){
    $( ".st-left-m" ).click(function() {
        $( "#tips-navbar ul" ).animate({
            marginLeft: "+=431"
        }, 500);
        $.current--;
        $.updateArrows();
    });
});

我们在这里实际做的是:

  1. 在当前幻灯片中保留一个 var,每次点击都会更新。
  2. 此外,每次单击时,我们都会更新检查当前幻灯片的箭头(具有额外功能),如果为 1,则隐藏左箭头,如果为 5,则隐藏右箭头。

【讨论】:

    【解决方案2】:

    您可以使用 jQuery 找到“最后一个孩子”,在您的情况下是最后一个

  • ,然后对其进行操作。请参阅此参考:http://api.jquery.com/last-child-selector/

    //以你为例

    $(".slides li").is(':last-child')
    
  • 【讨论】:

    • 你会怎么做?您将如何“检测”到您是最后一个孩子?
    • 我将代码添加到我的答案中,以了解如何检测您是否是最后一个孩子。 @putvande
    • 你会如何把它放在 OPs 函数中? OP 正在为父 ul 设置动画。我看不出这个last-child 选择器是如何工作的。
    • 如果没有看到其余的代码很难说,但是通过检测到你在最后一个孩子,你可以通过 jQuery 隐藏/删除箭头。我完全不明白为什么必须将其包含在函数中,但可以通过简单的 if 测试,不是吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 2022-11-29
    • 2021-09-01
    • 1970-01-01
    • 2022-07-07
    相关资源
    最近更新 更多