0

这是我在这里的第一个问题。我正在使用从教程中获取的 jQuery 滑块并尝试使其自动滑动。但不幸的是,没有运气。如果有人可以帮助我,我将不胜感激。以下是我的代码:

HTML

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="css/item_slider/slider.css" />
<script src="js/item_slider/modernizr.custom.63321.js"></script>
</head>
<body>
<div id="mi-slider" class="mi-slider fixed-container">
<ul>
<li class="diagram"><img src="http://cdn1.appleinsider.com/ios7-130610-1.jpg" /></li>
<li class="copy">
<h4>Nav Category One</h4>
<p>Paragraph one</p>
<p>Paragraph two</p>
<p>Paragraph three</p>
<p>Paragraph four</p>
</li>
</ul>
<ul>
<li class="diagram"><img src="http://cdn1.appleinsider.com/ios7-130610-1.jpg" /></li>
<li class="copy">
<h4>Nav Category Two</h4>
<p>Paragraph one</p>
<p>Paragraph two</p>
<p>Paragraph three</p>
<p>Paragraph four</p>
</li>
</ul>
<ul>
<li class="diagram"><img src="http://cdn1.appleinsider.com/ios7-130610-1.jpg" /></li>
<li class="copy">
<h4>Nav Category Three</h4>
<p>Paragraph one</p>
<p>Paragraph two</p>
<p>Paragraph three</p>
<p>Paragraph four</p>
</li>
</ul>
<ul>
<li class="diagram"><img src="http://cdn1.appleinsider.com/ios7-130610-1.jpg" /></li>
<li class="copy">
<h4>Nav Category Four</h4>
<p>Paragraph one</p>
<p>Paragraph two</p>
<p>Paragraph three</p>
<p>Paragraph four</p>
</li>
</ul>
<nav>
<a href="#">Nav Category One</a>
<a href="#">Nav Category Two</a>
<a href="#">Nav Category Three</a>
<a href="#">Nav Category Four</a>
</nav>
</div>
<script src="js/item_slider/jquery.catslider.js"></script>
<script>
$(function() {
$('#mi-slider').catslider();
}); 
</script>
</body>
</html>

CSS

.mi-slider {
    position: relative;
    margin-top: 0px;
    height: 360px;
    background:transparent !important;  
    font-family:Arial, Helvetica, sans-serif;
    font-size:12px;
}

.fixed-container {
    max-width:960px;
    margin-left:auto;
    margin-right:auto;
    pointer-events: auto;
}

.mi-slider ul {
    list-style-type: none;
    position: absolute;
    width: 100%;
    left: 0;
    bottom: 60px;
    overflow: hidden;
    text-align: center;
    pointer-events: none;
}

.no-js .mi-slider ul {
    position: relative;
    left: auto;
    bottom: auto;
    margin: 0;
    overflow: visible;
}

.mi-slider ul.mi-current {
    pointer-events: auto;
}

.mi-slider ul li {
    background-color:#ffffff;
    background-position:left top;
    display: inline-block;
    padding: 0px;
    height:125px;
    width: 300px;
    margin:0 10px 10px 0;
    -webkit-transform: translateX(600%);
    transform: translateX(600%);
    -webkit-transition: opacity 0.2s linear;
    transition: opacity 0.2s linear;
    border:1px solid #ddd;
    -webkit-border-radius:4px;
    -moz-border-radius:4px;
    border-radius:4px;
}

.mi-slider ul li:hover {
    background-color:#1290d0;
    color:#ffffff;
    display: inline-block;
    border:1px solid #ddd;
    -webkit-border-radius:4px;
    -moz-border-radius:4px;
    border-radius:4px;
}

