简体   繁体   English

如何定位引导轮播的活动幻灯片?

[英]How to target bootstrap carousel's active slide?

Evening,晚上,

Apologies for this likely being a really stupid question, I'm still learning!很抱歉这可能是一个非常愚蠢的问题,我还在学习!

I'm trying to target the active slide in the bootstrap carousel to perform an action.我正在尝试以引导轮播中的活动幻灯片为目标来执行操作。 I'd like it to trigger some style changes for each of the three slides ie changing button colours etc.我希望它能为三张幻灯片中的每一张触发一些样式更改,即更改按钮颜色等。

Earlier I tried just targeting the divs by their numbers( #1, #2, #3 ), but kept getting stuck there too, it looked something like this;早些时候我尝试只通过数字( #1, #2, #3 )来定位 div,但也一直卡在那里,看起来像这样;

var shead = document.getElementById("sub-head");
if ($('#1').is(':visible') && $('#2').is(':hidden') && $('#3').is(':hidden')) {   
    shead.style.color = "blue";
}

I repeated this for each of the slides using :visible & :hidden for each of the divs accordingly, although I only ever seemed to get stuck with the last presentation of the style colour change.我为每个 div 使用:visible & :hidden对每张幻灯片重复了此操作,尽管我似乎只对样式颜色更改的最后一次演示感到困惑。

I did a some searching on this and I've seen people using .carousel(1) , but I just seem to keep hitting dead ends, can anyone give me a hand with this, not sure why it's not catching, any guidance would be appreciated.我对此进行了一些搜索,我看到有人在使用.carousel(1) ,但我似乎一直在走入死胡同,任何人都可以帮我解决这个问题,不确定为什么它没有抓住,任何指导都是赞赏。

HTML HTML

<header id="Carousel" class="carousel slide carousel-fade">

    <ol class="carousel-indicators">
        <li data-target="Carousel" data-slide-to="0" class="active"></li>
        <li data-target="Carousel" data-slide-to="1"></li>
        <li data-target="Carousel" data-slide-to="2"></li>
    </ol>

    <div class="carousel-inner">
       <div class="item active" id="1"></div>
       <div class="item" id="2"></div>
       <div class="item" id="3"></div>
    </div>

</header>

JAVASCRIPT JavaScript

if ($('#Carousel').carousel(1).hasClass('active')) {
    alert("this is an alert");
}

Edit : This answer was written for Bootstrap 3, but it should work for bootstrap 4 as well.编辑:此答案是为 Bootstrap 3 编写的,但它也适用于 bootstrap 4。 See the following link for bootstrap 4 docs https://getbootstrap.com/docs/4.0/components/carousel/#events .有关 bootstrap 4 文档的以下链接,请参阅https://getbootstrap.com/docs/4.0/components/carousel/#events

Bootstrap has custom events you can hook to. Bootstrap 具有可以挂钩的自定义事件。

This event will fire when the method is going to slide.当方法要滑动时将触发此事件。

$('#Carousel').on('slide.bs.carousel', function onSlide (ev) {
  var id = ev.relatedTarget.id;
  switch (id) {
    case "1":
      // do something the id is 1
      break;
    case "2":
      // do something the id is 2
      break;
    case "3":
      // do something the id is 3
      break;
    default:
      //the id is none of the above
  }
})

There is also a slid.bs.carousel event.还有一个slid.bs.carousel事件。 This event is invoked when the slider is finished sliding.当滑块完成滑动时调用此事件。

You can read about the differences here https://getbootstrap.com/docs/3.3/javascript/#carousel-events .您可以在https://getbootstrap.com/docs/3.3/javascript/#carousel-events了解这些差异。

PS note that ev is the event object that is passed to the onSlide callback when the event was triggered. PS注意ev是事件触发时传递给onSlide回调的事件对象。 Note that the name ev could have been changed to anything, for example event , it's the order of the parameter in the callback that determines its value not its name.请注意,名称ev可以更改为任何内容,例如event ,回调中参数的顺序决定了它的值而不是它的名称。 Here the event object is passed in as the first parameter of the callback, this is due not to the above code having named the parameter accordingly ev (although that helps with understanding), but by how the jQuery code calls passed in event handlers (event handlers are a form of callback meant for events) and how the bootstrap code triggers the events.这里事件对象作为回调的第一个参数传入,这不是因为上面的代码相应地将参数命名为ev (尽管这有助于理解),而是由于 jQuery 代码如何调用在事件处理程序(event处理程序是一种用于事件的回调形式)以及引导代码如何触发事件。 The event object has common properties to a regular JavaScript event , and it has the additional properties given here in the bootstrap 4 docs for carousel events (with its current properties shown below as well in the yellow quote block). event 对象具有常规 JavaScript event 的通用属性,并且它具有在bootstrap 4 文档中为轮播事件提供的附加属性(其当前属性如下所示以及黄色引号块中所示)。

direction : The direction in which the carousel is sliding (either "left" or "right"). direction :旋转木马滑动的方向(“左”或“右”)。
relatedTarget : The DOM element that is being slid into place as the active item. relatedTarget :作为活动项滑动到位的 DOM 元素。
from : The index of the current item. from :当前项目的索引。
to : The index of the next item. to :下一项的索引。

You can use the self bootstrap events parameters "from" and "to" to get the indexes like this:您可以使用自引导事件参数“from”和“to”来获取这样的索引:

var myCarousel = document.getElementById('#myCarousel')

    myCarousel.addEventListener('slide.bs.carousel', function (ev) {
        console.log(ev.from);
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为bootstrap轮播设置活动幻灯片? - How to set active slide for bootstrap carousel? 如何访问轮播bootstrap4活动幻灯片以初始化事件 - How to access carousel bootstrap4 active slide to initialize an event 如何在 Bootstrap 3 carousel yii2 中设置活动幻灯片 - How to set Active slide in Bootstrap 3 carousel yii2 在活动的Bootstrap轮播幻灯片中定位iframe元素,并向SRC属性添加哈希? - Target iframe element in active Bootstrap carousel slide and add a hash to the SRC attribute? Bootstrap轮播幻灯片启动没有“活动” - Bootstrap carousel slide start without “active” Bootstrap Carousel - 在活动幻灯片上自动播放视频 - Bootstrap Carousel - Autoplay video on active slide Bootstrap Angular传送带可动态设置活动幻灯片 - Bootstrap Angular carousel to set active slide dynamically 根据活动的引导轮播幻灯片更改内容 - Change content based on active Bootstrap Carousel Slide 如何更改Bootstrap的轮播从幻灯片到淡入淡出的过渡 - How to change Bootstrap's carousel transition from slide to fade 如何将“幻灯片”和“滑动”事件附加到Bootstrap工具包的轮播? - How to attach “slide” & “slid” events to the Bootstrap toolkit's carousel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM