0

我制作了一个用作幻灯片的 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)
4

1 回答 1

1

现在,幻灯片使第一张图像淡出,然后使下一张图像淡入。我希望幻灯片在当前图像已经开始淡出时开始淡入下一张图像。

这里的问题是在下一张幻灯片中对淡入淡出的调用是匿名回调函数的一部分,该函数在初始 jQuery 动画完成之前不会触发。您需要重写您的代码,以使$.fadeOut()and$.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通常被读取为用于某种迭代的临时局部变量。

于 2013-11-12T22:59:21.197 回答