.mi-slider ul li.diagram {
    background-color:transparent;
    background-position:left top;
    display: inline-block;
    float:left;
    padding: 0px;
    height:200px;
    width: 420px;
    margin:0 10px 10px 0;
    -webkit-transform: translateX(600%);
    transform: translateX(600%);
    -webkit-transition: opacity 0.2s linear;
    transition: opacity 0.2s linear;
    border:0px solid #ddd;
    -webkit-border-radius:0px;
    -moz-border-radius:0px;
    border-radius:0px;
}

.mi-slider ul li.diagram img {
    position:relative;
    height:208px;
    width:auto;
}

.mi-slider ul li.copy {
    background-color:transparent;
    background-position:left top;
    display: inline-block;
    float:left;
    padding: 0px;
    height:260px;
    width: auto;
    margin:0 10px 10px 20px;
    -webkit-transform: translateX(600%);
    transform: translateX(600%);
    -webkit-transition: opacity 0.2s linear;
    transition: opacity 0.2s linear;
    border:0px solid #ddd;
    -webkit-border-radius:0px;
    -moz-border-radius:0px;
    border-radius:0px;
}

.mi-slider ul li.copy h4 {
    font-weight: 700;
    font-size: 32px;
    line-height:1.1;
    color:#0095D5;
    padding:5px 10px 5px 0;
    margin:0;
    text-align:left;
}

.mi-slider ul li.copy h5 {
    font-weight: 500;
    font-size: 22px;
    line-height:1;
    color:#575656;
    padding:0px 10px 10px 0;
    margin:0;
    text-align:left;
}

.mi-slider ul li.copy p {
    font-weight: 500;
    font-size: 14px;
    line-height:1.5;
    color: #000;
    padding:10px 0px 0 0;
    text-align:left;
}

.mi-slider ul li.copy a {
    font-weight: 500;
    font-size: 14px;
    line-height:1.5;
    color: #000;
    text-align:left;
    text-decoration:none;
    width:auto;
    height:auto;
}

.mi-slider ul li.copy a:hover {
    text-decoration:underline;
}

.mi-slider ul li.copy p span{
    font-weight: 700;
    font-size: 15px;
    color: #0095D5;
}

.mi-slider ul li.copy li {
    font-weight: 500;
    font-size: 14px;
    line-height:1.1;
    border:0;
    color: #000;
    padding:10px 10px 0 0;
    text-align:left;
}

.no-js .mi-slider ul li {
    -webkit-transform: translateX(0);
    transform: translateX(0);
}

.mi-slider ul li a,
.mi-slider ul li img {
    display: block;
    margin: 10px auto;
    width:200px;
    height:50px;
}

.mi-slider ul li a {
    outline: none;
    cursor: pointer;
    padding:0;
    margin:0;
}

.mi-slider ul li h4 {
    font-weight: 500;
    font-size: 14px;
    line-height:1.4;
    padding: 0;
    margin:0 5px;
}

.mi-slider nav {
    position: relative;
    top: 290px;
    text-align: center;
    max-width: 960px;
    margin: 0 auto;
    border-top: 1px solid #333;
    -webkit-transition: opacity 0.2s linear;
    transition: opacity 0.2s linear;
}

.no-js nav {
    display: none;
}

.mi-slider nav a {
    display: inline-block;
    font-weight:bold;
    text-transform: uppercase;
    padding: 30px 30px 30px 34px;
    position: relative;
    color: #888;
    outline: none;
    -webkit-transition: color 0.2s linear;
    transition: color 0.2s linear;
}

.mi-slider nav a:hover,
.mi-slider nav a.mi-selected {
    color: #0095D5;
}

.mi-slider nav a.mi-selected:after,
.mi-slider nav a.mi-selected:before {
    content: '';
    position: absolute;
    top: 0px;
    border: solid transparent;
    height: 0;
    width: 0;
    position: absolute;
    pointer-events: none;
}

.mi-slider nav a.mi-selected:after {
    border-color: transparent;
    border-top-color: #c2c2c2;
    border-width: 15px;
    left: 50%;
    margin-left: -20px;
}

