【问题标题】:How to combine materialboxed with materialize carousell?如何将materialboxed与materialize carousell结合起来?
【发布时间】:2020-11-20 23:50:31
【问题描述】:

我之前尝试过使用 materialboxed 来制作卡片图像,并且效果很好。

但是当我对轮播图片使用materialboxed时,结果是不同的,不如卡片图片。

固定的导航栏和下面的任何元素看起来也与卡片图像不同,所有周围的元素都是不可见的。

我尝试从 materialize 中编辑 css,但还是不行。

<div class="carousel">
    <a class="carousel-item" href="#one!"><img src="https://lorempixel.com/250/250/nature/1" class="materialboxed"></a>
    <a class="carousel-item" href="#two!"><img src="https://lorempixel.com/250/250/nature/2" class="materialboxed"></a>
    <a class="carousel-item" href="#three!"><img src="https://lorempixel.com/250/250/nature/3" class="materialboxed"></a>
    <a class="carousel-item" href="#four!"><img src="https://lorempixel.com/250/250/nature/4" class="materialboxed"></a>
    <a class="carousel-item" href="#five!"><img src="https://lorempixel.com/250/250/nature/5" class="materialboxed"></a>
  </div>

the result image materialboxed in carousel

【问题讨论】:

    标签: html css frameworks carousel materialize


    【解决方案1】:

    你写道:

    导航栏和下面的任何元素看起来也与卡片图像不同,所有周围的元素都是不可见的。

    这是否意味着当您在carousel 中使用带有img 标记的materialboxed 类时,当您单击图像时,页面的其余部分不会变得不可见/变黑?

    也许您的文档中有更多未共享的代码?

    我能够使用以下代码在您的轮播中实现materialboxed 效果:

    <!DOCTYPE html>
    <html>
      <head>
    
        <!-- IMPORT MATERIALIZE CSS-->
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
    
      </head>
    
      <body>
    
        <nav>
          <div class="nav-wrapper">
            <a href="#" class="brand-logo">Logo</a>
            <ul id="nav-mobile" class="right hide-on-med-and-down">
              <li><a href="sass.html">Sass</a></li>
              <li><a href="badges.html">Components</a></li>
              <li><a href="collapsible.html">JavaScript</a></li>
            </ul>
          </div>
        </nav>
    
        <div class="carousel">
          <a class="carousel-item" href="#one!"><img src="https://lorempixel.com/250/250/nature/1" class="materialboxed"></a>
          <a class="carousel-item" href="#two!"><img src="https://lorempixel.com/250/250/nature/2" class="materialboxed"></a>
          <a class="carousel-item" href="#three!"><img src="https://lorempixel.com/250/250/nature/3" class="materialboxed"></a>
          <a class="carousel-item" href="#four!"><img src="https://lorempixel.com/250/250/nature/4" class="materialboxed"></a>
          <a class="carousel-item" href="#five!"><img src="https://lorempixel.com/250/250/nature/5" class="materialboxed"></a>
        </div>
    
        <!-- IMPORT MATERIALIZE JS -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
    
        <!-- INIT CAROUSEL -->
        <script>
          document.addEventListener('DOMContentLoaded', function() {
              var elems = document.querySelectorAll('.carousel');
              var instances = M.Carousel.init(elems);
          });
        </script>
    
        <!-- INIT MATERIALBOXED -->
        <script>
          document.addEventListener('DOMContentLoaded', function() {
              var elems = document.querySelectorAll('.materialboxed');
              var instances = M.Materialbox.init(elems);
          });
        </script>
        
      </body>
    </html>
    

    结果见screen grab

    编辑 2020 年 10 月 23 日:

    以下几个假设:

    materialize.js 现在是 html 文件的本地文件,而不是从 CDN 导入的。此外,&lt;nav&gt; 标签现在被包裹在一个带有class="navbar-fixed"id="fixed-nav" 的div 中:

    <div id="fixed-nav" class="navbar-fixed">
        <nav>
            {...}
        </nav>
    </div>
    

    在 cmets 中,OP 澄清说他不喜欢使用 materialboxed + carousel + navbar-fixed 时的行为。

    一种解决方案是在打开轮播图像时从 &lt;nav&gt; 的包装 div 中删除类 navbar-fixed,并在关闭时将其添加回来。

    materialize.js 的第 3590 行和第 3722 行分别是打开和关闭类为 materialboxed 的图像的函数。在这些函数中,您可以添加以下内容以从 id fixed-nav 的包装器中删除/添加 navbar-fixed

    open() 函数内部:

    // remove navbar-fixed
    let fixedNav = document.getElementById('fixed-nav')
    fixedNav.classList.remove('navbar-fixed')
    

    close() 函数内部:

    // add navbar-fixed
    let fixedNav = document.getElementById('fixed-nav')
    fixedNav.classList.add('navbar-fixed')
    

    【讨论】:

    • 对不起,我忘了说它发生在固定导航栏中而不是正常导航栏中。
    • 试着给固定的导航栏类,看看如果你点击轮播图片会发生什么,结果就像我之前说的那样
    • 我能够复制您描述的行为。我用一个解决方案编辑了我的答案,该解决方案可以在没有固定导航的情况下打开图像,并在关闭图像时重新激活固定导航。我不建议大量修改materialize.js,但这样做相对容易,因为许多事件侦听器都封装在我在编辑中描述的open()close() 函数中。对延误表示歉意。
    猜你喜欢
    • 2018-12-08
    • 2012-03-17
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 2021-06-03
    相关资源
    最近更新 更多