bootstrap 4 轮播如何激活过渡效果?

huangapple go评论55阅读模式
英文:

bootstrap 4 carousel how to active transitions?

问题

I have a Bootstrap 4 carousel and I need to move 4 slides each time. I'm using this code, and it works:

$(this).carousel(4);

The problem is that the carousel just jumps to that index and doesn't show any transitions. If I use $(this).carousel("next"); or $(this).carousel("prev"); it works and transitions correctly.

I tried using something like this:

$(this).carousel("next");
$(this).carousel("next");

But it does not work. Any help will be appreciated.

UPDATE

I found a workaround:

test = setInterval(() => {
  count++;
  $(".carousel-control-next").click();
  if (count > 3) {
    count = 0;
    clearInterval(test);
  }
}, 200);

I used an interval because the following does not work:

  • $(".carousel-control-next").click() inside a loop, or
  • $(".carousel-control-next").click() inside a setTimeout inside a loop
英文:

I have a Bootstrap 4 carousel and I need to move 4 slides each time. I'm using this code, and it works:

$(this).carousel(4);

The problem is that the carousel just jumps to that index and doesn't show any transitions. If I use $(this).carousel("next"); or $(this).carousel("prev"); it works and transitions correctly.

I tried use something like this:

$(this).carousel("next");
$(this).carousel("next");

But it does not work. Any help will be appreciated.

UPDATE

I found a workaround:

test = setInterval(() => {
  count++;
  $(".carousel-control-next").click();
  if (count > 3) {
    count = 0;
    clearInterval(test);
  }
}, 200);

I used an interval because the following does not work:

  • $(".carousel-control-next").click() inside a loop, or
  • $(".carousel-control-next").click() inside a setTimeout inside a loop

答案1

得分: 2

Sure, here is the translated content without the code:

首先,您需要在项目中包含所需的JavaScript和CSS,创建必要的HTML,并使用内置的轮播类。

  • 在 "head" 中添加以下链接:
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

  • 在 "body" 的末尾添加以下脚本:
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

然后,使用轮播类和唯一的ID创建轮播HTML。结构应包括指示器、项目和导航控件:

<div id="myCarousel" class="carousel slide" data-ride="carousel">
  <!-- 指示器 -->
  <ul class="carousel-indicators">
    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
    <li data-target="#myCarousel" data-slide-to="1"></li>
    <li data-target="#myCarousel" data-slide-to="2"></li>
  </ul>

  <!-- 项目 -->
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="img1.jpg" alt="图像1" width="100%" height="100%">
      <div class="carousel-caption">
        <h3>图像1</h3>
        <p>描述1</p>
      </div>
    </div>
    <!-- 其他项目的结构类似 -->
  </div>

  <!-- 导航控件 -->
  <a class="carousel-control-prev" href="#myCarousel" data-slide="prev">
    <span class="carousel-control-prev-icon"></span>
  </a>
  <a class="carousel-control-next" href="#myCarousel" data-slide="next">
    <span class="carousel-control-next-icon"></span>
  </a>
</div>

将 "img1.jpg"、"img2.jpg" 和 "img3.jpg" 替换为您要使用的图像路径或链接(URL)。

最后一步,激活轮播。当将 data-ride="carousel" 属性添加到主轮播 div 元素时,Bootstrap会自动激活它。

希望这有助于回答您的问题。

英文:

first of all you need to include the required JavaScript and CSS in your project, create the necessary HTML and use build in carousel class.

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css&quot;&gt;
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;

link in the "head" and scripts in the end of "body".
Then create the Carousel HTML using the carousel class and a unique IDs. The structure should include indicators, items, and nav controls:

&lt;div id=&quot;myCarousel&quot; class=&quot;carousel slide&quot; data-ride=&quot;carousel&quot;&gt;