.mi-slider nav a.mi-selected:before {
    border-color: transparent;
    border-top-color: #c2c2c2;
    border-width: 20px;
    left: 50%;
    margin-left: -20px;
}

/* Move classes and animations */

.mi-slider ul:first-child li,
.no-js .mi-slider ul li {
    -webkit-animation: scaleUp 530ms ease-in-out both;
    animation: scaleUp 530ms ease-in-out both;
}

@-webkit-keyframes scaleUp {
    0% { -webkit-transform: translateX(0) scale(0); }
    100% { -webkit-transform: translateX(0) scale(1); }
}

@keyframes scaleUp {
    0% { transform: translateX(0) scale(0); }
    100% { transform: translateX(0) scale(1); }
}

.mi-slider ul:first-child li:first-child {
    -webkit-animation-delay: 90ms;
    animation-delay: 90ms;
}

.mi-slider ul:first-child li:nth-child(2) {
    -webkit-animation-delay: 180ms;
    animation-delay: 180ms;
}

.mi-slider ul:first-child li:nth-child(3) {
    -webkit-animation-delay: 270ms;
    animation-delay: 270ms;
}

.mi-slider ul:first-child li:nth-child(4) {
    -webkit-animation-delay: 360ms;
    animation-delay: 360ms;
}

.mi-slider ul:first-child li:nth-child(5) {
    -webkit-animation-delay: 450ms;
    animation-delay: 450ms;
}

.mi-slider ul:first-child li:nth-child(6) {
    -webkit-animation-delay: 540ms;
    animation-delay: 540ms;
}

/* moveFromRight */

.mi-slider ul.mi-moveFromRight li {
    -webkit-animation: moveFromRight 530ms ease-in-out both;
    animation: moveFromRight 530ms ease-in-out both;
}

/* moveFromLeft */

.mi-slider ul.mi-moveFromLeft li {
    -webkit-animation: moveFromLeft 530ms ease-in-out both;
    animation: moveFromLeft 530ms ease-in-out both;
}

/* moveToRight */

.mi-slider ul.mi-moveToRight li {
    -webkit-animation: moveToRight 530ms ease-in-out both;
    animation: moveToRight 530ms ease-in-out both;
}

/* moveToLeft */

.mi-slider ul.mi-moveToLeft li {
    -webkit-animation: moveToLeft 530ms ease-in-out both;
    animation: moveToLeft 530ms ease-in-out both;
}

/* Animation Delays */

.mi-slider ul.mi-moveToLeft li:first-child,
.mi-slider ul.mi-moveFromRight li:first-child,
.mi-slider ul.mi-moveToRight li:nth-child(6),
.mi-slider ul.mi-moveFromLeft li:nth-child(6) {
    -webkit-animation-delay: 0ms;
    animation-delay: 0ms;
}

.mi-slider ul.mi-moveToLeft li:nth-child(2),
.mi-slider ul.mi-moveFromRight li:nth-child(2),
.mi-slider ul.mi-moveToRight li:nth-child(5),
.mi-slider ul.mi-moveFromLeft li:nth-child(5) {
    -webkit-animation-delay: 90ms;
    animation-delay: 90ms;
}

.mi-slider ul.mi-moveToLeft li:nth-child(3),
.mi-slider ul.mi-moveFromRight li:nth-child(3),
.mi-slider ul.mi-moveToRight li:nth-child(4),
.mi-slider ul.mi-moveFromLeft li:nth-child(4) {
    -webkit-animation-delay: 180ms;
    animation-delay: 180ms;
}

.mi-slider ul.mi-moveToLeft li:nth-child(4),
.mi-slider ul.mi-moveFromRight li:nth-child(4),
.mi-slider ul.mi-moveToRight li:nth-child(3),
.mi-slider ul.mi-moveFromLeft li:nth-child(3) {
    -webkit-animation-delay: 270ms;
    animation-delay: 270ms;
}

