jquery:在第二个插件发生更改后触发一个插件的公共方法
嘿那里..再次需要一些建议:)
我正在开发一个具有可过滤组合的项目[基于此插件链接:www.gethifi.com/blog/a-jquery-plugin-to-create-an-interactive-filterable-portfolio-like -我们的。
投资组合项目显示在水平滑块中,该滑块在浏览器窗口的左侧和右侧添加滚动区域(热点)。
我的问题来了:
滑块的宽度是在插件 smoothdivscroller www.smoothdivscroll.com 中计算的。但是当我通过过滤器导航更改滑块的内容时,滑块的总宽度发生变化,但 smoothdivscroller 插件没有注意到它。
我在 jsfiddle 中为您设置了一个简化的示例,您可以在这里体验整个问题kuemmel-schnur.de/projekte:当显示所有项目(Alle)并且您向右滚动,然后切换到类别“Lehrprojekte”您将看不到任何项目,因为它们位于最左侧,并且不会重新计算容器的总宽度。
为了解决这个问题,我有三个想法,我需要一些认真的帮助。
1)smoothdivscroll插件提供了一个公共方法来重新计算容器的宽度,就像
$("#makeMeScrollable").smoothDivScroll("recalculateScrollableArea");
我每次单击portfolio-list a
后都需要触发的那样。我需要将其与滑块自动切换到当前内容的第一个元素的方法结合起来
$("#makeMeScrollable").smoothDivScroll("moveToElement", "first");
2)我的第二个想法 - 因为我不知道1)是否或如何工作 - 是检查网址是否更改,然后触发重新计算。过滤器使用哈希来寻址内容。所以我想我可以读出 url,每次哈希更改后的部分我都可以触发该方法。
3)我可以使用类似的东西将可过滤插件绑定到 smoothDivScroll 插件(注意完全错误的代码:)
$('#portfolio-list').filterable();
$('#portfolio-filter a').click(function(){
$('#makeMeScrollable').smoothDivScroll("moveToElement", "first")("recalculateScrollableArea");
});
所以..你觉得怎么样?再次提供 jsfiddle 链接:jsfiddle.net/tobiasmay/QudtF/
谢谢, 托比。
附:我本来可以正确设置链接,但我需要多 1 个声誉点才能发布多于 1 个链接;)
hey there.. need some advice again :)
I'm working on a project with a filterable portfolio [based on this plugin link: www.gethifi.com/blog/a-jquery-plugin-to-create-an-interactive-filterable-portfolio-like-ours.
the portfolio items are shown in a horizontal slider which is adding scroll areas (hot spots) on the left and right side of the browser window.
here comes my problem:
the width of the slider is calculated in the plug-in smoothdivscroller www.smoothdivscroll.com. but when i change the content of the slider via the filter navigation the total width of the slider changes, but the smoothdivscroller plug-in is not noticing it.
i set up a simplified example in jsfiddle for you and you can experience the whole problem herekuemmel-schnur.de/projekte: when all projects are shown (Alle) and you scroll to the right and then switch to the category "Lehrprojekte" you won't see any projects because they are on the far left side and the total width of the container is not recalculated.
in order to fix this i have three ideas where i need some serious help.
1) the smoothdivscroll plug in offers a public method to recalculate the width of the container like
$("#makeMeScrollable").smoothDivScroll("recalculateScrollableArea");
which i need to fire every time after portfolio-list a
is clicked. and i need to combine this with the method where the slider automatically switches to the first element of the current content
$("#makeMeScrollable").smoothDivScroll("moveToElement", "first");
2) My second idea - cause i don't know if or how 1) works - is to check if the url changes and then fire the recalculation. the filter uses a hash to address the content. so i thought i could read out the url and every time the part directly after the hash changes i could fire the method.
3) I could bind the filterable plug-in to the smoothDivScroll plug-in with something like (beware of completely wrong code :)
$('#portfolio-list').filterable();
$('#portfolio-filter a').click(function(){
$('#makeMeScrollable').smoothDivScroll("moveToElement", "first")("recalculateScrollableArea");
});
so.. what do you think? again the jsfiddle link: jsfiddle.net/tobiasmay/QudtF/
thanks,
tobi.
ps. i would have setup the links properly, but i need 1 more reputation point to post more then 1 link ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以收听该事件
filterportfolio
,并从那里重新计算/moveToElement。在 实时网站 的 JavaScript 控制台中使用它时,它似乎可以工作。将代码块添加到
aks.js
或在 Chrome 中尝试:转到该网站,打开控制台,粘贴代码并按 Enter 键。重现问题中的步骤时应该立即起作用:滚动到右侧,单击过滤器。编辑:由于过滤中的动画,需要在动画完成后调用
.smoothDivScroll(...)
。由于如果只调用一次动画可能看起来有点难看,因此将在duration
毫秒内进行多次重新计算,中间有一个interval
毫秒。You can listen to the event
filterportfolio
, and recalculate/moveToElement from there.It seemed to work when playing with it in the javascript console on the live site. Add the code block to
aks.js
or try it out in Chrome: go to the site, open the console, paste the code and press enter. Should work right away when reproducing the steps from the question: scroll far to the right, click on a filter.Edit: due to the animations in the filtering,
.smoothDivScroll(...)
needs to be called after the animations are done. Since the animations might look a bit ugly if only called once, the recalculations will be done several times duringduration
milliseconds with aninterval
milliseconds in between.您可以使用 jQuery 自定义事件:当它更改滑块的内容时,它会执行
$(document).trigger('contentChange');
并在插件中为此事件设置一个处理程序
$(document).bind('contentChange', function(){//这里是改变滑块宽度的代码});
You may use jQuery custom event: when it changes the content of slider, it executes
$(document).trigger('contentChange');
and in the plugin it sets an handler for this event
$(document).bind('contentChange', function(){//here the code that changes the width of slider});
这是我对这个问题的看法。
您可以尝试第二个选项:捕获哈希更改事件
这种方法的唯一缺点是:IE7 本身不支持 hashChange 事件。
Here is my view for this problem.
You can try your second option : capture hash change event
Only minus point with this approach is : IE7 do not support hashChange event natively.