JS 事件退出循环

发布于 2024-10-14 16:43:26 字数 1476 浏览 5 评论 0原文

所以我对我的 JS 有点生疏,但这是我的代码...... 基本上我有一个图像,当鼠标悬停时,它会循环显示一个充满其他图像的隐藏 div...淡出它,替换 src,然后淡入。它效果很好。但是一旦它遍历完所有图像,我希望它重新开始并继续循环遍历它们,直到 mouseout 事件停止它。

我以为我可以从函数 cycle_images($(current_image)); 中再次调用该函数,但这会导致浏览器崩溃,这是可以理解的。什么是完成此任务的好方法?

$.fn.image_cycler = function(options){
  // default configuration properties
  var defaults = {
    fade_delay:     150,
    image_duration: 1500,
    repeatCycle:    true,
  };
  var options = $.extend(defaults, options);

  this.each(function(){
    var product     = $(this);
    var image       = $('div.image>a.product_link>img', product);
    var description = $('div.image>div.description', product);
    var all_images  = $('div.all_images', product);
    var next_image  = ($(all_images).find('img[src="' + $(image).attr('src') + '"]').next('img').attr('src')) ? $(all_images).find('img[src="' + $(image).attr('src') + '"]').next('img') : $(all_images).children('img').first();;

    // mouseover
    image.fadeOut(options.fade_delay, function(){
      image.attr('src', next_image.attr('src'));
      image.fadeIn(options.fade_delay);
    });
    if (options.repeatCycle){
      var loop = function() {
        product.image_cycler();
      }
      setTimeout(loop,options.image_duration);
    }
  });
};


$(document).ready(function() {
  $('div.product').hover(function(){
    $(this).image_cycler();
  }, function(){
    $(this).image_cycler({repeatCycle: false});
  });
});

so im a little rusty with my JS, but here is my code...
basically i have an image, that on mouseover, it cycles through a hidden div full of other images... fading it out, replacing the src, and fading back in. it works great. but once it gets through all the images, i want it to start back over and keep looping through them until the mouseout event stops it.

i thought i could just call the function again from within the function cycle_images($(current_image));, but that leads to the browser freaking out, understandably. what is a good method to accomplish this?

$.fn.image_cycler = function(options){
  // default configuration properties
  var defaults = {
    fade_delay:     150,
    image_duration: 1500,
    repeatCycle:    true,
  };
  var options = $.extend(defaults, options);

  this.each(function(){
    var product     = $(this);
    var image       = $('div.image>a.product_link>img', product);
    var description = $('div.image>div.description', product);
    var all_images  = $('div.all_images', product);
    var next_image  = ($(all_images).find('img[src="' + $(image).attr('src') + '"]').next('img').attr('src')) ? $(all_images).find('img[src="' + $(image).attr('src') + '"]').next('img') : $(all_images).children('img').first();;

    // mouseover
    image.fadeOut(options.fade_delay, function(){
      image.attr('src', next_image.attr('src'));
      image.fadeIn(options.fade_delay);
    });
    if (options.repeatCycle){
      var loop = function() {
        product.image_cycler();
      }
      setTimeout(loop,options.image_duration);
    }
  });
};


$(document).ready(function() {
  $('div.product').hover(function(){
    $(this).image_cycler();
  }, function(){
    $(this).image_cycler({repeatCycle: false});
  });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春如此纠结 2024-10-21 16:43:26

听起来您希望它在鼠标移开时重新循环并停止。

定义cycle_images()函数后,添加一个标志,repeatCycle,

cycle_images.repeatCycle = true;

在cycle_images函数的末尾,添加一个带超时的递归调用,

if (this.repeatCycle) {
   var f = function() {
      return cycle_images.apply(this, [$current_image]);
   }
   setTimeout(f,50);
}

然后在mouseout中,

cycle_images.repeatCycle = false;

It sounds like you want it to re-cycle and stop on mouseout.

After you define the cycle_images() function, add a flag, repeatCycle

cycle_images.repeatCycle = true;

At the end of the cycle_images function, add a recursive call with a timeout

if (this.repeatCycle) {
   var f = function() {
      return cycle_images.apply(this, [$current_image]);
   }
   setTimeout(f,50);
}

Then in mouseout,

cycle_images.repeatCycle = false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文