How to bind a custom event to elements loaded on the fly from another script?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 04:03:16

问题


I have a infinite scroll script which works for my tumblr. It's loading images as I scroll down the page. However I'm looking to have the images fade in rather than suddenly appear.

I've found this awesome plugin called waypoints which does this sort of thing. And it works, but only on the first loaded images. After that, it's not recognising the newly loaded images.

I know that "live" only works on certain jQuery functions. And bind might be better as you can use custom functions. But then i still apparently have to trigger the script, and this is what i dont know how to do. This would mean i'd have to hack the tumblr script which does the infinite scroll, and it's scary in there, javascript rather than jQuery.

Maybe you could recommend a better way of doing this?

Here is the code that works for the first ten images:

$('.theImage').waypoint(function() {
   $(this).animate({opacity: 1}, 'slow');
});

I wish i could just do something like this:

$('.theImage').bind("waypoint", function() { $(this).animate({opacity: 1}, 'slow'); });

Thank you.

PS the content is miiiiildly nsfw. In fact its sfw since one mildly sexy picture is not even viewable and it's all the way at the bottom. Cheers.


回答1:


It's not that live doesn't work with custom events, but more that Waypoints only fires it's event on elements that have had .waypoint() called on it. So you would need to make this call after the new content has loaded regardless.

Since hacking the infinitescroll script is out of the question, I suggest you try using the jquery.inview plugin instead of Waypoints. The way inview is structured it creates an event that is triggered by every element so you can use .live, .delegate.



来源:https://stackoverflow.com/questions/7910520/how-to-bind-a-custom-event-to-elements-loaded-on-the-fly-from-another-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!