.mi-slider ul.mi-moveToLeft li:nth-child(5),
.mi-slider ul.mi-moveFromRight li:nth-child(5),
.mi-slider ul.mi-moveToRight li:nth-child(2),
.mi-slider ul.mi-moveFromLeft li:nth-child(2) {
    -webkit-animation-delay: 360ms;
    animation-delay: 360ms;
}

.mi-slider ul.mi-moveToLeft li:nth-child(6),
.mi-slider ul.mi-moveFromRight li:nth-child(6),
.mi-slider ul.mi-moveToRight li:first-child,
.mi-slider ul.mi-moveFromLeft li:first-child  {
    -webkit-animation-delay: 450ms;
    animation-delay: 450ms;
}

/* Animations */

@-webkit-keyframes moveFromRight {
    0% { -webkit-transform: translateX(600%); }
    100% { -webkit-transform: translateX(0%); }
}

@-webkit-keyframes moveFromLeft {
    0% { -webkit-transform: translateX(-600%); }
    100% { -webkit-transform: translateX(0%); }
}

@-webkit-keyframes moveToRight {
    0% { -webkit-transform: translateX(0%); }
    100% { -webkit-transform: translateX(600%); }
}

@-webkit-keyframes moveToLeft {
    0% { -webkit-transform: translateX(0%); }
    100% { -webkit-transform: translateX(-600%); }
}

@keyframes moveFromRight {
    0% { transform: translateX(600%); }
    100% { transform: translateX(0); }
}

@keyframes moveFromLeft {
    0% { transform: translateX(-600%); }
    100% { transform: translateX(0); }
}

@keyframes moveToRight {
    0% { transform: translateX(0%); }
    100% { transform: translateX(600%); }
}

@keyframes moveToLeft {
    0% { transform: translateX(0%); }
    100% { transform: translateX(-600%); }
}

