【问题标题】:Bootstrap CSS/Javascript For Carousel OnlyBootstrap CSS/Javascript 仅用于轮播
【发布时间】:2020-04-24 13:39:17
【问题描述】:

引导 CSS/Javascript 仅用于轮播

目前使用带控件的轮播

https://getbootstrap.com/docs/4.0/components/carousel/

<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
     <video controls="true">
      <source src="www.youtube.com/watch?v=3bGNuRtlqAQ" type="video/mp4" />
    </video>
    </div>
    <div class="carousel-item">
     <video controls="true">
       <source src="www.youtube.com/watch?v=3bGNuRtlqAQ" type="video/mp4" />
    </video>

    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="..." alt="Third slide">
    </div>
  </div>
  <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

谁有这个功能的 CSS 代码和 Javascript 代码? (不想要整个文件,因为它会与我的主要 CSS 冲突)

【问题讨论】:

  • 这个函数的Javascript代码是什么意思?你到底需要什么?请说得更具体些。
  • 好的,所以目前我已经为我的页面实现了“引导轮播”,但它需要额外的 CSS 和 js 文件来运行它。但是如果我链接“Bootstrap CSS and JS”,它将与我的原始 CSS 文件冲突,并导致我一半的页面内容混乱
  • 我不这么认为,因为你只对这个轮播使用引导类,它只会影响你的轮播样式,而不是你的其他硬编码 css 样式。除非您在自己的 css 文件中使用引导类命名。
  • 只需将您的覆盖 css 放在引导 css 之后。您还可以获取这篇文章中提到的单独的 Bootstrap 代码:stackoverflow.com/questions/23991987/…,其中提到了这个页面:getbootstrap.com/docs/3.4/customize 或单击 Bootstrap 4 的顶部栏。
  • 要调用多个实例,请使用唯一的 id 或类,然后使用引导轮播 js 回调触发它。

标签: javascript html css bootstrap-4


【解决方案1】:

如果您使用像 Webpack 这样的打包工具,您可以按照 here 中的说明导入和编译各个组件

