Home Web Front-end CSS Tutorial Detailed graphic explanation of the difference between mouse events CSS:hover and JS:mouseover

Detailed graphic explanation of the difference between mouse events CSS:hover and JS:mouseover

Sep 15, 2018 pm 02:13 PM

In order to make the page more attractive at work, front-end developers often add mouse movement in and out effects to the page. There are generally two methods for setting the mouse move in and out. One is to simply use the hover pseudo-class in CSS, and the other can use the DOM events in JS, namely onmouseover and onmouseout. Next, this article will talk to you about the difference between CSS pseudo-class hover and JS mouse event mouseover. I hope it can help you.

JS can react to HTML events. Mouse belongs to javascript. It can trigger js commands. When an element is bound to an event, when the event occurs, a piece of javascript code can be executed.

Mouse events in JavaScript include:
onmouseover and onmouseout: events are triggered when the mouse moves in and out
onmousedown and onmouseup: events are triggered when the mouse button is pressed or released
onclick and ondbclick: The event is triggered when the mouse clicks or double-clicks
onmousemover: The event is triggered when the mouse moves

CSS: hover is a pseudo-class selector in css, which refers to The process of moving the mouse in and out, this operation can change the style of the element, and its corresponding subclass is also changed. But the content of the element cannot be changed. For example, when the mouse passes to achieve the effect of a pop-up window, it is implemented using onmousemove. If hover is used, such an effect cannot be achieved.

Maybe the text description is not easy to understand. Next, let’s give an example to see the difference between hover, mouseover, and mouseout. It will be clear at a glance when combined with the picture.

Let’s take a look at the effect achieved by CSS:hover method

HTML part:

<div class="container">
     <div class="aa">内容1</div>
     <div class="bb">内容2</div>
</div>
Copy after login

CSS part:

.container {
           width: 200px;
           border: 1px solid #000000;
           margin: 200px auto;
           line-height: 100px;
           text-align: center;
          }
.aa,.bb {
          height: 100px;
          margin: 1px;
          background: #ccc;
         }
.aa:hover{background: pink;}
Copy after login

Effect Picture:

Detailed graphic explanation of the difference between mouse events CSS:hover and JS:mouseoverDetailed graphic explanation of the difference between mouse events CSS:hover and JS:mouseover

A large box contains two small divs. I want the background color to turn pink when the mouse passes over the upper div. The picture on the left is the effect when the mouse is not moved in, and the picture on the right is the effect when the mouse passes over. As you can see from the picture, CSS:hover can indeed achieve this effect.

Next, let’s take a look at how onmouseover and onmouseout are implemented in JavaScript. The CSS part of the code is the same, except that events are added to HTML and JavaScript is used.

HTML part:

<div class="container">
     <div class="aa" onmouseover="over(this)" onmouseout="out(this)">内容1</div>
     <div class="bb">内容2</div>
</div>
Copy after login

JavaScript part:

function over(obj){        
                obj.innerHTML = "鼠标移入";        
                obj.style.background = "pink";
            }
           function out(obj){
                obj.innerHTML = "移出了";
                obj.style.background = "#ccc";        
            }
Copy after login

Rendering:

Detailed graphic explanation of the difference between mouse events CSS:hover and JS:mouseoverDetailed graphic explanation of the difference between mouse events CSS:hover and JS:mouseoverDetailed graphic explanation of the difference between mouse events CSS:hover and JS:mouseover

## The picture on the left is the style when the mouse is not moved in. The same as above, the picture in the middle is the style when the mouse is passed. The background color of the above div becomes pink, and the content becomes "mouse in". The picture on the right is the effect after the mouse is moved out. The content inside the div has changed. By comparing the two examples, you should know the difference between hover, mouseover, and mouseout.

Summary: CSS can only change the style of the element, but cannot change the content of the element. If you want to change the content, you should use the JavaScript mouse events onmouseover and onmouseout. So just for the style effect, use the CSS pseudo-class hover. If you need to change it dynamically, choose the js event. What is specific at work depends on the situation and choose the appropriate method. Hope this tutorial is useful to you.

The above is the detailed content of Detailed graphic explanation of the difference between mouse events CSS:hover and JS:mouseover. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CSS tip: Use transition to retain hover state CSS tip: Use transition to retain hover state Sep 27, 2022 pm 02:01 PM

How to preserve hover state? The following article will introduce to you how to retain the hover state without using JavaScript. I hope it will be helpful to you!

Experience is flying. Take a flight with Haval X1 camera Experience is flying. Take a flight with Haval X1 camera Jan 15, 2024 pm 02:21 PM

We can often see a lot of wonderful top-down videos on the Internet. The pictures taken by drones are indeed quite shocking. However, in fact, many people have limited understanding of drones. For example, why can they still fly in some places where flying is restricted? In fact, ready-to-fly “drones” are the current mainstream, and they are more worthy of most people’s choice. Today I will give you a hands-on experience with the Harvest Flying Camera X1. In terms of appearance, the Harvest Flying Camera X1 has the first folding design. The whole camera is only 125g, which is lighter than a mobile phone. After folding, it can be easily held in the hand and put into a bag without any pressure. Four soft dyed leaves and safety frame design perfectly protect the safety of shooting. Dyed Leaf innovatively uses Biobased biological substrate, which is highly elastic, durable, safe and environmentally friendly; it also has a fully protective frame to protect your hands during takeoff and landing.

How to remove the hover event in css How to remove the hover event in css Feb 01, 2023 am 10:06 AM

Methods to remove css hover events: 1. Through "$("a").hover(function(){ alert('mouseover'); }, function(){ alert('mouseout'); })" method to bind the hover event; 2. Unbind the hover event through the "$('a').off('mouseenter').unbind('mouseleave');" method. Can.

How to use v-on:mouseover to listen to mouse move-in events in Vue How to use v-on:mouseover to listen to mouse move-in events in Vue Jun 11, 2023 pm 05:09 PM

Vue is a popular JavaScript framework that provides a simple and flexible way to build interactive web applications. The core concept of Vue is "reactive programming", that is, when the data changes, Vue will automatically update the view. In Vue, receiving user input events is very easy, just use the v-on directive. In this article, we will introduce how to use v-on:mouseover to listen for mouse move-in events. what is mouse

The role of hover in html The role of hover in html Feb 20, 2024 am 08:58 AM

The role of hover in HTML and specific code examples In web development, hover refers to triggering some actions or effects when the user hovers the cursor over an element. It is implemented through the CSS :hover pseudo-class. In this article, we will introduce the role of hover and specific code examples. First, hover enables an element to change its style when the user hovers over it. For example, when hovering the mouse over a button, you can change the button's background color or text color to remind the user what to do next.

Why is hover a pseudo element? Why is hover a pseudo element? Oct 09, 2023 pm 05:45 PM

Hover is not a pseudo-element, it is a pseudo-class. Pseudo-classes are used to select a specific state or behavior of an element, while pseudo-elements are used to add styles to specific parts of an element. Because :hover is used to select a specific state of an element rather than adding styles to a specific part of the element, you can use the :hover pseudo-class to style the mouseover state of an element, and you can use the :hover pseudo-class to add hover effects to links. The link's color, background color, etc. can change when the mouse hovers over it.

How to use hover in css How to use hover in css Feb 23, 2024 pm 12:06 PM

The hover pseudo-class in CSS is a very commonly used selector that allows us to change the style of an element when the mouse is hovering over it. This article will introduce the usage of hover and provide specific code examples. 1. Basic Usage To use hover, we need to first define a style for the element, and then use the :hover pseudo-class to specify the corresponding style when the mouse is hovering. For example, we have a button element. When the mouse hovers over the button, we want the background color of the button to change to red and the text color to white.

How to use hover in css How to use hover in css Nov 24, 2023 am 10:32 AM

In CSS, :hover is a pseudo-selector used to select elements that the mouse pointer is hovering over. You can use :hover to apply some style changes when the user hovers over an element.

See all articles