[英]jQuery click event stop working in plugin
I've been trying to add event listeners as a part of a plugin. 我一直在尝试将事件侦听器添加为插件的一部分。 The idea is really simple, the goal is to do a slider plugin that handles 2 dimensions (horizontally and vertically)
这个想法非常简单,目标是制作一个可处理2个维度(水平和垂直)的滑块插件
So I have something like this 所以我有这样的事情
<div class="tab-container bg-dark-blue" start-tab="Home">
<div class="tab-page" x-index="1" y-index="1" id="Home">
<h1>x=1, y=1</h1>
</div>
<div class="tab-page" x-index="1" y-index="2">
<h1>x=1, y=2</h1>
</div>
<div class="tab-page" x-index="2" y-index="1">
<h1>x=2, y=1</h1>
</div>
<div class="tab-page" x-index="2" y-index="2">
<h1>x=2, y=2</h1>
</div>
</div>
Where tab-page divs are absolutely positioned with 100% width and height, and tab-container is relatively positioned with 100% width and height of the browser. 标签页div绝对以100%的宽度和高度定位,而标签容器相对定位为以100%的浏览器宽度和高度定位。
var __MyMethods = {
width : 0
height : 0,
container : null,
// other stuff
setup : function(tab_container) {
// setup __MyPlugin.container = tab_container
// setup width, height of the container
},
/*
* Movement functions
*
*/
move : function(direction)
{
if( direction == 'left' )
{
__MyMethods.x--;
__MyMethods.translate( 'left', __MyMethods.width );
//
}
if( direction == 'right' )
{
__MyMethods.x++;
__MyMethods.translate( 'left', (-1)*__MyMethods.width );
//
}
if( direction == 'up' )
{
__MyMethods.y--;
__MyMethods.translate( 'top', __MyMethods.height );
}
if( direction == 'down' )
{
//
__MyMethods.y++;
__MyMethods.translate( 'top', (-1)*__MyMethods.height );
//
}
},
translate : function(property, offset)
{
// For each .tab-page in we animate the CSS property
$(__MyMethods.container).children(".tab-page").each( function() {
var animation = {};
var x = parseInt( $(this).css(property).replace('px','') ) + offset;
animation[property] = x.toString() + 'px';
$(this).animate(
animation,
{
'duration' : 0,
'queue' : false,
}
);
});
},
};
$.fn.MyPlugin = function()
{
__MyMethods.setup(this);
$(".btn").click( function(event)
{
__MyMethods.move( $(this).attr("move") );
console.log(this);
});
return this;
};
So I put 所以我把
<button class="btn" move="up"></button>
<button class="btn" move="down"></button>
<button class="btn" move="left"></button>
<button class="btn" move="right"></button>
Somewhere in the HTML and it works fine for up, left, right... but if you click the move="down" button it works only the first time. 在HTML的某个位置,向上,向左,向右都可以正常使用...但是,如果单击move =“ down”按钮,则只能在第一次使用。
I don't know why is that hapening, the event never fires again if you click that button, I tried doing console.log(this) inside the click event function, but it doesn't show anything after the down button is clicked... 我不知道为什么会这样,如果您单击该按钮,该事件将永远不会再次触发,我尝试在click事件函数中执行console.log(this),但是在单击向下按钮后它什么也没有显示。 ..
Any idea of what can be happening? 有什么可能发生的想法吗?
Thank you very much for any help you can give me 非常感谢您能给我的任何帮助
Edit : Here is a demo 编辑 :这是一个演示
Regards 问候
In you jsfiddle the down button seems to stay behind the tab-page s. 在您jsfiddle中,向下按钮似乎停留在tab-page的后面。 Giving z-index to menu and tab-page elements solves the problem.
将z-index赋予菜单和选项卡页面元素可以解决此问题。
In your css file: Add 在您的CSS文件中:添加
z-index: 10;
to .menu 到.menu
Add 加
z-index: 1;
to .tab-page 到.tab-page
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.