[英]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.