【问题标题】:Rebind Hover Effect After Click JQuery单击JQuery后重新绑定悬停效果
【发布时间】:2017-01-29 21:38:06
【问题描述】:

所以我有这个元素#box,它需要有一个悬停效果,当悬停时会自行移动。该元素将在 jQuery 中使用 .hover 正确悬停,然后我需要单击它以显示某些内容,但是单击它后它应该不再具有悬停效果,因此我使用 .unbind 将其删除。现在,当用户重新单击元素时,它将隐藏信息,然后重新应用悬停效果。所以就像一个切换效果。我的问题是最干净的方法是什么。

HTML:

<div class="box" id="box">
  <h1 class="headline">ABOUT ME</h1>
</div>

CSS:

.box {
height: 320px;
width: 320px;
background-color: rgba(0, 0, 0, 0.6);
position: relative;
left: 10px;
top: 10px;
transition: all 1s;
-webkit-transition: all 1s;
-moz-transition: all 1s;
-o-transition: all 1s;
}

.headline {
margin: 0 auto;
color: white;
text-align: center;
display: block;
padding-top: 130px;
font-family: 'Montserrat', sans-serif;
}

.box_hover {
left: 0px;
top: 0px;
height: 300px;
width: 300px;

}

JQuery:

 $(".box").hover(
  function() {
     $(this).toggleClass("box_hover");
   }
 );

 $('#box').click(function() {
   $(this).unbind('mouseenter mouseleave');
  });

这里是JSFiddle

编辑 1:

为了澄清我需要它在悬停时添加类,然后在单击时保持“mouseenter”外观,然后在重新单击时返回能够基于“mouseenter”悬停和移动, "鼠标离开"。

谢谢!

【问题讨论】:

    标签: javascript jquery html css hover


    【解决方案1】:

    除此之外,您可以使用布尔变量来取消绑定事件,单击事件将切换该变量,该变量将控制是否调用 toggleClass

    var bind = true;
    $(".box").hover(
      function() {
        if(bind) $(this).toggleClass("box_hover");
      }
    );
    
    $('#box').click(function() {
        bind = !bind;
    });
    

    Fiddle Example

    【讨论】:

      【解决方案2】:

      当元素具有.hover-effect 类时,您可以将mouseenter/mouseleave 事件委托给该元素。然后,您可以在单击元素时切换该类。

      这样做时,mouseenter/mouseleave 事件只会在元素具有 .hover-effect 类时触发,并且由于该类在单击时切换,因此您实际上是在绑定和取消绑定每个悬停事件点击。

      Updated Example

      $(document).on('mouseenter mouseleave', '.box.hover-effect', function (event) {
        $(this).toggleClass("box-hover", event.type === 'mouseenter');
      });
      
      $('.box').on('click', function() {
        $(this).toggleClass('hover-effect');
      });
      

      【讨论】:

        【解决方案3】:

        如果我正确理解了意图,您只需在点击时切换类:

        $('#box').click(function() {
          $(this).unbind('mouseenter mouseleave');
          $(this).toggleClass("box_hover");
        });
        

        Updated JSFiddle showcasing this.

        希望这会有所帮助!

        编辑

        根据链接的功能,您可以使用 jQuery Mobile 弹出窗口,您根本不需要更改绑定。

        您需要包含外部脚本:

        <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
        <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
        <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
        

        稍微改变你的 HTML:

        <div class="box" id="box">
          <h1 class="headline"><a href="#aboutme" data-rel="popup">ABOUT ME</a></h1>
        </div>
        
        <div data-role="popup" id="aboutme" class="ui-content">
          <h3>Welcome!</h3>
          <p>Popup content</p>
        </div>
        

        连同你的 CSS:

        .headline a {
          text-decoration: none;
          color: inherit !important;
        }
        

        然后,对于 jQuery,您可以简单地使用:

        $('#box').click(function() {
          $(this).toggleClass("box_hover");
        });
        

        我创建了一个展示 here 的新 JSFiddle。

        【讨论】:

        • 这使得移动基于点击一旦被点击。它需要悬停,然后在单击时移除悬停,然后在重新单击时应用悬停。所以它会返回到原来的“预点击”状态。
        • 我已经更新了我的答案,展示了一个解决方案,该解决方案具有初始悬停,单击时失去悬停(并打开一个弹出窗口),然后在弹出窗口关闭后重新获得悬停:)跨度>
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-31
        • 2023-02-21
        • 1970-01-01
        • 2011-06-02
        • 1970-01-01
        • 2010-09-23
        相关资源
        最近更新 更多