简体   繁体   English

实现轮播作物文本

[英]Materialize Carousel Crops Text

I have a Materialize carousel that is cropping my text at the bottom.我有一个 Materialize 轮播,它在底部裁剪我的文本。 I created a very simple example to see if it was something to do with the content.我创建了一个非常简单的示例来查看它是否与内容有关。 This is what I reduced the html to:这就是我将 html 简化为:

        <div class="carousel carousel-slider">
        <div class="carousel-item" href="#one!">
                <div>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
                        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                        sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
                        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                        sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
                        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                        sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
                        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                        sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
                        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                        sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
                        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                        sunt in culpa qui officia deserunt mollit anim id est laborum.LAST</p>
                </div>
        </div>
    </div>

The last paragraph is cropped out.最后一段被剪掉了。 This is the javascript:这是 javascript:

    ...
    $('.carousel').carousel({
        fullwidth: true,
    });

    $('.carousel.carousel-slider').carousel({
        fullwidth: true,
        indicators: true,
        duration: 200,
    }, setTimeout(autoplay, 4500));

     function autoplay() {
       $('.carousel').carousel('next');
       setTimeout(autoplay, 4500);
     }

The carousel appears to be a fixed height.旋转木马似乎是固定高度。 I can add a fixed height in the carousel class large enough to prevent cropping on a phone.我可以在旋转木马 class 中添加一个足够大的固定高度,以防止在手机上裁剪。 That leaves me with a large blank space on a desktop.这让我在桌面上留下了很大的空白空间。 I've tried placing the override in media queries.我试过将覆盖放在媒体查询中。 It is ignored.它被忽略。 Is there any way to make it auto size?有没有办法让它自动调整大小? I did try height:auto;我确实尝试过 height:auto; to no avail.无济于事。

One way to solve this is to set the height of the carousel based on the size of the content:解决这个问题的一种方法是根据内容的大小设置轮播的高度:

// Get height of content
var contentHeight = jQuery('.carousel-item.active div').height();

//Override the 3 height attributes that Materialize adds:

jQuery('.carousel').css('height', contentHeight);
jQuery('.carousel-item').css('height', contentHeight);
jQuery('.carousel-item').css('min-height', contentHeight);

Wrap this in a function, run it on page load, and anytime the window is resized:将其包装在 function 中,在页面加载时运行它,并且只要调整 window 的大小:

function setCarouselHeight(){
   var contentHeight = jQuery('.carousel-item.active div').height();
   jQuery('.carousel').css('height', contentHeight);
   jQuery('.carousel-item').css('height', contentHeight);
   jQuery('.carousel-item').css('min-height', contentHeight);
}

setCarouselHeight();

$( window ).resize(function() {
  setCarouselHeight();
});

Working codepen here .在这里工作codepen。

UPDATE更新

So, having looked at this again, it's actually much more difficult that it first seems.所以,再看一遍,实际上比最初看起来要困难得多。 The codepen is a now work in progress. Codepen 正在进行中。

The issue is that Materialize dynamically updates the transform values of the carousel to handle mouse and touch drag - and it is often putting whitespace up top when it is not needed.问题是 Materialize 会动态更新轮播的变换值以处理鼠标和触摸拖动 - 并且它通常在不需要时将空格放在顶部。

My solution is getting very complicated now - a second function that recalculates the transform X&Y and updated with margin accordingly.我的解决方案现在变得非常复杂 - 第二个 function 重新计算变换 X&Y 并相应地更新边距。

I would love to see a simpler solution.我很想看到一个更简单的解决方案。 Watch this space.关注此空间。

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

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