开发者

Scrollable, Carousel, Slider in jquery without any plugin - the simplest way

开发者 https://www.devze.com 2023-02-15 17:17 出处:网络
I looking-for tutorial about jQuery slider (like scrollable plugin) with cycle animation. Without any plugin开发者_JAVA百科, the simplest way, tutorialUPDATED: 27/08/2014

I looking-for tutorial about jQuery slider (like scrollable plugin) with cycle animation. Without any plugin开发者_JAVA百科, the simplest way, tutorial


UPDATED: 27/08/2014

  • DEMO: http://so.lucafilosofi.com/scrollable-carousel-slider-in-jquery-without-any-plugin-the-simplest-way
    $(function() {
        /* author: Luca Filosofi * contact: aseptik@gmail.com * http://devilmaycode.it * license: Public * Updated: 27/08/2014 */
        var animating = false, iXS = 3, $slider = $('.panel-inner'), liW = $slider.find('li:first').width(), liFW = (liW * $slider.find('li').length);
        $slider.width(liFW);
        $('.button a').on('click', function(e) {
            e.preventDefault();
            if(!animating){
                var left = Math.abs(parseInt($slider.css('left')));
                var side = ($(this).data('direction') == 'right')  ? (((left + (liW * iXS)) >= liFW) ? 0 : -(left + liW)) : ((left > 0) ? -(left - liW) : -(liFW - (liW * iXS)));
                rotate(side);
            }
        });
        var rotate = function(leftY) {
            if(!animating){
                animating = true;
                $slider.stop(true, true).animate({left : leftY}, 500, function(){animating = false;});
            }
        }
    });


DEMO

https://jsfiddle.net/w9wjk22n/

I write some code:

    <script type="text/javascript"> 
        $(document).ready(function() {      
            $('a.next').click(function() {
                var duplicate = $('#a li:first').clone();

                //
                $(duplicate).appendTo('#a ul');

                $('#a ul').animate({
                    marginLeft: '-=52px'
                }, 2000, 'linear', function() {
                    $('#a li:first').remove();
                }); 


            });
        });
</script>
<style type="text/css">
        #a {
            list-style: none;
            width: 52px;
            height: 50px;
            border: 1px solid blue;
            margin-left: 200px;
        }   
        #a ul {
            margin: 0;
            margin-left: -104px;
            padding: 0;
            width: 420px;
            border: 1px solid green;
            height: 50px;
            position: absolute;
        }   
        #a ul li {
            border: 1px solid red;
            float: left;
            width: 50px;
            text-align: center;
        }   
        .services {
            border: 1px solid green;
            padding: 5px;
            width: 300px;
            margin-left: 100px;
            display: none;
        }</style>
    <a href="#" class="next">next</a>
    <div id="a">
        <ul>
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
            <li>7</li>
            <li>8</li>
        </ul>
    </div>

...but when I remove first element, the elements moved to left after the animate, and when I clicked a few times 'next' the element will be outside blue container.

0

精彩评论

暂无评论...
验证码 换一张
取 消