jQuery - stopping a hover event while dragging

笑着哭i 提交于 2019-12-07 15:16:01

问题


I'm creating a drag and drop image environment, where if you hover over an image, a small menu pops up overtop of it. if you click and drag the image, you can reorder them.

The problem I'm having is, I want the hover event to be disabled when you're dragging around. Currently if you drag an image around, it triggers all the hover menus on the other images you hover over.

$('ul.imglist li').mousedown(
  function() { 
    $(this).find('ul')
      .fadeOut(100); 
  });

// Image actions menu
$('ul.imglist li').hover(
  function() { 
    $(this).find('ul')
      .css('display', 'none')
      .fadeIn('fast')
      .css('display', 'block');
  },
  function() { 
    $(this).find('ul')
      .fadeOut(100); 
  });   

I have a bit of jQuery here, the bottom hover event is the one I want to disable while dragging, the top mousedown is what gets rid of the menu when you click on the image. I need something that will disable the hover while moving the mouse around, with the key still pressed down (dragging).

I tried a few things with no luck, does anyone have a suggestion?


回答1:


there are hundreds of ways.

    // Image actions menu
$('ul.imglist li').hover(
  function() { 
    $(this).find('ul')
      .css('display', 'none')
      .fadeIn('fast')
      .css('display', 'block');
  },
  function() { 
    $(this).find('ul')
      .fadeOut(100); 
  }); 

should be on mouseup event... then on mousedown even you can cancel the hover event. by the way look up binding and unbinding

but why not use the draggable ui ? and save yourself some time end effort




回答2:


.hover-off {
  pointer-events: none;
}

...to the rescue: Paul Lewis + thecssninja.com w/ a pretty nice, 1-stop solution.

http://www.thecssninja.com/css/pointer-events-60fps



来源:https://stackoverflow.com/questions/3338063/jquery-stopping-a-hover-event-while-dragging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!