css – 防止在父Div上触发Div的Hover事件?

前端之家收集整理的这篇文章主要介绍了css – 防止在父Div上触发Div的Hover事件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我鼠标悬停.mensal DIV时,它会触发鼠标移动到父.opera DIV,这对我来说似乎不对.我只想要“突出”效果来处理孩子.opera DIV.
  1. #operaContent {
  2. padding: 0 50px 0 50px;
  3. position: relative;
  4. overflow: visible;
  5. }
  6. #operaContent .opera {
  7. display: block;
  8. border: 1px solid #FFFFFF;
  9. border-bottom: 1px solid #DDDDDD;
  10. padding: 5px;
  11. margin-bottom: 10px;
  12. height: 120px;
  13. background-color: #0A8ECC;
  14. }
  15. #operaContent .opera:hover {
  16. border: 1px solid #AAAAAA;
  17. background-color: #DDDDDD;
  18. cursor: pointer;
  19. }
  20. .mensal {
  21. position: absolute;
  22. top: 1px;
  23. left: 8px;
  24. z-index: 3;
  25. display: block;
  26. }
  1. <div id="operaContent">
  2. <div class="opera">
  3. <div class="mensal">
  4. DIV
  5. </div>
  6. </div>
  7. </div>

解决方法

根据定义,将鼠标悬停在孩子身上,也会悬停在父母身上. html事件中没有“阻塞”.

有两个方法链,泡沫和捕获.

Any event taking place in the W3C event model is first captured until
it reaches the target element and then bubbles up again.

http://www.quirksmode.org/js/events_order.html

你要阻止这种情况的唯一方法是通过向你的页面添加javascript来防止冒泡,以防止链.这在jQuery中很简单

  1. $('.mensal').hover(function(e){
  2. e.stopPropagation();
  3.  
  4. });

在我看来,这个答案在处理CSS时完全没有用. Javascript事件不处理CSS选择器或阻止它们.

不幸的是,仅使用CSS,我不知道如何实现这一点(即使在javascript中它也会变得棘手). CSS 4选择器将允许您指定选择器http://dev.w3.org/csswg/selectors4/#subject主题,以便您可以执行类似的操作

  1. #operaContent .opera:hover! .mensal:not(:hover) { /*your css*/ }

但这还没有实施,并且仍在制定草案.

编辑:
这是一个适合你的javascript(jQuery)实现

  1. $(function(){
  2. $('.opera').hover(function() {$(this).addClass('hoverIntent')},function(){ $(this).removeClass('hoverIntent'); }
  3. );
  4.  
  5. $('.opera .mensal').hover(function() {
  6. $(this).parent('.opera').removeClass('hoverIntent');
  7. });
  8.  
  9. })​

修改后的CSS

  1. #operaContent {
  2. padding: 0 50px 0 50px;
  3. position: relative;
  4. overflow: visible;
  5. }
  6.  
  7. #operaContent .opera {
  8. display: block;
  9. border: 1px solid #FFFFFF;
  10. border-bottom: 1px solid #DDDDDD;
  11. padding: 5px;
  12. margin-bottom: 10px;
  13. height: 120px;
  14. background-color: #0A8ECC;
  15. }
  16.  
  17.  
  18. #operaContent .opera.hoverIntent {
  19. border: 1px solid #AAAAAA;
  20. background-color: #DDDDDD;
  21. cursor: pointer;
  22. }
  23.  
  24. .mensal {
  25. position: absolute;
  26. top: 1px;
  27. left: 8px;
  28. z-index: 3;
  29. display: block;
  30. }​

和权利工作演示:http://jsfiddle.net/WB6Ty/

猜你在找的CSS相关文章