&lt;!-- Indic--&gt;
  &lt;ul class=&quot;carousel-indicators&quot;&gt;
    &lt;li data-target=&quot;#myCarousel&quot; data-slide-to=&quot;0&quot; class=&quot;active&quot;&gt;&lt;/li&gt;
    &lt;li data-target=&quot;#myCarousel&quot; data-slide-to=&quot;1&quot;&gt;&lt;/li&gt;
    &lt;li data-target=&quot;#myCarousel&quot; data-slide-to=&quot;2&quot;&gt;&lt;/li&gt;
  &lt;/ul&gt;
  
  &lt;!-- slides --&gt;
  &lt;div class=&quot;carousel-inner&quot;&gt;
    &lt;div class=&quot;carousel-item active&quot;&gt;
      &lt;img src=&quot;img1.jpg&quot; alt=&quot;Image 1&quot; width=&quot;100%&quot; height=&quot;100%&quot;&gt;
      &lt;div class=&quot;carousel-caption&quot;&gt;
        &lt;h3&gt;Image 1&lt;/h3&gt;
        &lt;p&gt;Description 1&lt;/p&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;carousel-item&quot;&gt;
      &lt;img src=&quot;img2.jpg&quot; alt=&quot;Image 2&quot; width=&quot;100%&quot; height=&quot;100%&quot;&gt;
      &lt;div class=&quot;carousel-caption&quot;&gt;
        &lt;h3&gt;Image 2&lt;/h3&gt;
        &lt;p&gt;Description 2&lt;/p&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;carousel-item&quot;&gt;
      &lt;img src=&quot;img3.jpg&quot; alt=&quot;Image 3&quot; width=&quot;100%&quot; height=&quot;100%&quot;&gt;
      &lt;div class=&quot;carousel-caption&quot;&gt;
        &lt;h3&gt;Image 3&lt;/h3&gt;
        &lt;p&gt;Description 3&lt;/p&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;!-- Contr--&gt;
  &lt;a class=&quot;carousel-control-prev&quot; href=&quot;#myCarousel&quot; data-slide=&quot;prev&quot;&gt;
    &lt;span class=&quot;carousel-control-prev-icon&quot;&gt;&lt;/span&gt;
  &lt;/a&gt;
  &lt;a class=&quot;carousel-control-next&quot; href=&quot;#myCarousel&quot; data-slide=&quot;next&quot;&gt;
    &lt;span class=&quot;carousel-control-next-icon&quot;&gt;&lt;/span&gt;
  &lt;/a&gt;
&lt;/div&gt;

Swap "img1.jpg", "img2.jpg", and "img3.jpg" with the your image paths or links (urls) you want to use.
And last thing, activate the carousel, bootstrap automatically activates the it when the data-ride=&quot;carousel&quot; attribute is added to the main Carousel div element.
Also you can check more info here: carousel bootstrap docs.

Hope this helps and answers your question.

Almost forgot:

