简体   繁体   中英

JS and HTML slideshow jumps is not using position absolute

I am trying to create a simple Div Slideshow, here is what I have so far: http://jsfiddle.net/ZZLHF/

HTML:

<div class="slideShow">
    <div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled including versions of Lorem Ipsum.
    </div>
    <div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span>
    </div>
</div>

JS:

//Side Show
$(function () {
    $('.slideShow div:gt(0)').hide();
    setInterval(function () {
        $('.slideShow > :first-child').fadeOut().next('div').fadeIn().end().appendTo('.slideShow');
    }, 1200);
});

As you can see in the JSfiddle - it slides but it jumps! and that is because I have two divs and each have a different amount of content, so there is no height specified, and also because I am not adding position relative to the .slideshow and absolute to the divs inside it. Why? Because if I did I will have to give it a fixed height.

How can I stop it from jumping and give it height auto?

Use a function callback like this:

//Side Show
$(function () {
    $('.slideShow div:gt(0)').hide();

    setInterval(function () {
        // 400 is default value
        $('.slideShow > :first-child').fadeOut(400, function(){
            $(this).next('div').fadeIn().end().appendTo('.slideShow');
        })
    }, 1200);
});

The function passed to .fadeOut means that when fadeOut is complete, perform the actions specified in the function.

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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