jQuery幻灯片不会同时淡出

时间:2013-11-12 22:28:47

标签: javascript jquery slideshow

我制作了一个jQuery插件,可以充当幻灯片。我的所有问题都是它先消失,然后下一个图像逐渐消失。这有点奇怪。我整天都在想,而且我真的很喜欢它。谁能帮助我?这是信息:

以下是托管当前幻灯片的网站列表:

http://behdis.org

www.ms-models.com

www.royal-fci.com

现在,问题是:

幻灯片显然会有一堆图像淡入淡出。现在,幻灯片显示第一个图像淡出然后使下一个图像淡入。我希望幻灯片在当前图像已经开始淡出时开始淡入下一个图像。就像许多网站上的数百个幻灯片一样。无需提供链接。

这是插件脚本本身:

(function(){

     /*  function read_line (path_to_file, container, tag, url_prefix)
        {
            myObject = {}; //myObject[numberline] = "textEachLine";
            $.get(, functipath_to_fileon(myContentFile) {
            var lines = myContentFile.split("\r\n");

                    for(var i  in lines)
                    {                         
                          myObject[i] = i;
                          container.append('<'+tag+'>'+"<img src='"+url_prefix+'/'+lines[i]+"'>"+'</img>'+'</'+tag+'>');
                   }      
           }, 'text');// end of anonymous function   
      } */
        $.fn.mtSlideGallery = function(options){
            var options = $.extend({
                fullWidth : false,  // this makes a full background slideshow
                FadeIn : 2,  // this is the duration of fading in
                FadeOut : 2, // this is the duration of fading out
                visibilityDuration : 2, //  this is the duration of visible image
                startSlide : 1, // this is the first slide when the page loads
                height : '100%',
                height_fixed : true,
                enable_error : false,
                enable_gallery : false,
                slide_previous : '.slide-left-control', // this is the name of class for previous-picture-handle
                slide_next : '.slide-right-control', // this is the name of class for next-picture-handle 
                print_output : false,
                print_area : '.slideshow-label',
                seo_campatible : false, // this allows the script to print the title of each slide out into a h1-h6 tag
                seo_suggestion : 'h2' // this allows the user to specify which h1-h6 tag be used. default is h6
                /*auto_generate_from_file : false, // if true, then it will generate the div tag based on a file given
                auto_generate_url : false, // if true, then a url_prefix is added to each line of text
                auto_generate_tag : 'div', // the tag inside which the image tags nest. default is div and it is recommended no to change
                auto_generate_url_prefix : "" // this is the url added to each image address.  Depends on auto_generate_url options*/
            }, options)

        // develope $fn protype        
           return this.each(function(){                         

              if(options.print_output === true)
              {
                var text_for_output = $(this).children('div').eq(options.startSlide).children('img').attr('alt');   
                $(options.print_area).text(text_for_output);              
              }

              if(options.fullWidth === true)
              {
                    $("body").css({'padding' : '0', 'margin' : '0'}); 
                    $("body").css({'overflow' : 'hidden'});  
                    $("html").css({'padding' : '0', 'margin' : '0'});  
                    $(this).css({'width' : '100%', 'height' : options.height});
                    $(this).css({'margin' : '0', 'padding' : '0'});                                         
              }
              if(options.height_fixed === true)
              {
                    $(this).css({'overflow' : 'hidden'});  
              }
              var slideWrapper = $(this);
              var slidesGroup = slideWrapper.children('div');
              slidesLength = slidesGroup.length;
              if(options.enable_error == true)
              {
                      if(options.startSlide > slidesLength)
                        {
                            alert("Please correct the \"slideStart\" option. The number has to be less than the total number of images.")

                }

                }
              i = options.startSlide;
              slidesGroup.not(":eq("+i+")").hide();       
              console.log(slidesLength);
////////////////////// Print To label if true

    var print_label = function(i)
    {
        var label = slidesGroup.eq(i).children('img').attr('alt');  
        $(options.print_area).text(label);
    }
    ;
///////////////////// End of printing label 


//////////////// GALLERY SLIDESHOW IF ENABLED USES THIS SCRIPT  
if(options.enable_gallery === true)
{         
              $(options.slide_previous).click(function(event){ // this is a click event for the previous picture in the queue
                              event.preventDefault();   
                              event.stopPropagation();                        
                              slidesGroup.eq(i).hide();                           
                              if(i === slidesLength)
                                  {
                                      i=0;
                                  }
                              i -= 1;                             
                              slidesGroup.eq(i).show();
                              if(options.print_output === true)
                              {
                                  print_label(i);
                              }
                          });
            $(options.slide_next).click(function(event){ // this is a click event for the next picture in the queue
                event.preventDefault();
                event.stopPropagation();
                slidesGroup.eq(i).hide();                             
                if(i === slidesLength)
                    {
                        i=0;
                    }
                i += 1;                           
                slidesGroup.eq(i).show();
                if(options.print_output === true)
                              {
                                  print_label(i);
                              }
            });
}
////////////////////// END OF GALLERY-ENABLED SCRIPT    


////////////////////////////////////////////////////////        
              var slideShow = function () {                                               
                          slidesGroup.eq(i).fadeOut(options.FadeOut*1000, function(){
                             slidesGroup.eq(i).hide();
                            i += 1;
                              if(i === slidesLength)
                                  {
                                      i=0;
                                  }
                                  slidesGroup.eq(i).fadeIn(options.FadeIn*1000);                                 
                                  if(options.print_output === true)
                              {
                                  print_label(i);
                              }
                          })                    
            }
              setInterval(slideShow, options.visibilityDuration*1000);

           })                
            }
})(jQuery)