$(document).ready(function() {
  // Initialize the carousel
  $(&quot;#myCarousel&quot;).carousel({ interval: false });

  // Custom next function
  $(&quot;.carousel-control-next&quot;).click(function() {
    moveCarousel(4);
  });

  // Custom prev function
  $(&quot;.carousel-control-prev&quot;).click(function() {
    moveCarousel(-4);
  });

  // Move the carousel by the specified number of slides
  function moveCarousel(steps) {
    var currentIndex = $(&quot;#myCarousel .carousel-inner .carousel-item.active&quot;).index();
    var totalItems = $(&quot;#myCarousel .carousel-inner .carousel-item&quot;).length;

    var targetIndex = currentIndex + steps;
    if (targetIndex &lt; 0) {
      targetIndex = 0;
    } else if (targetIndex &gt;= totalItems) {
      targetIndex = totalItems - 1;
    }

    $(&quot;#myCarousel&quot;).carousel(targetIndex);
  }
});

To enable smooth transition effect modify code:

// Move the carousel by the specified number of slides
function moveCarousel(steps) {
  var currentIndex = $(&quot;#myCarousel .carousel-inner .carousel-item.active&quot;).index();
  var totalItems = $(&quot;#myCarousel .carousel-inner .carousel-item&quot;).length;
  var remainingSteps = steps;

  function moveOneStep() {
    var newIndex = currentIndex + (remainingSteps &gt; 0 ? 1 : -1);
    if (newIndex &lt; 0 || newIndex &gt;= totalItems) {
      return;
    }

    $(&quot;#myCarousel&quot;).one(&quot;slid.bs.carousel&quot;, function() {
      remainingSteps -= (remainingSteps &gt; 0 ? 1 : -1);
      if (remainingSteps !== 0) {
        moveOneStep();
      }
    });

    $(&quot;#myCarousel&quot;).carousel(newIndex);
    currentIndex = newIndex;
  }

  moveOneStep();
}

答案2

得分: 0

我不确定为什么 .carousel(4) 在你那里没有动画效果,但在基本示例中是有的:

两点注意:

  • .carousel(4) 跳转到索引为 4 的幻灯片(即第五张幻灯片),不考虑当前幻灯片。如果要从当前幻灯片向前跳 4 个幻灯片,请使用以下代码:
let index = $(this).find(".active").index();
$(this).carousel(index + 4);
  • 动画直接从当前幻灯片跳转到目标幻灯片,跳过任何中间幻灯片。如果希望动画经过所有中间幻灯片,请查看@David NoHorizon的答案

我注意到调用 .carousel("next") 两次对我也不起作用(只会向前移动一张幻灯片)。

英文:

I am not sure why .carousel(4) doesn't animate for you, it does on a basic example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!-- https://getbootstrap.com/docs/4.6/getting-started/introduction/#starter-template --&gt;
&lt;!doctype html&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1, shrink-to-fit=no&quot;&gt;

    &lt;!-- Bootstrap CSS --&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css&quot; integrity=&quot;sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N&quot; crossorigin=&quot;anonymous&quot;&gt;
  &lt;/head&gt;
  &lt;body&gt;

    &lt;!-- https://getbootstrap.com/docs/4.6/components/carousel/#slides-only --&gt;
    &lt;div id=&quot;carouselExampleSlidesOnly&quot; class=&quot;carousel slide&quot; data-ride=&quot;carousel&quot;&gt;
      &lt;div class=&quot;carousel-inner&quot;&gt;
        &lt;div class=&quot;carousel-item active&quot;&gt;&lt;svg class=&quot;bd-placeholder-img bd-placeholder-img-lg d-block w-100&quot; width=&quot;200&quot; height=&quot;100&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; role=&quot;img&quot; aria-label=&quot;Placeholder: First slide&quot; preserveAspectRatio=&quot;xMidYMid slice&quot; focusable=&quot;false&quot;&gt;&lt;title&gt;Placeholder&lt;/title&gt;&lt;rect width=&quot;100%&quot; height=&quot;100%&quot; fill=&quot;#777&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;50%&quot; y=&quot;50%&quot; fill=&quot;#555&quot; dy=&quot;.3em&quot;&gt;First slide&lt;/text&gt;&lt;/svg&gt;&lt;/div&gt;
        &lt;div class=&quot;carousel-item&quot;&gt;&lt;svg class=&quot;bd-placeholder-img bd-placeholder-img-lg d-block w-100&quot; width=&quot;200&quot; height=&quot;100&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; role=&quot;img&quot; aria-label=&quot;Placeholder: Second slide&quot; preserveAspectRatio=&quot;xMidYMid slice&quot; focusable=&quot;false&quot;&gt;&lt;title&gt;Placeholder&lt;/title&gt;&lt;rect width=&quot;100%&quot; height=&quot;100%&quot; fill=&quot;#666&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;50%&quot; y=&quot;50%&quot; fill=&quot;#444&quot; dy=&quot;.3em&quot;&gt;Second slide&lt;/text&gt;&lt;/svg&gt;&lt;/div&gt;
        &lt;div class=&quot;carousel-item&quot;&gt;&lt;svg class=&quot;bd-placeholder-img bd-placeholder-img-lg d-block w-100&quot; width=&quot;200&quot; height=&quot;100&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; role=&quot;img&quot; aria-label=&quot;Placeholder: Third slide&quot; preserveAspectRatio=&quot;xMidYMid slice&quot; focusable=&quot;false&quot;&gt;&lt;title&gt;Placeholder&lt;/title&gt;&lt;rect width=&quot;100%&quot; height=&quot;100%&quot; fill=&quot;#555&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;50%&quot; y=&quot;50%&quot; fill=&quot;#333&quot; dy=&quot;.3em&quot;&gt;Third slide&lt;/text&gt;&lt;/svg&gt;&lt;/div&gt;
        &lt;div class=&quot;carousel-item&quot;&gt;&lt;svg class=&quot;bd-placeholder-img bd-placeholder-img-lg d-block w-100&quot; width=&quot;200&quot; height=&quot;100&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; role=&quot;img&quot; aria-label=&quot;Placeholder: Fourth slide&quot; preserveAspectRatio=&quot;xMidYMid slice&quot; focusable=&quot;false&quot;&gt;&lt;title&gt;Placeholder&lt;/title&gt;&lt;rect width=&quot;100%&quot; height=&quot;100%&quot; fill=&quot;#777&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;50%&quot; y=&quot;50%&quot; fill=&quot;#555&quot; dy=&quot;.3em&quot;&gt;Fourth slide&lt;/text&gt;&lt;/svg&gt;&lt;/div&gt;
        &lt;div class=&quot;carousel-item&quot;&gt;&lt;svg class=&quot;bd-placeholder-img bd-placeholder-img-lg d-block w-100&quot; width=&quot;200&quot; height=&quot;100&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; role=&quot;img&quot; aria-label=&quot;Placeholder: Fifth slide&quot; preserveAspectRatio=&quot;xMidYMid slice&quot; focusable=&quot;false&quot;&gt;&lt;title&gt;Placeholder&lt;/title&gt;&lt;rect width=&quot;100%&quot; height=&quot;100%&quot; fill=&quot;#666&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;50%&quot; y=&quot;50%&quot; fill=&quot;#444&quot; dy=&quot;.3em&quot;&gt;Fifth slide&lt;/text&gt;&lt;/svg&gt;&lt;/div&gt;
        &lt;div class=&quot;carousel-item&quot;&gt;&lt;svg class=&quot;bd-placeholder-img bd-placeholder-img-lg d-block w-100&quot; width=&quot;200&quot; height=&quot;100&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; role=&quot;img&quot; aria-label=&quot;Placeholder: Sixth slide&quot; preserveAspectRatio=&quot;xMidYMid slice&quot; focusable=&quot;false&quot;&gt;&lt;title&gt;Placeholder&lt;/title&gt;&lt;rect width=&quot;100%&quot; height=&quot;100%&quot; fill=&quot;#555&quot;&gt;&lt;/rect&gt;&lt;text x=&quot;50%&quot; y=&quot;50%&quot; fill=&quot;#333&quot; dy=&quot;.3em&quot;&gt;Sixth slide&lt;/text&gt;&lt;/svg&gt;&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;

    &lt;button onclick=&quot;$(&#39;#carouselExampleSlidesOnly&#39;).carousel(4)&quot;&gt;Skip to fifth slide&lt;/button&gt;

    &lt;!-- jQuery and Bootstrap Bundle (includes Popper) --&gt;
    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js&quot; integrity=&quot;sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js&quot; integrity=&quot;sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

Two notes:

  • .carousel(4) jumps to the slide at index 4 (i.e. the fifth slide), regardless of the current slide. If you want to jump 4 forward from the current slide, use

    let index = $(this).find(&quot;.active&quot;).index();
    $(this).carousel(index + 4);
    
  • The animation goes directly from the current slide to the target slide, skipping over any intermediate slides. If you want the animation to go through all the intermediate slides, have a look at @David NoHorizon's answer.

I noticed that calling .carousel(&quot;next&quot;) twice doesn't work for me either (it only goes forward one slide).

huangapple
  • 本文由 发表于 2023年4月16日 23:30:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定