简体   繁体   English

将一个jQuery函数连接到引导程序的轮播幻灯片事件

[英]connect a jquery function to bootstrap's carousel slide event

I created the following function: 我创建了以下函数:

//Toggle height of header-foto function
function height_toggler() {
    $('body').toggleClass('large-slider small-slider',500);
    $('#height-toggler').find('i').toggleClass('fa-rotate-270 fa-rotate-90');
}

I call this function when a user click the button: 我在用户单击按钮时调用此函数:

// bind the function to a click on the toggler
$('#height-toggler').click( function() {
    height_toggler();
});

this works perfectly, you can see it live on this page 效果很好,您可以在此页面上实时查看

My client also wants this function bound on the Bootstrap Carousel's slide event and only on pages with 100% height carousels. 我的客户还希望将此功能绑定在Bootstrap Carousel的slide 事件上,并且仅在高度为100%的页面上绑定。 I came up with the following code, but this triggers everytime the carousel slides (which I understand). 我想出了以下代码,但是每次轮播幻灯片(据我了解)都会触发。 How can I make it trigger only on the first slide after page load? 如何才能使其仅在页面加载后在第一张幻灯片上触发?

if( $('body').hasClass('large-slider') ) { //only perform this on large sliders
    $('.carousel').on('slide.bs.carousel', function () {
        height_toggler();
    });
}

You can use one() method. 您可以使用one()方法。

if( $('body').hasClass('large-slider') ) { //only perform this on large sliders
  $('.carousel').one('slide.bs.carousel', function () {
    height_toggler();
  });
}

IF you want an event to be executed only once, you can use .one() instead of .on() 如果只希望事件执行一次,则可以使用.one()代替.on()

if( $('body').hasClass('large-slider') ) { //only perform this on large sliders
    $('.carousel').one('slide.bs.carousel', function () {
        height_toggler();
    });
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM