简体   繁体   English

单击下一项时如何显示工具栏(并隐藏当前)?

[英]How to show toolbar ( and hide current ) when click on the next item?

how to show toolbar (and hide current) when i click on next similar element? 当我单击下一个类似元素时如何显示工具栏(并隐藏当前)? in my code, when i click on next similar element, toolbar doesn't disappear, he disappears only if i firsly click on body and then on element, how to remove toolbar without clicking to body to show next toolbar? 在我的代码中,当我单击下一个相似元素时,工具栏不会消失,只有当我先单击主体然后单击元素时,他才会消失,如何删除工具栏而不单击主体以显示下一个工具栏? thx! 谢谢! http://jsfiddle.net/wwL8fgr1/1/ http://jsfiddle.net/wwL8fgr1/1/

$(".element").on('mouseup', function(e){

        $('[el-button]').click(function(e){
            e.preventDefault();
        });

        var toolbar  = $('<div class="dm-popover"></div>');

        if ( !$('.dm-popover').hasClass('in') ) {
            setTimeout(function(){
                toolbar.addClass('in');
            },100);
            $('body').prepend(toolbar);
        }

        toolbar.addClass('dm-link-frontend-control-top');

        toolbar.css({
            left: $(this).offset().left,
            top:  $(this).offset().top - toolbar.height() - 10  
        });

        setTimeout(function(){
            $('body').on('mouseup', function(e){
                if($(e.target).closest(toolbar).length == 0){
                    $('body').unbind('click', arguments.callee);
                        toolbar.removeClass('in');
                        toolbar.remove();
                }
            });
        }, 100);

        e.stopPropagation();
});

The code responsible for removing the toolbar is here: 负责删除工具栏的代码在这里:

$('body').on('mouseup', function(e){
    if($(e.target).closest(toolbar).length == 0){
        $('body').unbind('click', arguments.callee);
        toolbar.removeClass('in');
        toolbar.remove();
    }
});

Note this registers a mouseup handler on body . 请注意,这会在body上注册一个mouseup处理程序。 That's why you need to click on body to remove the toolbar. 这就是为什么您需要单击body以删除工具栏的原因。 You can attach this handler to the second element, if that's what you expect. 您可以将此处理程序附加到第二个元素。

EDIT 编辑

My guess is that you wanted to achieve something like in this fiddle . 我的猜测是,您想在这种小提琴中获得类似的效果。 Note that is is suitable for 2 elements only, if you need more similar elements , you'd probably need to make a function to generate id's for you instead of storing them in data- attributes. 请注意,它仅适用于2个元素,如果您需要更多类似的元素 ,则可能需要创建一个函数来为您生成ID,而不是将其存储在data- attribute中。

My opinion: 我的意见:

Your code seems overly complex - you're using timeouts, swapping css, mouseup instead of click , creating div elements on each click, preventing handler's propagation... Try to make it simpler by removing unnecessary stuff. 您的代码似乎过于复杂-您正在使用超时,交换css, mouseup而不是click ,每次单击都会创建div元素,从而阻止了处理程序的传播...尝试通过删除不必要的内容使其更简单。

You can try this 你可以试试这个

$(".element").on('mouseup', function(e){

            $('[el-button]').click(function(e){
                e.preventDefault();
            });

            if ( !$('.dm-popover').hasClass('in') ) 
            {
                var toolbar  = $('<div class="dm-popover"></div>');
                setTimeout(function(){
                    toolbar.addClass('in');
                },100);
                $('body').prepend(toolbar);
            }
            else
            {
                var toolbar  = $('.dm-popover');
            }    

            toolbar.addClass('dm-link-frontend-control-top');

            toolbar.css({
                left: $(this).offset().left,
                top:  $(this).offset().top - toolbar.height() - 10  
            });

            setTimeout(function(){
                $('body').on('mouseup', function(e){
                    if($(e.target).closest(toolbar).length == 0){
                        $('body').unbind('click', arguments.callee);
                            toolbar.removeClass('in');
                            toolbar.remove();
                    }
                });
            }, 100);

            e.stopPropagation();
        });

See the JSFiddle http://jsfiddle.net/wwL8fgr1/3/ 参见JSFiddle http://jsfiddle.net/wwL8fgr1/3/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在一次单击时隐藏按钮,它将显示当前时间和日期 - How to hide the button when click at a time it will show the current time and date 仅显示列表中的7个列表项,在“下一个”上单击隐藏当前的7个,显示下一个7个,与“上一个”单击相同 - Show only 7 list items in list, on “next” click hide current 7 show next 7, same with “previous” click 使用jQuery单击时显示第一项并显示/隐藏其他项 - Show first item and show/hide anothers when click using jquery 如何使用click()继续下一个表单元素并隐藏当前表单元素 - How to use click() to proceed to next form elements and hide current one 单击任何行时如何切换()显示/隐藏下一个表行 - How I can toggle() show/hide next table row when click on any of rows jQuery-单击链接并隐藏其他链接时显示当前div - Jquery - Show current div when click a link and hide others 限制菜单中的项目并在单击下一步时显示 - Limit item in menu and show when click next button 如何在jquery中单击按钮时显示隐藏下一个div? - How to show hide the next div on button click in jquery? 如何显示元素并在单击另一个元素时隐藏 - How to show an element and hide when click on another 单击同一按钮后,一次显示下一个图像div并在javascript HTML中隐藏当前图像div - After click on same button show next image div at a time and hide current image div in javascript html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM