onmouseover

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title></title>
  8. <style>
  9. .box{
  10. width: 200px;
  11. height: 200px;
  12. /* padding: ; */
  13. background-color: orange;
  14. }
  15. /* .box:hover{
  16. background-color: green;
  17. } */
  18. /* 下面的js代码,效果相当于这段css代码
  19. 效果复杂,或者不需要移入移出都触发条件,这时用伪类就不合适了 */
  20. .box1{
  21. width: 100px;
  22. height: 100px;
  23. background-color: black;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div class="box">
  29. <div class="box1"></div>
  30. </div>
  31. <script src="./js/utils.js"></script>
  32. <script>
  33. //鼠标的滑入滑出 mouseover mouseout
  34. //存在冒泡行为,解决冒泡行为
  35. var oBox=document.getElementsByClassName('box')[0],
  36. oBox1=document.getElementsByClassName('box1')[0];
  37. oBox.onmouseover=function() {
  38. this.style.backgroundColor='green';
  39. }
  40. oBox.onmouseout=function() {
  41. this.style.backgroundColor='orange';
  42. }
  43. oBox1.onmouseover=function(e) {
  44. cancelBubble(e)
  45. this.style.backgroundColor='white';
  46. }
  47. oBox1.onmouseout=function(e) {
  48. cancelBubble(e)
  49. this.style.backgroundColor='black';
  50. }
  51. </script>
  52. </body>
  53. </html>

onmouseover的缺陷

  1. <body>
  2. <div class="box">
  3. <div class="box1"></div>
  4. </div>
  5. <script src="./js/utils.js"></script>
  6. <script>
  7. // //鼠标的滑入滑出 mouseover mouseout
  8. // //存在冒泡行为,解决冒泡行为
  9. var oBox=document.getElementsByClassName('box')[0],
  10. oBox1=document.getElementsByClassName('box1')[0],
  11. overCount=0,
  12. outCount=0;
  13. // oBox.onmouseover=function() {
  14. // this.style.backgroundColor='green';
  15. // }
  16. // oBox.onmouseout=function() {
  17. // this.style.backgroundColor='orange';
  18. // }
  19. // oBox1.onmouseover=function(e) {
  20. // cancelBubble(e)
  21. // this.style.backgroundColor='white';
  22. // }
  23. // oBox1.onmouseout=function(e) {
  24. // cancelBubble(e)
  25. // this.style.backgroundColor='black';
  26. // }
  27. //mouseenter mouseleave
  28. //绑定大盒子对小盒子也生效,不是事件冒泡,是对所以子元素都生效,取消冒泡没有用
  29. // oBox.onmouseover = function(e) {
  30. // cancelBubble(e);
  31. // overCount++;
  32. // console.log('overcount:'+overCount);
  33. // }
  34. // oBox.onmouseout = function(e) {
  35. // cancelBubble(e);
  36. // outCount++;
  37. // console.log('outcount:'+outCount);
  38. // }
  39. //ie支持,都支持,只对绑定的元素负责
  40. //元素时被绑定,绑定的是事件处理函数
  41. oBox.onmouseenter = function(e) {
  42. cancelBubble(e);
  43. overCount++;
  44. console.log('overcount:'+overCount);
  45. }
  46. oBox.onmouseleave = function(e) {
  47. cancelBubble(e);
  48. outCount++;
  49. console.log('outcount:'+outCount);
  50. }
  51. </script>
  52. </body>
  53. </html>
  1. /*没有冒泡,只对自己生效,复杂时用enter、leave,但也解决不了所有问题
  2. ,简单时用over、out*/
  3. oBox.onmouseenter = function(e) {
  4. this.style.backgroundColor='green';
  5. }
  6. oBox.onmouseleave = function(e) {
  7. this.style.backgroundColor='orange';
  8. }
  9. oBox1.onmouseenter=function(e) {
  10. cancelBubble(e);
  11. this.style.backgroundColor='white'
  12. }
  13. oBox1.onmouseleave = function(e) {
  14. cancelBubble(e);
  15. this.style.backgroundColor='black'
  16. }

实战

循环绑定

  1. ;(function(doc) {
  2. // document->window
  3. //temporary variable
  4. var oItems=doc.getElementsByClassName('list-item'),
  5. curIdx=0;
  6. console.log(oItems)
  7. var init=function() {
  8. bindEvent();
  9. }
  10. function bindEvent() {
  11. // console.log(1)
  12. for (var i = 0; i <oItems.length;i++){
  13. addEvent(oItems[i],'mouseover',function(e) {
  14. oItems[curIdx].className='list-item';
  15. curIdx=Array.prototype.indexOf.call(oItems,this);
  16. // console.log(curIdx);
  17. oItems[curIdx].className+=' active';//循环绑定
  18. })
  19. }
  20. }
  21. init();
  22. })(document);
  23. // window

事件代理

  1. //方法二:事件代理
  2. ;(function(doc) {
  3. var oList = doc.getElementsByClassName('list')[0],
  4. oItems = oList.getElementsByClassName('list-item'),
  5. curIdx = 0;
  6. var init = function() {
  7. bindEvent();
  8. }
  9. function bindEvent() {
  10. addEvent(oList, 'mouseover', slide);//绑定slide函数
  11. addEvent(oList, 'mouseout', slide);
  12. // addEvent(oList,'mouseover',function(){ //写法二 短触发
  13. // addEvent(document,'mousemove',slide);//mousemove长触发 move性能更好
  14. // });
  15. // addEvent(oList,'mouseout',function(){
  16. // removeEvent(document,'mousemove',slide);
  17. // });
  18. }
  19. function slide(e) {
  20. var e = e || window.event,
  21. tar = e.target || e.scrElement;//找到事件源
  22. oParent = getParent(tar,'li');
  23. thisIdx = Array.prototype.indexOf.call(oItems,oParent);//oItems在li的第几个
  24. if(curIdx!==thisIdx){
  25. // console.log(curIdx,thisIdx);
  26. oItems[curIdx].className = 'list-item';
  27. curIdx = thisIdx;
  28. oItems[curIdx].className+= ' active';
  29. }
  30. }
  31. //无论鼠标停留在哪个函数上都能找到父元素li
  32. //target事件源头,li找哪个元素,元素名字
  33. function getParent(target, element) {
  34. while (target.tagName.toLowerCase() !== element) {
  35. //不等于继续执行,否则return结束函数
  36. target = target.parentNode;
  37. }
  38. return target;
  39. }
  40. init();
  41. })(document);

事件冒泡

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. body{
  10. margin: 0;
  11. }
  12. #box1{
  13. width: 200px;
  14. height: 200px;
  15. background-color: bisque;
  16. }
  17. #box2{
  18. width: 100px;
  19. height: 100px;
  20. background-color: aqua;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="box1">
  26. <div id="box2"></div>
  27. </div>
  28. </body>
  29. <script src="./js/untils.js"></script>
  30. <script>
  31. //换成onmouseover也一样,打印结果一样
  32. window.onclick=function() {
  33. console.log('window');
  34. }
  35. document.onclick=function() {
  36. console.log('document');
  37. }
  38. box1.onclick = function() {
  39. console.log('box1')
  40. }
  41. box2.onclick = function() {
  42. console.log('box2')
  43. }
  44. //冒泡机制
  45. //window->document->box1->box2
  46. //事件流的捕获过程(默认情况下不执行)
  47. //DOM事件流:事件捕获阶段(不执行)->目标阶段(才开始执行)->冒泡阶段
  48. //其实就是:目标阶段(才开始执行)->冒泡阶段,捕获没有
  49. </script>
  50. </html>

image.png

  1. window.addEventListener('mouseover',function(e) {
  2. console.log('window')
  3. },true)
  4. document.addEventListener('mouseover',function(e) {
  5. console.log('document');
  6. },true)
  7. box1.addEventListener('mouseover',function(e) {
  8. console.log('box1')
  9. },true)
  10. box2.addEventListener('mouseover',function(e) {
  11. console.log('box2')
  12. },true)

反过来了
image.png