【问题标题】:Infinite scroll and image gallery issue无限滚动和图片库问题
【发布时间】:2011-12-28 16:46:13
【问题描述】:

我已经在我在这里建立的网站上设置了无限滚动:http://richardgordoncook.com/fg/

我正在为图片库使用 Wordpress 插件,我对其进行了一些操作以将鼠标悬停在文件名等上。这一切都很好,直到无限滚动启动并且第一页之后的任何帖子似乎停止使用图库功能和真正的任何 javascript。

有什么想法吗?当您在上面的站点链接上进行测试时,您会得到一个想法。

【问题讨论】:

  • 我在页面中遇到了几个 JS 错误,可能是因为脚本以错误的顺序加载或找不到。
  • 哦,真的吗? Firebug 和 Chrome 开发工具没有显示任何错误。

标签: jquery wordpress infinite-scroll


【解决方案1】:

无限滚动添加的元素在创建后需要将 JavaScript 事件连接到它们。 请参阅 jQuery 的 .live() 函数以获取有关如何轻松执行此操作的更多信息。

本质上,你替换

$('.myclass').mouseover(function(){})

$('.myclass').live('mouseover', function(){});

并且 jQuery 会在新的 dom 元素添加到页面后自动连接所有额外的事件。

更新

好的,忘记.live() 方法。看起来插件有一个回调函数,每当它向页面添加新元素时都会引发该函数。

$(elem).infinitescroll(options,[callback]);

回调函数记录为

function(arrayOfNewElems){
 
     // optional callback when new content is successfully loaded in.
 
     // keyword `this` will refer to the new DOM content that was just added.
     // as of 1.5, `this` matches the element you called the plugin on (e.g. #content)
     //                   all the new elements that were found are passed in as an array
 
}

您应该使用此回调将事件处理程序连接到页面上的新元素。它看起来像这样

function(elements)
{
    $(elements).mouseover(function(){});
    $(elements).mouseout(function(){});
}

根据您需要执行的操作,您可能需要使用 for 循环并单独迭代每个项目,而不是作为一个批次对它们进行操作。

【讨论】:

  • 感谢您的帮助。我可能会这样做以将鼠标悬停在东西上,但也许编辑画廊插件会有点冒险。
  • 此外,悬停在东西上看起来不错,尽管这样:<script> $('a:has(.image-active)').live('hover', function() { $('.image-active', this).stop().animate({"opacity": 1}, 1); },function() { $('.image-active', this).stop().animate({"opacity": 0}, 1); }); </script> 当我悬停时似乎不会将不透明度变为 0。一定是第二个 function() 但想不通。
  • 设法得到它:<script> $('a:has(.image-active)').live('mouseover', function() { $('.image-active', this).stop().animate({"opacity": 1}, 1); }); </script> <script> $('a:has(.image-active)').live('mouseout', function() { $('.image-active', this).stop().animate({"opacity": 0}, 1); }); </script> 仍然停留在画廊的东西上 - 我猜我必须经历这一切?
  • 画廊插件是一个完整的 $(window).load 所以我把里面的所有东西都拿了进去,把它变成了一个函数。 $(window).load(function() { portfolioSlideshow() }); 但是现在我需要在无限滚动中加载下一页时继续调用该函数。有任何想法吗?我不能在 .load 函数上使用 .live 所以关于回调的任何线索?提前致谢。
  • 对不起,我让你挂了。更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 2021-04-30
  • 2023-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多