简体   繁体   中英

image slider navigation buttons

I would like to add two buttons onto my navigation slider so that I can go back and forth. What is the most suitable and simplistic way of doing this? Will appreciate any answers provided.

Here is my code

<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type"/>
<title></title>
<link rel="stylesheet" href="stylesheet.css" type="text/css" media="all" />
<script type="text/javascript" src="jquery/jquery-1.2.6.min.js"></script>
<script src="jquery/slider.js"></script>

</head>
<body style="font-family: Arial, Sans-serif, sans;">

<h1>Slideshow</h1>


<div id="slideshow">
    <img src="image1.jpg" alt="Slideshow Image 1" class="active" />
    <img src="image2.jpg" alt="Slideshow Image 2" />
    <img src="image3.jpg" alt="Slideshow Image 3" />
    <img src="image4.jpg" alt="Slideshow Image 4" />
</div>
</body>
</html>

css

#slideshow {
    position:relative;
    height:350px;
}

#slideshow IMG {
    position:absolute;
    top:0;
    left:0;
    z-index:8;
    opacity:0.0;
}

#slideshow IMG.active {
    z-index:10;
    opacity:1.0;
}

#slideshow IMG.last-active {
    z-index:9;
}

Jquery

function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

     $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');



    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 5000 );
});

The forward navigation is already there, you can use it like:

<a href="#" onclick="slideSwitch();return false;">next</a>

The back button is the reverse of the slideSwitch() function:

function slideSwitchBack() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:first');

    var $prev = $active.prev().length ? $active.prev() : $('#slideshow IMG:last');

    $active.addClass('last-active');

    $prev.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

See demo at http://jsfiddle.net/zuY5H/

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