.mi-slider {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

/* Media Queries */
@media screen and (max-width: 910px){
    .mi-slider nav {
        max-width: 90%;
    }
    .mi-slider nav a {
        font-size: 12px;
        padding: 40px 10px 30px 14px;
    }
}

@media screen and (max-width: 740px){
    .mi-slider {
        height: 300px;
    }
    .mi-slider nav {
        top: 220px;
    }
}

@media screen and (max-width: 490px){ 
    .mi-slider {
        text-align: center;
        height: auto;
    }

    .mi-slider ul {
        position: relative;
        display: inline;
        bottom: auto;
        pointer-events: auto;
    }

    .mi-slider ul li {
        -webkit-animation: none !important;
        animation: none !important;
        -webkit-transform: translateX(0) !important;
        transform: translateX(0) !important;
        padding: 10px 3px;
        min-width: 140px;
    }

    .mi-slider nav {
        display: none;
    }
}

JS

( function( $, window, undefined ) {

    'use strict';

    $.CatSlider = function( options, element ) {
        this.$el = $( element );
        this._init( options );
    };

    $.CatSlider.prototype = {

        _init : function( options ) {

            // the categories (ul)
            this.$categories = this.$el.children( 'ul' );
            // the navigation
            this.$navcategories = this.$el.find( 'nav > a' );
            var animEndEventNames = {
                'WebkitAnimation' : 'webkitAnimationEnd',
                'OAnimation' : 'oAnimationEnd',
                'msAnimation' : 'MSAnimationEnd',
                'animation' : 'animationend'
            };
            // animation end event name
            this.animEndEventName = animEndEventNames[ Modernizr.prefixed( 'animation' ) ];
            // animations and transforms support
            this.support = Modernizr.csstransforms && Modernizr.cssanimations;
            // if currently animating
            this.isAnimating = false;
            // current category
            this.current = 0;
            var $currcat = this.$categories.eq( 0 );
            if( !this.support ) {
                this.$categories.hide();
                $currcat.show();
            }
            else {
                $currcat.addClass( 'mi-current' );
            }
            // current nav category
            this.$navcategories.eq( 0 ).addClass( 'mi-selected' );
            // initialize the events
            this._initEvents();
        },
        _initEvents : function() {

            var self = this;
            this.$navcategories.on( 'click.catslider', function() {
                self.showCategory( $( this ).index() );
                return false;
            } );

            // reset on window resize..
            $( window ).on( 'resize', function() {
                self.$categories.removeClass().eq( 0 ).addClass( 'mi-current' );
                self.$navcategories.eq( self.current ).removeClass( 'mi-selected' ).end().eq( 0 ).addClass( 'mi-selected' );
                self.current = 0;
            } );

        },
        showCategory : function( catidx ) {

            if( catidx === this.current || this.isAnimating ) {
                return false;
            }
            this.isAnimating = true;
            // update selected navigation
            this.$navcategories.eq( this.current ).removeClass( 'mi-selected' ).end().eq( catidx ).addClass( 'mi-selected' );

            var dir = catidx > this.current ? 'right' : 'left',
                toClass = dir === 'right' ? 'mi-moveToLeft' : 'mi-moveToRight',
                fromClass = dir === 'right' ? 'mi-moveFromRight' : 'mi-moveFromLeft',
                // current category
                $currcat = this.$categories.eq( this.current ),
                // new category
                $newcat = this.$categories.eq( catidx ),
                $newcatchild = $newcat.children(),
                lastEnter = dir === 'right' ? $newcatchild.length - 1 : 0,
                self = this;

            if( this.support ) {

                $currcat.removeClass().addClass( toClass );

                setTimeout( function() {

                    $newcat.removeClass().addClass( fromClass );
                    $newcatchild.eq( lastEnter ).on( self.animEndEventName, function() {

                        $( this ).off( self.animEndEventName );
                        $newcat.addClass( 'mi-current' );
                        self.current = catidx;
                        var $this = $( this );
                        // solve chrome bug
                        self.forceRedraw( $this.get(0) );
                        self.isAnimating = false;

                    } );

                }, $newcatchild.length * 90 );

            }
            else {

                $currcat.hide();
                $newcat.show();
                this.current = catidx;
                this.isAnimating = false;

            }

        },
        // based on http://stackoverflow.com/a/8840703/989439
        forceRedraw : function(element) {
            if (!element) { return; }
            var n = document.createTextNode(' '),
                position = element.style.position;
            element.appendChild(n);
            element.style.position = 'relative';
            setTimeout(function(){
                element.style.position = position;
                n.parentNode.removeChild(n);
            }, 25);
        }

    }

    $.fn.catslider = function( options ) {
        var instance = $.data( this, 'catslider' );
        if ( typeof options === 'string' ) {
            var args = Array.prototype.slice.call( arguments, 1 );
            this.each(function() {
                instance[ options ].apply( instance, args );
            });
        }
        else {
            this.each(function() {
                instance ? instance._init() : instance = $.data( this, 'catslider', new $.CatSlider( options, this ) );
            });
        }
        return instance;
    };

} )( jQuery, window );

我尝试了以下

<script src="js/item_slider/jquery.catslider.js"></script>
<script>
$(function() {
$('#mi-slider').catslider(          
setInterval(function() {$('#mi-slider nav > a').trigger('click');}, 2500)
);
}); 
</script>

但它只能在第二个和第一个导航之间切换。

4

1 回答 1

0

自动滑动:不干净,但对我有用。jquery.catslider.js LINE 49(之后:var self = this;)添加:

var changeTime = 5000;
 var actualCat = 0
 setInterval(function() {
 self.showCategory(actualCat);

 if(actualCat < $('#mi-slider ul').length - 1) {
     actualCat++;
 }else {
 actualCat = 0;
 }
 }, changeTime);
于 2014-06-19T10:22:07.567 回答