【问题标题】:I have a bunch of images being shown on click via JQuery - any easy way to animate this?我有一堆通过 JQuery 点击显示的图像 - 有什么简单的动画方法吗?
【发布时间】:2010-05-07 12:03:50
【问题描述】:

这是我正在使用的(非常简单的)JS 代码:

    $(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;
        });
        $(".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;
        });
    });

图像列表的 HTML 代码如下所示:

<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>

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;
}

我想做的是在图像来来去去时为它们设置动画 - 现在它们刚刚出现,这没关系,但更吸引眼球会更好。

有人可以帮我吗?甚至可以这样做吗?谢谢!!

【问题讨论】:

标签: javascript jquery html jquery-animate


【解决方案1】:

我为你制作了a demo。基本上我使用jQuery的animate()添加了一些左右滚动动画@

您可以在任何方向为图像设置动画,但如果您想要更花哨,那么我会按照 Matt 的建议使用 Cycle 插件。

附加 CSS

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

脚本

$(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;
 });

});

【讨论】:

  • 哦,伙计..这真的有效!非常感谢!你才是男人!!
猜你喜欢
  • 2019-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-29
  • 2012-10-15
  • 1970-01-01
  • 1970-01-01
  • 2015-04-15
相关资源
最近更新 更多