我需要将以下鼠标滚轮事件转换为可调用函数
#horiz
是我想要应用粗体代码的任何通用标签。我正在使用 jScrollPane 和 jQuery MouseWheel 库。
$("#horiz").mousewheel(function(event, delta) {
**event.preventDefault();
$(this).find(".jspPane").animate({"left": "+=" + (50 * delta) + "px"}, 0);
$(this).find(".jspPane").css("left").replace(/[^-\d\.]/g, '') > 0 ? $(this).find(".jspPane").animate({"left": "0px"}, 0) : null;
$(this).find(".jspPane").css("left").replace(/[^-\d\.]/g, '') < (($(this).find("#scrollText").css("width").replace(/[^-\d\.]/g, '') - $(this).css("width").replace(/[^-\d\.]/g, '')) * -1) ? $(this).find(".jspPane").animate({"left": (($(this).find("#scrollText").css("width").replace(/[^-\d\.]/g, '') - $(this).css("width").replace(/[^-\d\.]/g, '')) * -1) + "px"}, 0) : null;
if($(this).find(".jspTrack").css("width").replace(/[^-\d\.]/g, '') - $(this).find(".jspDrag").css("width").replace(/[^-\d\.]/g, '') == $(this).find(".jspDrag").css("left").replace(/[^-\d\.]/g, '')) {
//Track End - Right
} else if ($(this).find(".jspDrag").css("left").replace(/[^-\d\.]/g, '') == 0) {
//Track End - Left
} else {
//Track Mid - Anywhere between ends
}**
});
#horiz
would be any generic tag that I want to apply the code in bold to. I'm using jScrollPane and the jQuery MouseWheel library.
$("#horiz").mousewheel(function(event, delta) {
**event.preventDefault();
$(this).find(".jspPane").animate({"left": "+=" + (50 * delta) + "px"}, 0);
$(this).find(".jspPane").css("left").replace(/[^-\d\.]/g, '') > 0 ? $(this).find(".jspPane").animate({"left": "0px"}, 0) : null;
$(this).find(".jspPane").css("left").replace(/[^-\d\.]/g, '') < (($(this).find("#scrollText").css("width").replace(/[^-\d\.]/g, '') - $(this).css("width").replace(/[^-\d\.]/g, '')) * -1) ? $(this).find(".jspPane").animate({"left": (($(this).find("#scrollText").css("width").replace(/[^-\d\.]/g, '') - $(this).css("width").replace(/[^-\d\.]/g, '')) * -1) + "px"}, 0) : null;
if($(this).find(".jspTrack").css("width").replace(/[^-\d\.]/g, '') - $(this).find(".jspDrag").css("width").replace(/[^-\d\.]/g, '') == $(this).find(".jspDrag").css("left").replace(/[^-\d\.]/g, '')) {
//Track End - Right
} else if ($(this).find(".jspDrag").css("left").replace(/[^-\d\.]/g, '') == 0) {
//Track End - Left
} else {
//Track Mid - Anywhere between ends
}**
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每当您需要对特定 DOM 对象执行大量调用时,通常最好将功能包装到 jQuery 插件中。从长远来看,您的代码将更具可维护性。
现在,每当您需要将此事件应用于新对象时,您只需:
我对您拥有的代码进行了一些优化。缓存选择器总是一个好主意,而不是一遍又一遍地使用它们。
特别是重复调用
$self.find('.jsPane');
Whenever you have a ton of calls you need to do on a specific DOM object, it's usually best to wrap the functionality into a jQuery plugin. Your code will be more maintainable in the long run.
Now, whenever you need to apply this event to a new object you just:
I took some optimization liberties with the code you had. It's always a good idea to cache your selectors, rather than use them over and over again.
in particular the repeat calls to
$self.find('.jsPane');
您通过 Id 将 .mousewheel 事件绑定到特定元素来应用此代码(我希望页面上只有一个 id=horiz 的元素)。您可以使用类而不是 ID 对页面上的任何元素执行此操作。
这会将您的函数应用于具有 Id horiz 的元素以及页面上具有类 myMouseWheelBinding 的任何元素
,或者只是省略 id 并使用该类(不要忘记将该类添加到您的 horiz 元素)
Html< /强>:
You're applying this code via binding the .mousewheel event to a specific element via an Id (I hope you only have one element with id=horiz on the page). You can do this to any element on the page by using classes instead of and ID.
This would apply your function to the element with Id horiz and any element on the page with the class myMouseWheelBinding
or just leave off the id and use the class (don't forget to add the class to your horiz element)
Html: