Parent element onmouseover triggering events to move between parent and child elements.

Source: Internet
Author: User

Today I wrote a sidebar. Dynamic expansion Shrinkage Effect

<! DOCTYPE html>body{Font-size:20px;font-weight:bold;color:white;line-height:30px;text-Align:center}. Container{height:347px;position:absolute;right:0; border:1px solid black}. Container Li{position:relative;list-style:decimal;height:30px;width:200px; Margin-BOTTOM:3PX;BORDER:1PX Solid Black;display:list-item;right:-165px}. Container li::before{content:"";d isplay:block;position:relative; height:30px;width:35px;background:red} @keyframes bance {0%{right:-165px}100%{right:0}} @keyframes Bance1 {0%{right:0 }            100%{right:-165px}}</style>window.onload=function(){            varLilist=document.queryselectorall (' Li '); varContainer=document.queryselector ('. Container ')); vart1=NULL, t2=NULL; Container.onmouseover=function(){                varN=0; T1=setinterval (function() {lilist[n].style.animationname= ' Bance '; Lilist[n].style.animationduration= ' 0.5s '; Lilist[n].style.animationfillmode= "Forwards"; N++; if(n>9) {clearinterval (T1)}},50)            }; Container.onmouseout=function(){                varI=0; T2=setinterval (function() {lilist[i].style.animationname= ' Bance1 '; Lilist[i].style.animationduration= ' 0.5s '; Lilist[i].style.animationfillmode= "Forwards"; I++; if(i>9) {clearinterval (T2)}},100)            }        }            </script></body>

In the process of continuous error, carefully check the logic did not find any problems, Baidu's discovery may be the parent element onmouseover triggered events in the move between the parent and child elements of the problem, to begin to solve it. After reading http://blog.sina.com.cn/s/blog_7488043d0101dnuz.html This article know the solution;

The following judgment is made at onmouseover and the method body is executed when the result is true:
if (!this.contains (event.fromelement)) {Mouseoverfunc ()}
The following judgment is made at onmouseout and the method body is executed when the result is true:
if (!this.contains (event.toelement)) {Mouseoutfunc ()}
Here's what the two lines of code mean:
In IE, all HTML elements have a contains method that determines whether the element within the current element contains the specified elements. We use this method to determine whether the event of an outer element is triggered by an internal element, and if an internal element causes unwanted events to be triggered, we ignore the event.
event.fromelement Pointer to the element that the mouse left when the onmouseover and onmouseout events are triggered;event.toelement Points to the element that the mouse enters when the onmouseover and onmouseout events are triggered.
The meaning of the above two lines of code is, respectively:
0 when the onmouseover event is triggered, determines whether the element left by the mouse is the inner element of the current element, and if so, ignores this event;
0 when the onmouseout event is triggered, determines whether the element that the mouse enters is the inner element of the current element, and if so, ignores this event;
In this way, the inner elements do not interfere with the onmouseover and onmouseout events of the outer elements.

After adding the judgment as follows

<! DOCTYPE html>body{Font-size:20px;font-weight:bold;color:white;line-height:30px;text-Align:center}. Container{height:347px;position:absolute;right:0; border:1px solid black}. Container Li{position:relative;list-style:decimal;height:30px;width:200px; Margin-BOTTOM:3PX;BORDER:1PX Solid Black;display:list-item;right:-165px}. Container li::before{content:"";d isplay:block;position:relative; height:30px;width:35px;background:red} @keyframes bance {0%{right:-165px}100%{right:0}} @keyframes Bance1 {0%{right:0 }            100%{right:-165px}}</style>window.onload=function(){            varLilist=document.queryselectorall (' Li '); varContainer=document.queryselector ('. Container ')); vart1=NULL, t2=NULL; Container.onmouseover=function(){                if(! This. Contains (event.fromelement)) {                    varN=0; T1=setinterval (function() {lilist[n].style.animationname= ' Bance '; Lilist[n].style.animationduration= ' 0.5s '; Lilist[n].style.animationfillmode= "Forwards"; N++; if(n>9) {clearinterval (T1)}},50)                }            }; Container.onmouseout=function(){                if(! This. Contains (event.toelement)) {                    varI=0; T2=setinterval (function() {lilist[i].style.animationname= ' Bance1 '; Lilist[i].style.animationduration= ' 0.5s '; Lilist[i].style.animationfillmode= "Forwards"; I++; if(i>9) {clearinterval (T2)}},100)                }            }; }    </script></body>

No problem..

Parent element onmouseover triggering events to move between parent and child elements.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.