【问题标题】:How do I get this script to NOT loop?如何让这个脚本不循环?
【发布时间】:2012-04-06 12:14:16
【问题描述】:

我正在使用 Marco Kuiper 的这个脚本,它的效果非常好。我唯一的问题是我不希望这个幻灯片循环播放。谁能帮我弄清楚如何让这个脚本只运行一次?我怀疑它与 setInterval 函数有关,但我只有足够的 Javascript 来玩,而不足以编写(虽然我学得很慢)。提前致谢!

还有脚本链接:

http://www.marcofolio.net/webdesign/advanced_jquery_background_image_slideshow.html

     /*
     * Author:      Marco Kuiper (http://www.marcofolio.net/)
     */

     // Speed of the automatic slideshow
     var slideshowSpeed = 7000;

     // Variable to store the images we need to set as background
     // which also includes some text and url's.
     var photos = [ {
    "title" : "Stairs",
    "image" : "dove.jpg",
    "url" : "http://www.sxc.hu/photo/1271909",
    "firstline" : "Imagine a Day of Peace.",
    "secondline" : "",
    "thirdline" : ""
}, {
    "title" : "Office Appartments",
    "image" : "prayer.jpg",
    "url" : "http://www.sxc.hu/photo/1265695",
    "firstline" : "",
    "secondline" : "Imagine a day where the world unites,<br><br> for one purpose",
    "thirdline" : ""
}, {
    "title" : "Mountainbiking",
    "image" : "trees.jpg",
    "url" : "http://www.sxc.hu/photo/1221065",
    "firstline" : "",
    "secondline" : "",
    "thirdline" : "To take a day, to feel what it would be like..."
}, {
    "title" : "Mountains Landscape",
    "image" : "ocean.jpg",
    "url" : "http://www.sxc.hu/photo/1271915",
    "firstline" : "...to have a world at",
    "secondline" : "",
    "thirdline" : ""
}
     ];



jQuery(document).ready(function() {

 jQuery("#headerimgs").css({"display":"none"}); 
jQuery("#headerimgs").fadeIn(2000); 

// Backwards navigation
jQuery("#back").click(function() {
    stopAnimation();
    navigate("back");
});

// Forward navigation
jQuery("#next").click(function() {
    stopAnimation();
    navigate("next");
});

var interval;
jQuery("#control").toggle(function(){
    stopAnimation();
}, function() {
    // Change the background image to "pause"
    jQuery(this).css({ "background-image" : "url(images/btn_pause.png)" });

    // Show the next image
    navigate("next");

    // Start playing the animation
    interval = setInterval(function() {
        navigate("next");
    }, slideshowSpeed);
});


var activeContainer = 1;    
var currentImg = 0;
var animating = false;
var navigate = function(direction) {
    // Check if no animation is running. If it is, prevent the action
    if(animating) {
        return;
    }

    // Check which current image we need to show
    if(direction == "next") {
        currentImg++;
        if(currentImg == photos.length + 1) {
            currentImg = 1;
        }
    } else {
        currentImg--;
        if(currentImg == 0) {
            currentImg = photos.length;
        }
    }

    // Check which container we need to use
    var currentContainer = activeContainer;
    if(activeContainer == 1) {
        activeContainer = 2;
    } else {
        activeContainer = 1;
    }

    showImage(photos[currentImg - 1], currentContainer, activeContainer);

};

var currentZindex = -1;
var showImage = function(photoObject, currentContainer, activeContainer) {
    animating = true;

    // Make sure the new container is always on the background
    currentZindex--;

    // Set the background image of the new active container
    jQuery("#headerimg" + activeContainer).css({
        "background-image" : "url(images/" + photoObject.image + ")",
        "display" : "block",
        "z-index" : currentZindex
    });

     // Hide the header text
    jQuery("#headertxt").css({"display" : "none"});

    // Set the new header text
    jQuery("#firstline").html(photoObject.firstline);
    jQuery("#secondline")
        .attr("href", photoObject.url)
        .html(photoObject.secondline);
    jQuery("#thirdline")
        .attr("href", photoObject.url)
        .html(photoObject.thirdline);
    jQuery("#pictureduri")
        .attr("href", photoObject.url)
        .html(photoObject.title);



    // Fade out the current container
    // and display the header text when animation is complete
    jQuery("#headerimg" + currentContainer).fadeOut(2000,function() {

        setTimeout(function() {
            jQuery("#headertxt").fadeOut({duration:1000});
            jQuery("#headertxt").fadeIn({duration:1000});
            animating = false;
        }, 300);
    });
};

var stopAnimation = function() {
    // Change the background image to "play"
    jQuery("#control").css({ "background-image" : "url(images/btn_play.png)"          });

    // Clear the interval
    clearInterval(interval);
};

// We should statically set the first image
navigate("next");

// Start playing the animation
interval = setInterval(function() {
    navigate("next");
}, slideshowSpeed);
     });

【问题讨论】:

    标签: jquery loops background slideshow setinterval


    【解决方案1】:

    如果您希望它浏览图像一次然后停止,您可以像这样更改导航功能(添加带有注释的行)。此新代码查找幻灯片索引将要环绕的情况,当它看到时,它会停止间隔并且不显示新幻灯片 - 有效地停止幻灯片放映。

    var navigate = function(direction) {
        // Check if no animation is running. If it is, prevent the action
        if(animating) {
            return;
        }
    
        // Check which current image we need to show
        if(direction == "next") {
            currentImg++;
            if(currentImg == photos.length + 1) {
                currentImg = 1;
                stopAnimation();                     // add this
                return;                              // add this
            }
        } else {
            currentImg--;
            if(currentImg == 0) {
                currentImg = photos.length;
                stopAnimation();                     // add this
                return;                              // add this
            }
        }
    
        // Check which container we need to use
        var currentContainer = activeContainer;
        if(activeContainer == 1) {
            activeContainer = 2;
        } else {
            activeContainer = 1;
        }
    
        showImage(photos[currentImg - 1], currentContainer, activeContainer);
    
    };
    

    【讨论】:

    • 我认为不需要第二个clearInterval,因为这只是在您手动按下时,我认为幻灯片总是向前播放...
    • 哇,真快!谢谢你,这是一种享受,你的精彩解释加深了我的知识......非常感谢!
    • @BillyMoon 你是说第二个setInterval吗?我只看到clearInterval 被使用过一次。
    • @BillyMoon - 导航函数可以传递一个参数来指定运行方式。我修复了导航功能,因此无论如何调用它,它都会在包装时停止循环。 OP 的代码可能仅以一种方式使用它,但我认为修复所有可能的代码路径和/或用于此功能的用途会更好。
    【解决方案2】:

    navigate函数中,添加stopAnimation()

        if(currentImg == photos.length + 1) {
            currentImg = 1;
            stopAnimation() // <~~ add this line here
        }
    

    【讨论】:

      【解决方案3】:

      这应该可以工作

           /*
           * Author:      Marco Kuiper (http://www.marcofolio.net/)
           */
      
           // Speed of the automatic slideshow
           var slideshowSpeed = 7000;
           var count = 1;
      
           // Variable to store the images we need to set as background
           // which also includes some text and url's.
           var photos = [ {
          "title" : "Stairs",
          "image" : "dove.jpg",
          "url" : "http://www.sxc.hu/photo/1271909",
          "firstline" : "Imagine a Day of Peace.",
          "secondline" : "",
          "thirdline" : ""
      }, {
          "title" : "Office Appartments",
          "image" : "prayer.jpg",
          "url" : "http://www.sxc.hu/photo/1265695",
          "firstline" : "",
          "secondline" : "Imagine a day where the world unites,<br><br> for one purpose",
          "thirdline" : ""
      }, {
          "title" : "Mountainbiking",
          "image" : "trees.jpg",
          "url" : "http://www.sxc.hu/photo/1221065",
          "firstline" : "",
          "secondline" : "",
          "thirdline" : "To take a day, to feel what it would be like..."
      }, {
          "title" : "Mountains Landscape",
          "image" : "ocean.jpg",
          "url" : "http://www.sxc.hu/photo/1271915",
          "firstline" : "...to have a world at",
          "secondline" : "",
          "thirdline" : ""
      }
           ];
      
      
      
      jQuery(document).ready(function() {
      
       jQuery("#headerimgs").css({"display":"none"}); 
      jQuery("#headerimgs").fadeIn(2000); 
      
      // Backwards navigation
      jQuery("#back").click(function() {
          stopAnimation();
          navigate("back");
      });
      
      // Forward navigation
      jQuery("#next").click(function() {
          stopAnimation();
          navigate("next");
      });
      
      var interval;
      jQuery("#control").toggle(function(){
          stopAnimation();
      }, function() {
          // Change the background image to "pause"
          jQuery(this).css({ "background-image" : "url(images/btn_pause.png)" });
      
          // Show the next image
          navigate("next");
      
          // Start playing the animation
          interval = setInterval(function() {
              navigate("next");
          }, slideshowSpeed);
      });
      
      
      var activeContainer = 1;    
      var currentImg = 0;
      var animating = false;
      var navigate = function(direction) {
          // Check if no animation is running. If it is, prevent the action
          if(animating) {
              return;
          }
      
          // Check which current image we need to show
          if(direction == "next") {
              currentImg++;
              if(currentImg == photos.length + 1) {
                  currentImg = 1;
              }
          } else {
              currentImg--;
              if(currentImg == 0) {
                  currentImg = photos.length;
              }
          }
      
          // Check which container we need to use
          var currentContainer = activeContainer;
          if(activeContainer == 1) {
              activeContainer = 2;
          } else {
              activeContainer = 1;
          }
      
          showImage(photos[currentImg - 1], currentContainer, activeContainer);
      
      };
      
      var currentZindex = -1;
      var showImage = function(photoObject, currentContainer, activeContainer) {
          animating = true;
      
          // Make sure the new container is always on the background
          currentZindex--;
      
          // Set the background image of the new active container
          jQuery("#headerimg" + activeContainer).css({
              "background-image" : "url(images/" + photoObject.image + ")",
              "display" : "block",
              "z-index" : currentZindex
          });
      
           // Hide the header text
          jQuery("#headertxt").css({"display" : "none"});
      
          // Set the new header text
          jQuery("#firstline").html(photoObject.firstline);
          jQuery("#secondline")
              .attr("href", photoObject.url)
              .html(photoObject.secondline);
          jQuery("#thirdline")
              .attr("href", photoObject.url)
              .html(photoObject.thirdline);
          jQuery("#pictureduri")
              .attr("href", photoObject.url)
              .html(photoObject.title);
      
      
      
          // Fade out the current container
          // and display the header text when animation is complete
          jQuery("#headerimg" + currentContainer).fadeOut(2000,function() {
      
              setTimeout(function() {
                  jQuery("#headertxt").fadeOut({duration:1000});
                  jQuery("#headertxt").fadeIn({duration:1000});
                  animating = false;
              }, 300);
          });
      };
      
      var stopAnimation = function() {
          // Change the background image to "play"
          jQuery("#control").css({ "background-image" : "url(images/btn_play.png)"          });
      
          // Clear the interval
          clearInterval(interval);
      };
      
      // We should statically set the first image
      navigate("next");
      
      // Start playing the animation
      interval = setInterval(function() {
        if(count != photos.length){
          navigate("next");
          count++;
         }
      }, slideshowSpeed);
           });
      

      【讨论】:

        猜你喜欢
        • 2020-09-04
        • 2016-09-27
        • 2015-10-01
        • 2021-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多