调整大小

时间:2017-07-19 11:23:22

标签: jquery resize slider

我已经构建了一个滑块,其中包含自动滑动的证词。当第一次加载网页时,它会计算窗口的宽度,并使用此值在整个文本块中移动,但是当我调整窗口大小时,它会保留以前的值并且一切都是不平衡的。

主要问题是滑块容器和幻灯片的值是百分比,因此尝试$(window).resize(function(){$slides.width(width)});它是无用的。

HTML

<div class="testimonial-container">
            <div class="testimonials">
                <p>Text</p>
            </div>
            <div class="testimonials">
                <p>Text</p>
            </div>
            <div class="testimonials">
                <p>Text</p>
            </div>
            <div class="testimonials">
                <p>Text</p>
            </div>

CSS

.testimonial-container{
    width: 400%;
}
.testimonials{
    width: 25%;
    float: left;
}

JQUERY

$(function(){
        //configuration
        var width=$(window).width();
        var animationSpeed = 1200;
        var pause = 3000;
        var currentSlide = 1;
        var interval;
        //cache DOM
        var $slider = $('#testimonial');
        var $slideContainer = $slider.find('.testimonial-container');
        var $slides = $slideContainer.find('.testimonials');

        function startSlider(){
            interval = setInterval(function(){
                $slideContainer.animate({'margin-left':'-='+width},animationSpeed,"easeOutQuart", function(){
                    currentSlide++;
                    if(currentSlide === $slides.length){
                        currentSlide = 1;
                        $slideContainer.css('margin-left', 0);
                        addActive();
                    }
                });
            }, pause);
        }
        startSlider();
});

1 个答案:

答案 0 :(得分:1)

这是一个工作小提琴。

编辑:使用下面的JSFiddle链接来测试调整大小: - )

我在调整大小时重新计算宽度,停止滑块,重置边距然后再次运行滑块:

&#13;
&#13;
$(function(){
        //configuration
        var width=$(window).width();
        var animationSpeed = 1200;
        var pause = 3000;
        var currentSlide = 1;
        var interval;
        //cache DOM
        var $slideContainer = $('.testimonial-container');
        var $slides = $slideContainer.find('.testimonials');

        function startSlider(){
            interval = setInterval(function(){
                $slideContainer.animate({'margin-left':'-='+width},animationSpeed, function(){
                    currentSlide++;
                    if(currentSlide === $slides.length){
                        currentSlide = 1;
                        $slideContainer.css('margin-left', 0);
                    }
                });
            }, pause);
        }
        startSlider();
        
        $(window).on('resize', function(){
        	clearInterval(interval);
        	width=$(window).width();
            $slideContainer.css('marginLeft', '0');
            startSlider();
        });
});
&#13;
html, body{
    padding:0;
    margin:0;
}
.testimonial-container{
    width: 400%;
}
.testimonials{
    width: 25%;
    float: left;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="testimonial-container">
            <div class="testimonials">
                <p>Text</p>
            </div>
            <div class="testimonials">
                <p>Text</p>
            </div>
            <div class="testimonials">
                <p>Text</p>
            </div>
            <div class="testimonials">
                <p>Text</p>
            </div>
&#13;
&#13;
&#13;

INFO:以下是调整大小测试的JSFiddle: https://jsfiddle.net/ks50njnz/1/