简体   繁体   English

IE11 上 eventListener 的 JS 问题 - 幻灯片导航不起作用

[英]JS problem with eventListener on IE11 - slide navigation not working

i'm very new on the javascript field and i have a big problem.我是 javascript 领域的新手,我有一个大问题。 I worked on it the last 7 days and i can't find a solution.我过去 7 天一直在研究它,但找不到解决方案。

I hope that anyone could tell me the code to solve this problem.我希望任何人都可以告诉我解决这个问题的代码。

The following snippet show's my navigation.以下代码段显示了我的导航。 It work's fine on chrome, firefox but not on the IE11 - and it must worked on it.它在 chrome、firefox 上运行良好,但在 IE11 上不运行 - 它必须在它上面运行。

It don't opened on IE11.它不会在 IE11 上打开。

I tried the attachEvent function, but i didn't find the right code.我尝试了 attachEvent 函数,但没有找到正确的代码。

I'm very thankful for every help.我非常感谢每一个帮助。 It would be a pleasure if anybody could tell me the little code part.如果有人能告诉我小代码部分,我会很高兴。

Thank you!!谢谢!!

 const toggleButton = document.querySelector('.toggle-menu'); const navBar = document.querySelector('.nav-bar'); toggleButton.addEventListener('click', () => { navBar.classList.toggle('toggle'); });
 .nav-bar { position: fixed; z-index: 1; background-color: red; top: 0; left: -100%; height: 100%; width: auto; display: flex; justify-content: center; align-items: center; transition: 0.5s ease-out; padding: 2%; } .toggle { left: 0; box-shadow: 1px 0 15px 2px rgba(0, 0, 0, 0.4); } .toggle-menu { background-color: white; position: fixed; top: 1rem; left: 1rem; width: 3.5rem; height: 3rem; display: flex; flex-direction: column; justify-content: space-around; padding: 0.2rem 0.5rem; border-radius: 0.5rem; overflow: hidden; background-clip: padding-box; border: 3px solid black; cursor: pointer; } .line { width: 100%; height: 4px; border-radius:5px; background-color: #000; transition: 0.2s ease-out; } .toggle .line1 { background-color: #e30513; transform: scale(0.9) rotateZ(-45deg) translate(-8px, 8px); } .toggle .line2 { display: none; } .toggle .line3 { background-color: #e30513; transform: scale(0.9) rotateZ(45deg) translate(-7px, -8px); } .toggle .toggle-menu { background-color: white; border: 0; } .nav-list { list-style: none; padding: 0; line-height: 0.5; } .nav-list-item { text-align: left; padding: 1rem 0; } .nav-list-item a:hover{ color: white; } .nav-link { color: #fff; font-size: 1.3rem; text-decoration: none; position: relative; padding-bottom: 0.4rem; } .nav-list-item a:hover{ color: white; } .nav-link::before { position: absolute; content: ''; left: 0; bottom: 0; width: 100%; height: 1px; background-color: #fff; transform: scaleX(0); transition: 0.4s ease-in-out; transform-origin: left; } .nav-link:hover::before { transform: scaleX(1); }
 <div class="navigation"> <nav class="nav-bar"> <div class="toggle-menu"> <div class="line line1"></div> <div class="line line2"></div> <div class="line line3"></div> </div> <ul class="nav-list"> <li class="nav-list-item"><a href="page1.html" class="nav-link">Link 1</a></li> <li class="nav-list-item"><a href="page2.html" class="nav-link">Link 2</a></li> </ul> </nav> </div>

Try to replace your arrow function with a regular function as IE11 doesn't support arrow functions.尝试用常规函数替换箭头函数,因为 IE11 不支持箭头函数。

const toggleButton = document.querySelector('.toggle-menu');
const navBar = document.querySelector('.nav-bar');
toggleButton.addEventListener('click', function () {
  navBar.classList.toggle('toggle');
});

I agree with the suggestion provided by Acidic9 regarding the arrow functions.我同意Acidic9提供的关于箭头函数的建议。

Arrow functions not supported in the IE browser and your JS code uses the arrow functions. IE 浏览器不支持箭头函数,您的 JS 代码使用箭头函数。

While running the code in the IE browser it will show the syntax error.在 IE 浏览器中运行代码时,它会显示语法错误。

To fix the issue you need to remove the arrow function from your JS code.要解决此问题,您需要从 JS 代码中删除箭头函数。

Below is your problematic code:以下是您有问题的代码:

toggleButton.addEventListener('click', () => {
  navBar.classList.toggle('toggle');
});

It needs to replace with the code below will fix the issue.它需要替换为下面的代码才能解决问题。

toggleButton.addEventListener('click', function () {
  navBar.classList.toggle('toggle');
});

Here is the full modified code:这是完整的修改代码:

 var toggleButton = document.querySelector('.toggle-menu'); var navBar = document.querySelector('.nav-bar'); toggleButton.addEventListener('click', function () { navBar.classList.toggle('toggle'); });
 .nav-bar { position: fixed; z-index: 1; background-color: red; top: 0; left: -100%; height: 100%; width: auto; display: flex; justify-content: center; align-items: center; transition: 0.5s ease-out; padding: 2%; } .toggle { left: 0; box-shadow: 1px 0 15px 2px rgba(0, 0, 0, 0.4); } .toggle-menu { background-color: white; position: fixed; top: 1rem; left: 1rem; width: 3.5rem; height: 3rem; display: flex; flex-direction: column; justify-content: space-around; padding: 0.2rem 0.5rem; border-radius: 0.5rem; overflow: hidden; background-clip: padding-box; border: 3px solid black; cursor: pointer; } .line { width: 100%; height: 4px; border-radius:5px; background-color: #000; transition: 0.2s ease-out; } .toggle .line1 { background-color: #e30513; transform: scale(0.9) rotateZ(-45deg) translate(-8px, 8px); } .toggle .line2 { display: none; } .toggle .line3 { background-color: #e30513; transform: scale(0.9) rotateZ(45deg) translate(-7px, -8px); } .toggle .toggle-menu { background-color: white; border: 0; } .nav-list { list-style: none; padding: 0; line-height: 0.5; } .nav-list-item { text-align: left; padding: 1rem 0; } .nav-list-item a:hover{ color: white; } .nav-link { color: #fff; font-size: 1.3rem; text-decoration: none; position: relative; padding-bottom: 0.4rem; } .nav-list-item a:hover{ color: white; } .nav-link::before { position: absolute; content: ''; left: 0; bottom: 0; width: 100%; height: 1px; background-color: #fff; transform: scaleX(0); transition: 0.4s ease-in-out; transform-origin: left; } .nav-link:hover::before { transform: scaleX(1); }
 <div class="navigation"> <nav class="nav-bar"> <div class="toggle-menu"> <div class="line line1"></div> <div class="line line2"></div> <div class="line line3"></div> </div> <ul class="nav-list"> <li class="nav-list-item"><a href="page1.html" class="nav-link">Link 1</a></li> <li class="nav-list-item"><a href="page2.html" class="nav-link">Link 2</a></li> </ul> </nav> </div>

Output in the IE 11 browser:在 IE 11 浏览器中的输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM