简体   繁体   English

当我用鼠标离开元素但用鼠标进入弹出框时,如何隐藏弹出框?

[英]How do I make the Popover hide when I mouseleave the element but mouseenter the popover?

This is the Html for the popover, which is used to display a summary of a user profile when someone hover's over the profile thumbnail. 这是弹出窗口的HTML,用于当有人将鼠标悬停在个人资料缩略图上方时显示用户个人资料的摘要。

                      <div class="user-avatar" style="background-image: url({{          $chat->from->small_avatar }}); " data-container="body" data-toggle="popover" data-placement="right" data-html="true" data-content="<div class='group-chat-popover'>
                      <div class='popover-header'>
                        <div class='chat-avatar' style='background-image:url({{ $chat->from->small_avatar }})'></div>
                        <div class='header-description'>
                          <p class='user-name'>{{ $chat->from->full_name }}</p>
                          <p class='user-bio'>{{ $chat->from->about }}</p>
                        </div>
                      </div>
                      <div class='user-activity'>
                        <div class='activity'>
                          <p class='activity-category'>Reputation</p>
                            <p class='activity-count'>{{ $chat->from->total_points }}</p></div>
                        <div class='activity'>
                          <p class='activity-category'>Submissions</p>
                            <p class='activity-count'>{{ $chat->from->approved_tutorials->count() }}</p></div>
                        <div class='activity'>
                          <p class='activity-category'>Upvotes</p>
                            <p class='activity-count'>{{ $chat->from->votes->count() }}</p></div>
                      </div>
                      <div class='popover-footer'>
                        <a href='{{ $chat->from->profile_link }}' class='btn btn-sm btn-select'>Open profile</a>
                        <a href='{{ $chat->from->chat_link }}' class='btn btn-sm btn-primary'>Private Chat</a>
                        </div>
                    </div>">
                    </div>

Here is the code I have written to trigger and close the popover. 这是我编写的触发和关闭弹出窗口的代码。 Also I am using the bootstrap popovers here. 我也在这里使用引导程序弹出窗口。

            var timer;
            $(".user-avatar").popover({
                trigger: "manual",
                animation: false
            })
                .on("mouseenter", function(){
                var self = $(this);
                timer = setTimeout(function(){
                    self.popover("show");
                }, 1000);
            })
                .on("mouseleave", function () {
                clearTimeout(timer);

                $(".popover").on("mouseleave", function () {
                    $(this).popover('hide');
            });
                setTimeout(function () {
                    if (!$(".popover:hover").length) {
                        $(this).popover("hide");
                    }
                }, 30);
            });

The issue is I am not able to hide the popover when I mouseenter the thumbnail but directly mouseleave the thumbnail (without mouseleaving the popover). 问题是,当我用鼠标输入缩略图时,我无法隐藏弹出窗口,而直接用鼠标留下缩略图(没有鼠标留下弹出窗口)。

I want the following behaviour: 我想要以下行为:

Popover show when I mouseenter the thumbnail. 当我用鼠标输入缩略图时会显示弹出窗口。 Popover stays open when I mouseenter the popover. 当我用鼠标点击弹出窗口时,弹出窗口保持打开状态。 Popover hides when I mouseleave the popover. 当我用鼠标离开弹出窗口时,弹出窗口会隐藏。 Popover hides when I mouseleave the thumbnail (without going to the popover). 当我用鼠标离开缩略图时,弹出窗口会隐藏(不显示弹出窗口)。

I am not able to achieve the last point! 我无法达到最后一点!

You should probably set a timer when the mouse leaves the element and clear it when the mouse enters the popover. 您可能应该在鼠标离开元素时设置一个计时器,并在鼠标进入弹出框时清除它。 Something like this: 像这样:

        var timer;
        $(".user-avatar").popover({
            trigger: "manual",
            animation: false
        }).on("mouseenter", function(){
            $(this).popover("show");
        }).on("mouseleave", function () {
            var self = $(this);
            timer = setTimeout(function(){ // You may want to keep a reference to the time of each element.
                self.popover("hide");
            }, 1000);
        });

        $(".popover").on("mouseenter", function(){
            clearTimer(timer);
        }).on("mouseleave", function () {
            $(this).popover("hide"); // I'm not sure this will work, you may have to keep a reference to the element that owns this popover.
        });

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

相关问题 当我单击同一div或在div之外时,如何隐藏和切换popover元素? - How do i hide and toggle popover element when i click on the same div or outside of the div? 如何在弹出窗口中获取单选元素? - How do I get a radio element in a popover? 我到底该如何使用mouseenter和mouseleave? - How exactly do I use mouseenter and mouseleave? 如何使 Bootstrap 弹出窗口与单独元素中的 HTML 内容一起使用 - How do I make Bootstrap popover work with HTML content in a seperate element 单击弹出触发器元素时,如何关闭/关闭Bootstrap Popover? - How can I close / dismiss Bootstrap Popover when clicking the popover trigger element? 如何正确使用jQuery off()方法删除元素上的mouseenter和mouseleave事件 - How do I properly use the jQuery off() method to remove the mouseenter and mouseleave events off an element 当我将鼠标悬停在完整日历中的弹出框上时,如何保持弹出框可见? - how to keep popover visible when i hover on the popover in full calendar? 如何在mouseenter和mouseleave上使用jQuery淡化2张图像? - How do I fade 2 images with jQuery on mouseenter and mouseleave? 在外部单击时如何关闭自定义弹出窗口? - How do I close custom popover when click outside? 当 TAB 按键通过 on keyup 发生时,如何中断 mouseenter/on mouseleave 事件? - How do I interrupt on mouseenter/on mouseleave events when TAB keypresses occur via on keyup?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM