开发者

I have a bunch of images being shown on click via JQuery - any easy way to animate this?

开发者 https://www.devze.com 2022-12-29 18:36 出处:网络
This is the (quite simple) JS code I\'m using: $(document).ready(function() { $(\".button-list .next\").click(function() {

This is the (quite simple) JS code I'm using:

    $(document).ready(function() {
        $(".button-list .next").click(function() {
            project = $(this).parents().filter(".projektweb").eq(0);
            currentimg = project.find(".images-list li.current");
            nextimg = currentimg.next();
            firstimg = project.find(".images-list li:first");
            currentimg.removeClass("current");
            if (nextimg.is("li")) nextimg.addClass("current");
            else firstimg.addClass("current");
            return false;
        });
 开发者_如何学JAVA       $(".button-list .prev").click(function() {
            project = $(this).parents().filter(".projektweb").eq(0);
            currentimg = project.find(".images-list li.current");
            previmg = currentimg.prev();
            lastimg = project.find(".images-list li:last");
            currentimg.removeClass("current");
            if (previmg.is("li")) previmg.addClass("current");
            else lastimg.addClass("current");
            return false;
        });
    });

And this is how the HTML code for the image list looks like:

<ul class="images-list">
    <li class="current"><img src="img/1.jpg" alt="" /></li>
    <li><img src="img/1b.jpg" alt="" /></li>
</ul>

<ul class="button-list"> <li><a class="button prev" href="#">←</a></li> 
<li><a class="button next" href="#">→</a></li></ul>

The CSS:

.images-list {
    height: 460px;
    list-style-type:none;
    float:left;
    width: 460px;
    overflow:hidden;
    position:relative;
}

.images-list img {
    height: 460px;
    width: 460px;
    display:block;
}

 .images-list li {
    display:none;
}

.images-list li.current {
    display:block;
}

What I'd like to do is animate the images as they come and go - right now they just appear, which is OK but a bit more eye-candy would be nice.

Can anyone help me out here? It it even possible to do it this way? Thanks!!


I made a demo for you. Basically I added some left and right scrolling animation using jQuery's animate()

You can animate the image in any direction, but if you want to get more fancy, then I would use the Cycle plugin as Matt suggested.

Additional CSS

.images-list li.animating {
 position: absolute;
 top: 0;
 left: 0;
 display: block;
}

Script

$(document).ready(function() {

 // *** Constants ***
 var project = $('.projektweb');
     animationTime = 500,  // scroll time in milliseconds
     animationWidth = 480; // image width = 460 + 20px padding between images
     leftPadding = parseInt( project.find('ul').css('padding-left'), 10); // padding due to UL

 // *** Next image ***
 $('.button-list .next').click(function() {
  currentimg = project.find('li.current');
  nextimg = (currentimg.next().is('li')) ? currentimg.next() : project.find('.images-list li:first');

  currentimg
   .removeClass('current')
   .addClass('animating')
   .css('left', leftPadding)
   .animate({
    left: '-=' + animationWidth 
   }, animationTime, function(){
    $(this).removeClass('animating');
   });

  nextimg
   .addClass('animating')
   .css('left', (animationWidth + leftPadding) + 'px')
   .animate({
    left: 0 + leftPadding
   }, animationTime, function(){
    $(this).removeClass('animating').addClass('current');
   })

  return false;
 });

 // *** Prev image ***
 $('.button-list .prev').click(function() {
  currentimg = project.find('.images-list li.current');
  previmg = (currentimg.prev().is("li")) ? currentimg.prev() : project.find('.images-list li:last');

  currentimg
   .addClass('animating')
   .css('left', leftPadding)
   .removeClass("current")
   .animate({
    left: '+=' + animationWidth
   }, animationTime, function(){
    $(this).removeClass('animating');
   });

  previmg
   .addClass('animating')
   .css('left',  '-' + (animationWidth) + 'px')
   .animate({
    left: '+=' + (animationWidth + leftPadding)
   }, animationTime, function(){
    $(this).removeClass('animating').addClass('current');
   })

  return false;
 });

});
0

精彩评论

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