【问题标题】:jQuery cycle plugin customizingjQuery循环插件定制
【发布时间】:2009-11-09 02:18:36
【问题描述】:

当鼠标悬停在初始图像上时,我正在使用 jQuery Cycle 插件启动图像幻灯片。这工作正常。之后我想要的是让幻灯片在悬停时停止,并开始手动幻灯片(下一个/上一个按钮)。这目前有效,但幻灯片每次初始化时都会从头开始。我希望它从当前加载的图像开始。

我正在尝试从 DOM 中获取图像的索引(因为它是唯一具有 display:block 的图像)并使用选项“startingSlide”来恢复它,但我的索引一直返回为 -1。

jQuery('#home-menu li').hover(function()
{
   setImages(jQuery(this).attr('title'), jQuery(this).find('.subtitle').text());

   jQuery('#main .blog #projects .gallery .slideshow').cycle(
   {
      fx: 'scrollHorz',
      easing: 'easeOutBounce',
      speed: 2000,
      delay: 0
   });
}, function()
{
   // Before loading the images for the clickable slideshow, find the current image in the DOM array to use as the starting position
   slideshowStart = jQuery('.gallery .slideshow img:visible').index(this);
   console.log('Index: ' + slideshowStart);
   setImages(jQuery(this).attr('title'), jQuery(this).find('.subtitle').text());

   jQuery('#main .blog #projects .gallery .slideshow').cycle('stop').cycle(
   {
      fx: 'scrollHorz',
      easing: 'easeOutBounce',
      startingSlide: slideshowStart,
      speed: 2000,
      timeout: 0,
      next: "#main .blog #projects .gallery span.span2",
      prev: "#main .blog #projects .gallery span.span1"
   });
});  

setImages() 只是根据 li 悬停的内容将图像加载到 DOM 中。没什么特别的。

我希望在悬停和悬停时恢复图像。当我试图让它工作时,我暂时忽略了简历部分以悬停 - 以防你想知道。

【问题讨论】:

标签: jquery plugins cycle customizing


【解决方案1】:

问题在于悬停回调:

slideshowStart = jQuery('.gallery .slideshow img:visible').index(this);

1) 首先,jQuery 语句选择与选择器匹配的所有元素。循环插件确保只显示其容器内的一张图片,因此此语句最多选择一个img 元素。

2) 其次,index 函数搜索在步骤 1 中选择的元素(单个 img 元素),看看它是否可以找到与 subject 匹配的元素(提供给索引函数)。在您的情况下,主题是this,在悬停回调的上下文中是jQuery(#home-menu li)

li 元素永远不会匹配 img 元素,因此对 index() 的调用将始终返回 -1(表示“未找到对象”)。

要解决这个问题,请像这样修改语句:

slideshowStart = $('.slideshow img').index($('img:visible'));

1) 首先,这会选择您的 <div class='slideshow'> 元素中的所有 img 元素。

2) 其次,它返回该列表中第一个可见的img 元素的索引。

【讨论】:

  • 仍然返回-1。我尝试将其更改为var slideshowStart = jQuery('.gallery .slideshow').find('img[style*="display: hidden"]').index(this);。还是-1。根据我在 Google 上找到的很多网站所说,我一直在 index() 中使用“this”。我似乎无法让它返回任何东西,但 -1
  • 我要睡觉了,但你也应该发布相关的 html,这应该可以帮助其他人解决这个问题。
  • HTML: <div id="parent"> <div class="gallery"> <span class="span1">◄</span><span class="span2">►</span> <div class="slideshow"> <img src="initial image" /> </div> </div> <span class="description" style="font-style: italic"></span> </div> 图像通过 jQuery 添加到 .slideshow 中。我设法通过使用slideshowStart = jQuery('*').index(jQuery('.gallery .slideshow .img:visible')); 获得了一个索引,但是当只有 5 张图像时,它的数量就达到了数百个。所以我猜我需要用 .slideshow 替换 '*' 来减少索引?
  • 差不多了。如果将 '*' 替换为 '.slideshow',您将无法找到与 '.gallery .slideshow .img:visible' 匹配的任何内容,因为您已经在幻灯片 div 中。那是索引函数搜索的上下文,幻灯片 div 不包含任何具有画廊或幻灯片类的元素(更不用说 .gallery 元素内的 .slideshow 元素内的可见 img 了,这就是您要指示的内容要查找的索引函数)。我已经用可行的解决方案更新了我的答案(这次实际测试了 D: )。
【解决方案2】:

如果您只是想让它暂停和播放,请使用以下选项:

jQuery('#home-menu li').hover(function() { $("#yourSlideshow").cycle("resume"); }, 功能 { $("#yourSlideshow").cycle("pause"); });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多