【讨论】:

    【解决方案2】:

    如果您的网站除了滑块部分之外的其他部分不必使用引导程序,那么我建议您仅将外部库用于滑块,因为从引导程序库中仅过滤滑块代码并不容易。

    这是仅滑块的参考网站链接:https://kenwheeler.github.io/slick/

    这个滑块很容易自定义,并且有很多选项可以使用 Jquery 设置滑块属性。

    我希望这将帮助您解决您的问题,这只是 SLider 库,因此它不会与您的主 css 冲突,因此您可以轻松地将这个库添加到您的代码中。

    谢谢...

    【讨论】:

    • 嘿,你看过这个答案吗?
    【解决方案3】:

    我找到了一个旧的Pen 并对其进行了一些定制以满足您的需求。

    这是一个独立的 Bootstrap 4 Carousel,没有依赖项(甚至是 jQuery):

    更新1:添加图片源并将默认高度设置为70vh

    更新 2: 添加了视频 - iframe 支持。似乎 SOF 不允许使用视频/iframe 背景,所以这里是 codepen 来源。

    const carousel = document.getElementById('carouselExampleControls')
    const items = carousel.querySelectorAll('.carousel-item');
    let currentItem = 0;
    let isActive = true;
    
    function setCurrentItem(index) {
      currentItem = (index + items.length) % items.length;
    }
    
    function hideItem(direction) {
      isActive = false;
      items[currentItem].classList.add(direction);
      items[currentItem].addEventListener('animationend', function() {
        this.classList.remove('active', direction);
      });
    }
    
    function showItem(direction) {
      items[currentItem].classList.add('next', direction);
      items[currentItem].addEventListener('animationend', function() {
        this.classList.remove('next', direction);
        this.classList.add('active');
        isActive = true;
      });
    }
    
    document.getElementById('carouselPrev').addEventListener('click', function(e) {
      e.preventDefault()
      if (isActive) {
        hideItem('to-right');
        setCurrentItem(currentItem - 1);
        showItem('from-left');
      }
    });
    
    document.getElementById('carouselNext').addEventListener('click', function(e) {
      e.preventDefault()
      if (isActive) {
        hideItem('to-left');
        setCurrentItem(currentItem + 1);
        showItem('from-right');
      }
    });
    *,
    *::after,
    *::before {
      box-sizing: border-box;
    }
    
    img {
      display: block;
      vertical-align: middle;
    }
    
    body {
      margin: 0;
      font-family: sans-serif;
    }
    
    .carousel {
      position: relative;
    }
    
    .carousel-inner {
      position: relative;
      width: 100%;
      overflow: hidden;
    }
    
    .carousel-inner>.carousel-item {
      position: relative;
      display: none;
      animation: 0.6s ease-in-out;
      height: 70vh; /* Set your height */
    }
    
    .carousel-item>.carousel-img {
      width: 100%;
      min-height: 70vh; /* same height */
      height: auto;
    }
    .carousel-item.carousel-video {
      display: block;
      object-fit: cover;
      position: absolute;
      top: 50%;
      left: 50%;
      min-width: 100%;
      min-height: 100%;
      width: auto;
      height: auto;
      z-index: 0;
      -webkit-transform: translateX(-50%) translateY(-50%);
      transform: translateX(-50%) translateY(-50%);
      /* uncomment the following line if you want to prevent mouse (or touch) clicks */
      /* pointer-events: none; */
    }
    .carousel-inner>.active,
    .carousel-inner>.next {
      display: block;
    }
    
    .carousel-inner>.next {
      position: absolute;
      top: 0;
      width: 100%;
    }
    
    .carousel-inner>.to-left {
      animation-name: left;
    }
    
    .carousel-inner>.from-right {
      animation-name: right;
    }
    
    .carousel-inner>.to-right {
      animation-name: right;
      animation-direction: reverse;
    }
    
    .carousel-inner>.from-left {
      animation-name: left;
      animation-direction: reverse;
    }
    
    .carousel-control {
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      width: 15%;
      cursor: pointer;
    }
    
    .carousel-control-prev,
    .carousel-control-next {
      position: absolute;
      top: 0;
      bottom: 0;
      z-index: 1;
      display: -ms-flexbox;
      display: flex;
      -ms-flex-align: center;
      align-items: center;
      -ms-flex-pack: center;
      justify-content: center;
      width: 15%;
      color: #fff;
      text-align: center;
      opacity: 0.5;
      transition: opacity 0.15s ease;
    }
    
    @media (prefers-reduced-motion: reduce) {
      .carousel-control-prev,
      .carousel-control-next {
        transition: none;
      }
    }
    
    .carousel-control-prev:hover,
    .carousel-control-prev:focus,
    .carousel-control-next:hover,
    .carousel-control-next:focus {
      color: #fff;
      text-decoration: none;
      outline: 0;
      opacity: 0.9;
    }
    
    .carousel-control-prev {
      left: 0;
    }
    
    .carousel-control-next {
      right: 0;
    }
    
    .carousel-control-prev-icon,
    .carousel-control-next-icon {
      display: inline-block;
      width: 20px;
      height: 20px;
      background: no-repeat 50% / 100% 100%;
    }
    
    .carousel-control-prev-icon {
      background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' width='8' height='8' viewBox='0 0 8 8'%3e%3cpath d='M5.25 0l-4 4 4 4 1.5-1.5L4.25 4l2.5-2.5L5.25 0z'/%3e%3c/svg%3e");
    }
    
    .carousel-control-next-icon {
      background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' width='8' height='8' viewBox='0 0 8 8'%3e%3cpath d='M2.75 0l-1.5 1.5L3.75 4l-2.5 2.5L2.75 8l4-4-4-4z'/%3e%3c/svg%3e");
    }
    
    .sr-only {
      position: absolute;
      width: 1px;
      height: 1px;
      padding: 0;
      margin: -1px;
      overflow: hidden;
      clip: rect(0, 0, 0, 0);
      white-space: nowrap;
      border: 0;
    }
    
    .sr-only-focusable:active,
    .sr-only-focusable:focus {
      position: static;
      width: auto;
      height: auto;
      overflow: visible;
      clip: auto;
      white-space: normal;
    }
    
    @keyframes left {
      from {
        left: 0;
      }
      to {
        left: -100%;
      }
    }
    
    @keyframes right {
      from {
        left: 100%;
      }
      to {
        left: 0;
      }
    }
    <div id="carouselExampleControls" class="carousel">
      <div class="carousel-inner">
        <!-- YouTube Video -->
        <div class="carousel-item active">
          <iframe class="carousel-video" width="100%" height="100%" src="https://www.youtube.com/embed/QEbuc3cgmsI" frameborder="0"
            allowfullscreen></iframe>
          <!-- add "?autoplay=1" at the end of the URL for autoplay i.e. https://www.youtube.com/embed/QEbuc3cgmsI?autoplay=1 -->
        </div>
        <!-- Local Video -->
        <div class="carousel-item">
          <video class="carousel-video" autoplay muted loop playsinline preload="metadata"
            poster="http://techslides.com/demos/sample-videos/small.jpg">
            <source src="http://techslides.com/demos/sample-videos/small.webm" type="video/webm">
            <source src="http://techslides.com/demos/sample-videos/small.ogv" type="video/ogg">
            <source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
          </video>
        </div>
        <!-- Image -->
        <div class="carousel-item">
          <img class="carousel-img" alt="Second slide" src="https://source.unsplash.com/collection/190728/1920x1080">
        </div>
      </div>
    
      <a id="carouselPrev" class="carousel-control-prev" href="#carouselExampleControls" role="button">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
      </a>
      <a id="carouselNext" class="carousel-control-next" href="#carouselExampleControls" role="button">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
      </a>
    </div>

    【讨论】:

    • 抱歉回复慢但是它不是“移动友好”,知道如何让它对移动友好吗?
    • @greenboxgoolu 它在某种程度上“对移动设备友好”,但它取决于您的图像高度以及您是否要向其添加内容。你能给一个链接看看你想要的轮播形式是什么吗?我已经编辑了答案并将默认高度设置为70vh
    • 但是如果是视频呢?
    • 相同的概念,您只需要添加一些额外的 CSS。见here
    • 抱歉回复晚了,下班后会更新的
    【解决方案4】:

    你应该重写你的代码。因为 Bootstrap 是万维网 2.0 的一种模式。使用 Bootstrap 的所有功能,您的网站将看起来很漂亮。请下载完整的引导程序包并使用它,不要做任何事情。使用https://getbootstrap.com/docs/4.4/getting-started/download/。十年后开始后信息时代-Bootstrap 将完成。最后,程序不是数据大小。

    【讨论】:

      【解决方案5】:

      这是 Nathaniel 建议你做的一个例子。

      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.8.0/css/bulma.min.css" />
      <link rel="stylesheet" href="style.css" />
      

      当然,在您的情况下,您使用的是 Bootstrap,但希望这个视觉效果可以帮助您理解 Nathaniels 推荐的那一部分。

      然后你只需获取你需要的 Bootstrap 类名,你就可以在你的 JavaScript 文件中编写的 HTML 中实现它。

      const carouselTemplate = (videoDetail) => {
         return `<div class=“carousel-item-active”>
                  <video controls=“true”>
                    <source src=“${videoDetail.id}”>
                  </video>
                </div>`;
      }
      

      然后您想将该 HTML 渲染到 DOM,因此您可以像这样在 HTML 中创建一个元素:

      <div id="carousel"></div>
      

      然后回到您的 JavaScript 文件中,您执行以下操作:

      document.querySelector('#carousel').innerHTML = carouselTemplate(response.data);
      

      您传入的 response.data 可以是 videoDetail 数据对象,我假设您可能已通过以下方式获取:

      const onVideoSelect = async video => {
        const response = await axios.get('http://www.youtube.com/');
        document.querySelector('#carousel').innerHTML = carouselTemplate(response.data)
      }
      

      axios.get() 中可能缺少一些参数对象,以便 YouTube 获取特定 ID 的视频,但我目前没有 YouTube API 文档。

      【讨论】:

        【解决方案6】:

        我对 Bootstrap Carousel (3.x) 有同样的渴望,但没有 Bootstrap 的所有其余部分。我在这里放了一个小的 git repo 和如何使用它的说明:https://github.com/mdlavin/bootstrap-carousel-standalone

        【讨论】:

          猜你喜欢
          • 2017-07-27
          • 1970-01-01
          • 2019-02-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-27
          • 2014-11-13
          • 2012-10-09
          相关资源
          最近更新 更多