1 个答案:

答案 0 :(得分:1)

  

现在,幻灯片显示第一张图片淡出然后制作了   下一张图片渐渐消失了。我希望幻灯片在下一张图片中逐渐消失   当前图像已经开始淡出时的图像。

这里的问题是,在下一张幻灯片中淡入淡出的调用是匿名回调函数的一部分,该函数在初始jQuery动画完成之前不会触发。您需要重写代码,以便$.fadeOut()$.fadeIn()函数同时发生:

var transition = function() {
    //Determine the index of the next slide to show, based on the current index
    var next_index = (current_index < slidesLength - 1) ? current_index + 1 : 0;
    //Fade out the 'old' slide while fading in the 'new' slide
    slidesGroup.eq(current_index).fadeOut({
        duration: options.FadeOut*1000,
        start: function() {
            slidesGroup.eq(next_index).fadeIn({
                duration: options.FadeIn*1000
            });
        },
        complete: function() {
            //Update the value of current_index
            current_index = next_index;
        }
    });
}

但是,此功能几乎将您限制为仅向前转换。您可以包含一个函数参数,该参数指定将确定“下一张幻灯片”索引的转换方向(向前和向后)。

工作jsFiddle:http://jsfiddle.net/gh25s/

编辑:为了澄清,您需要替换这部分代码:

var slideShow = function () {                                               
    slidesGroup.eq(i).fadeOut(options.FadeOut*1000, function(){
        slidesGroup.eq(i).hide();
        i += 1;
        if(i === slidesLength) {
              i=0;
        }
        slidesGroup.eq(i).fadeIn(options.FadeIn*1000);                                 
        if(options.print_output === true) {
          print_label(i);
        }
    });                    
};

用这个:

var slideShow = function() {
    //Determine the index of the next slide to show, based on the current index
    var next_index = (i < slidesLength - 1) ? i + 1 : 0;
    //Fade out the 'old' slide while fading in the 'new' slide
    slidesGroup.eq(i).fadeOut({
        duration: options.FadeOut*1000,
        start: function() {
            slidesGroup.eq(next_index).fadeIn({
                duration: options.FadeIn*1000
            });
        },
        complete: function() {
            //Update the value of i
            i = next_index;
        }
    });
}

此外,在整个代码中使用变量名i会让人感到困惑。我建议给它一个更有意义,更具描述性的名称,如current_index。像i这样的变量名通常被读作临时的局部变量,用于某种迭代。