简体   繁体   English

使用“white-space:nowrap”会导致div幻灯片显示问题

[英]Using “white-space: nowrap” causes issue with div slideshow

I have an automatic JavaScript slideshow inside a section. 我在一个部分内有一个自动JavaScript幻灯片。 The slideshow uses divs instead of images. 幻灯片显示使用div而不是图像。 When the divs are empty, the slideshow works fine, but when I put an element inside however, the whole div gets pushed down a lot into the section. 当div为空时,幻灯片放映工作正常,但是当我放入一个元素时,整个div被大量推入该部分。

I tried moving the individual elements with by applying negative margin-top values, but it didn't work. 我尝试通过应用负margin-top值来移动单个元素,但它不起作用。 After messing with the code, I found that when I take out white-space: nowrap from the section's CSS, the div doesn't get pushed down, but it doesn't slide anymore. 在弄乱代码之后,我发现当我从部分的CSS中取出white-space: nowrap ,div不会被推下,但它不会再滑动了。

<section style="margin-top: 167px">
    <section id="home">
        <div id="home1">

        </div>
        <div id="home2">

        </div>
        <div id="home3">

        </div>
    </section>
</section>
section{
    width: 99.9%;
    height: 750px;
    margin-top: 150px;
    background-color: white;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

#home{
    width: 100%;
    overflow: hidden;
    white-space: nowrap;
}

#home1{
    background-image: url("../img/home1.jpg");
    background-size: cover;
    background-repeat: no-repeat;
    text-align: center;
}

#home2{
    margin-left: -4px;
    background-image: url("../img/home2.jpg");
    background-size: cover;
    background-repeat: no-repeat;
}

#home3{
    margin-left: -4px;
    background-image: url("../img/home3.jpg");
    background-size: cover;
    background-repeat: no-repeat;
}

#home div{
    display: inline-block;
    width: 100%;
    height: 100%;
}
$(document).ready(function(){
    const NUMBER_OF_SLIDES = 3;
    var slide_width = document.getElementById('home').scrollWidth / NUMBER_OF_SLIDES; 
    var slide_number = 1;

    setInterval(function(){
        if (slide_number < NUMBER_OF_SLIDES){
            $("#home").animate({
                scrollLeft: slide_width * slide_number
            },500);                 
            slide_number++; 
        }else{
            $("#home").animate({
                scrollLeft: 0
            },500);
            slide_number = 1;
        }
    }, 4500);   
});

I want to be able to put elements inside my divs without the divs being pushed down and sill have the slideshow work. 我希望能够将元素放在我的div中,而不会将div推下来,并且窗格可以播放幻灯片。

Probably not the best solution, but what you can do to make it work is encapsulate the content of each slide inside an absolute positioned div . 可能不是最好的解决方案,但是你可以做的就是将每个幻灯片的内容封装在absolute定位的div Something like: 就像是:

<div id="home1">
    <div class="content">Slide 1</div>
</div>
<div id="home2">
    <div class="content">Slide 2</div>
</div>
...

CSS: CSS:

#home .content {
    position: absolute;
}

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

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