javascript - jQuery 点击事件在插件中停止工作

标签 javascript jquery html jquery-plugins

我一直在尝试添加事件监听器作为插件的一部分。这个想法非常简单,目标是做一个处理 2 维(水平和垂直)的 slider 插件

所以我有这样的东西

<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>

其中 tab-page div 以 100% 宽度和高度绝对定位,而 tab-container 以浏览器的 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;
};

所以我把

<button class="btn" move="up"></button>
<button class="btn" move="down"></button> 
<button class="btn" move="left"></button>
<button class="btn" move="right"></button> 

在 HTML 中的某个位置,它可以很好地用于向上、向左、向右...但是如果您单击 move="down"按钮,它仅在第一次时有效。

我不知道为什么会发生这种情况,如果您单击该按钮,该事件永远不会再次触发,我尝试在单击事件函数内执行 console.log(this) ,但在向下按钮之后它不显示任何内容被点击...

知道会发生什么吗?

非常感谢您给我的帮助

编辑:这是一个demo

问候

最佳答案

在你的jsfiddle中,向下按钮似乎停留在标签页后面。为菜单和选项卡页元素提供 z-index 可以解决该问题。

在你的 CSS 文件中: 添加

z-index: 10;

到.menu

添加

z-index: 1;

到.tab页

关于javascript - jQuery 点击事件在插件中停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33050425/

相关文章:

javascript - 如何在 jQuery 或 JavaScript 中将 $(document).html() 保存到 .xml 文件?

html - aside 不会漂浮在文章旁边

html - CSS 正确 :5% not of parent element but % of the window's width

javascript - 使用 JQuery/Javascript 获取组合上传文件的总大小

javascript - 如果在服务器(nodeJS)上运行,此 JavaScript 代码的安全性如何?

javascript - 什么是覆盖 promise 解决的值(value)的好方法?

javascript - 如何提取iframe的重定向url?

jquery - 添加按钮/html内容到kendo对话框标题栏

javascript - 如何在没有可选查询字符串参数的情况下从 URL 获取路径或 slug

javascript - jQ 格式不是函数