亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我的懸停功能可以工作,但我的 onmouseleave 功能卻不能?(在控制臺調用該函數有效)

為什么我的懸停功能可以工作,但我的 onmouseleave 功能卻不能?(在控制臺調用該函數有效)

浮云間 2024-01-22 15:28:53
這是我的代碼筆:https://codepen.io/squishyboots19996/pen/BaoQjPL我正在嘗試創建一個導航菜單,當您將鼠標懸停在箭頭上時,該菜單會滑入,然后當鼠標離開菜單時會滑開。我有 UIController.showMenu 和 UIController.closeMenu。如果我在控制臺中調用其中任何一個,它們就會按預期工作。但是 closeMenu 的事件偵聽器不起作用。它只是沒有檢測到鼠標離開菜單并且沒有關閉。如果我向函數添加 console.log("Hello") ,它將在頁面首次加載時觸發。但當我需要它時就不起作用了。應用程序.js:var Controller = (function() {    UIController.updateNav;    setInterval(UIController.moveArrowDown, 3000);    //UIController.addBodyMargin(); document.querySelector('#menuArrow').addEventListener("mouseover", UIController.openMenu);      document.querySelector('#sideMenu').addEventListener("onmouseleave", UIController.closeMenu);});//-----------------------------------------------------------var UIController = (function() {    return {        openMenu: (function(){          document.getElementById('sideMenu').style.marginLeft = '0';          document.getElementById('menuArrow').style.marginLeft = '250px';          document.body.style.marginLeft = '25px'        }),        closeMenu: (function(){          document.getElementById('sideMenu').style.marginLeft = '-250px';          document.getElementById('menuArrow').style.marginLeft = '0';          document.body.style.marginLeft = '0';        })        }})();document.addEventListener('DOMContentLoaded', (event) => {  Controller();})導航欄.css:.sidemenu {  height: 100vh;  width: 250px;  position: fixed;  top: 0;  left: 0;  background-color: red;  z-index: 5001;  display: flex;  flex-direction: column;  justify-content: center;  align-items: center;  margin-left: -250px;  transition: .5s;  cursor: pointer;}.sidemenu ul {  list-style: none;  padding: 0;}.sidemenu a {  text-decoration: none;  font-size: 2rem;}.expand-arrow {  z-index: 4999;  position: fixed;  transition: .5s;  bottom: 50%;}
查看完整描述

2 回答

?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

使用該addEventListener()方法時,"mouseleave"不是"onmouseleave"



查看完整回答
反對 回復 2024-01-22
?
慕森王

TA貢獻1777條經驗 獲得超3個贊

添加事件時不應使用“on”,只需使用函數名稱

document.querySelector('#sideMenu').addEventListener("mouseleave", UIController.closeMenu);

代替

document.querySelector('#sideMenu').addEventListener("onmouseleave", UIController.closeMenu);



查看完整回答
反對 回復 2024-01-22
  • 2 回答
  • 0 關注
  • 169 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號