How to implement DIV's blur function

How to implement DIV's blur function

Use anti-shake to make DIV disappear when the mouse moves out

Since the div tag itself does not support the onblur event, for a div that pops up when a button is clicked, we want to make it disappear when the div loses focus, which cannot be achieved using onblur.

However, you can use onmouseout and events to make DIV disappear when it loses focus. There may be a problem with directly using onmouseout to achieve disappearance: if the position of your button and the position of the pop-up div do not overlap, the onmouseout event will be triggered immediately when the mouse moves, which is useless.

Use the combination of anti-shake, onmouseout, and onmouseover to achieve a good blur event experience

    /**
     *Mouse moves over div event*/
    function moveOverEvent(ele,outTimer) {
        let overTimer = null;
        return function(){
            clearTimeout(outTimer); //If the div does not disappear, if another div moves in, clear the last event that was moved out clearTimeout(overTimer); //Anti-shake overTimer = setTimeout(()=>{        
                ele.style.display = "block";
            },500);                     
        }
    }
    /**
     *Mouse out*/
    function moveOutEvent(ele,outTimer) {
        return function(){
            clearTimeout(outTimer); //Anti-shake outTimer = setTimeout(()=>{ //Wait 500ms after moving out, and then disappear this div
                ele.style.display = "none";
            },500);
        }
    }

Then I accidentally discovered that the blur event can be implemented by adding the tabindex attribute to the div, so the above code may have been written in vain. (PS I feel the above experience is better, reducing a lot of false touches)

//After setting tabindex, the element is dotted by default, which can be removed by setting ouline=0 (IE sets hidefocus="true")
<div tabindex="0" outline=0" hidefocus="true"></div>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Solution for Baidu site search not supporting https (tested)

>>:  MySQL index failure principle

Recommend

How to check PCIe version and speed in Linux

PCIE has four different specifications. Let’s tak...

JavaScript implements an input box component

This article example shares the specific code for...

Steps to modify the MySQL database data file path under Linux

After installing the MySQL database using the rpm...

Three ways to achieve background blur in CSS3 (summary)

1. Normal background blur Code: <Style> htm...

Example code for implementing simple ListViews effect in html

HTML to achieve simple ListViews effect Result: c...

Vue implements horizontal beveled bar chart

This article shares the specific code of Vue to i...

How to configure Nginx load balancing

Table of contents Nginx load balancing configurat...

Basic usage tutorial of MySQL slow query log

Slow query log related parameters MySQL slow quer...

The table tbody in HTML can slide up and down and left and right

When the table header is fixed, it needs to be di...

Html+CSS drawing triangle icon

Let’s take a look at the renderings first: XML/HT...

Implementation of MySQL GRANT user authorization

Authorization is to grant certain permissions to ...

Detailed explanation of the difference between $router and $route in Vue

We usually use routing in vue projects, and vue-r...