Jquery ui图像循环仪

时间:2013-04-17 14:21:27

标签: jquery image jquery-ui

我一直在使用jquery ui开发。今天我被困在图像循环器上。默认图像滑块代码适用于3张图片。我在我的网站上有30张图片,我必须使用ui-cycler流畅地循环,但这是不可能的。为了使它工作,我必须在图像循环器开始播放前点击X时间的静态图片。这是3张图片的循环代码:

<script>
    $(function() {
        // TODO refactor into a widget and get rid of these plugin methods
        $.fn.left = function( using ) {
            return this.position({
                my: "right middle",
                at: "left+25 middle",
                of: "#container",
                collision: "none",
                using: using
            });
        };
        $.fn.right = function( using ) {
            return this.position({
                my: "left middle",
                at: "right-25 middle",
                of: "#container",
                collision: "none",
                using: using
            });
        };
        $.fn.center = function( using ) {
            return this.position({
                my: "center middle",
                at: "center middle",
                of: "#container",
                using: using
            });
        };

        $( "img:eq(0)" ).left();
        $( "img:eq(1)" ).center();
        $( "img:eq(2)" ).right();

        function animate( to ) {
            $( this ).stop( true, false ).animate( to );
        }
        function next( event ) {
            event.preventDefault();
            $( "img:eq(2)" ).center( animate );
            $( "img:eq(1)" ).left( animate )
            $( "img:eq(0)" ).right().appendTo( "#container" );
        }
        function previous( event ) {
            event.preventDefault();
            $( "img:eq(0)" ).center( animate );
            $( "img:eq(1)" ).right( animate );
            $( "img:eq(2)" ).left().prependTo( "#container" );
        }
        $( "#previous" ).click( previous );
        $( "#next" ).click( next );

        $( "img" ).click(function( event ) {
            $( "img" ).index( this ) === 0 ? previous( event ) : next( event );
        });

        $( window ).resize(function() {
            $( "img:eq(0)" ).left( animate );
            $( "img:eq(1)" ).center( animate );
            $( "img:eq(2)" ).right( animate );
        });
    });
    </script>

这是我的图片参考html:

    <div id="container">

        <img class = "slide"  src="images/slide1.jpg"  alt="Race2"/>
        <img class="slide" src="images/slide2.jpg"  alt="Akshay Kumar"/>
        <img class = "slide" src = "images/slide_eng1.jpg" alt = "English Roller"/>
        <img class = "slide" src = "images/slide_eng2.jpg" alt = "Bruce Willis"/>
<!-- still many image -->
    </div>

我该怎么做才能让它发挥作用?

1 个答案:

答案 0 :(得分:0)

首先,您必须使用函数ext_right隐藏其余图像(索引3及以上),您应该更改附加和前置,如下所示:

<script>
        $(function () {
            // TODO refactor into a widget and get rid of these plugin methods
            $.fn.left = function (using) {
                return this.position({
                    my: "right middle",
                    at: "left+25 middle",
                    of: "#image_container",
                    collision: "none",
                    using: using
                });
            };
            $.fn.right = function (using) {
                return this.position({
                    my: "left middle",
                    at: "right-25 middle",
                    of: "#image_container",
                    collision: "none",
                    using: using
                });
            };
            $.fn.center = function (using) {
                return this.position({
                    my: "center middle",
                    at: "center middle",
                    of: "#image_container",
                    using: using
                });
            };

            $.fn.ext_right = function () {
                return this.position({
                    my: "left middle",
                    at: "right+30 middle",
                    of: "#image_container",
                    collision: "none",
                });
            };

            $("img.cycler:eq(0)").left();
            $("img.cycler:eq(1)").center();
            $("img.cycler:eq(2)").right();

            /*Place the rest of the image_container images hidden to the right*/
            $("img.cycler:gt(2)").ext_right();

            function animate(to) {
                $(this).stop(true, false).animate(to);
            }

            function next(event) {
                event.preventDefault();
                $("img.cycler:eq(2)").center(animate);
                $("img.cycler:eq(1)").left(animate)
                //$("img.cycler:eq(0)").right().appendTo("#image_container");                
                $("img.cycler:eq(3)").right();
                $("img.cycler:eq(0)").ext_right().appendTo("#image_container");
            }
            function previous(event) {
                event.preventDefault();
                $("img.cycler:eq(0)").center(animate);
                $("img.cycler:eq(1)").right(animate);                                
                //$("img.cycler:eq(2)").left().prependTo("#image_container");
                $("img.cycler:eq(2)").ext_right();
                $("img.cycler:last").left().prependTo("#image_container");                                
            }

            $("#previous").click(previous);
            $("#next").click(next);

            $("img.cycler").click(function (event) {
                $("img.cycler").index(this) === 0 ? previous(event) : next(event);
            });

            $(window).resize(function () {
                $("img.cycler:eq(0)").left(animate);
                $("img.cycler:eq(1)").center(animate);
                $("img.cycler:eq(2)").right(animate);
            });
        });
    </script>

以下是图片:

<div id="image_container">                    
                    <img class="cycler" src="images/flight.jpg" width="512" height="307" alt="flight" />
                    <img class="cycler" src="images/rocket.jpg" width="300" height="353" alt="rocket" />
                    <img class="cycler" src="images/earth.jpg" width="458" height="308" alt="earth" /> 
                    <img class="cycler" src="images/high_tatras.jpg" width="512" height="307" alt="flight" />
                    <img class="cycler" src="images/high_tatras2.jpg" width="300" height="353" alt="rocket" />
                    <img class="cycler" src="images/high_tatras3.jpg" width="458" height="308" alt="earth" />                    
                <a id="previous" href="#">Previous</a>  <a id="next" href="#">Next</a>
</div>