JavaScript功能适用于Chrome但不适用于IE

时间:2017-06-19 19:39:33

标签: javascript html css google-chrome internet-explorer

我在下面的代码中提供了一些我创建的信息框以及其他编码器的一些帮助,使用HTML CSS和JavaScript。我的信息框应该在悬停时显示另一个div并保持焦点,直到您单击关闭悬停div的按钮。一切都在Google Chrome上完美运行,但JavaScript悬停功能似乎并不适用于IE。有没有人对如何使代码在IE中工作有任何想法。什么都有帮助,欢呼!



function open(e){
  let li = e.target;
  li.classList.add('hover');
}

function close(e){
  const hoverable = e.target.closest('li.hover');
  hoverable.classList.remove('hover');
}

const infoBoxListItems = document.querySelectorAll('.info-boxes li');
const closeButtons = document.querySelectorAll('.profileclose');

infoBoxListItems.forEach(li => {
  li.addEventListener('mouseenter', open);
});

closeButtons.forEach(btn => {
  btn.addEventListener('click', close);
});

.everything {
  text-align:center;
height:1000px;
}

.everything.hover {
cursor: default;
}

#wrapper {

    margin: 0 auto;
}
#wrapper img{
    width:100%;       /* the image will now scale down as its parent gets smaller */
}

.infobox-list {
    display: inline-block;
    text-align: center;
}

ul, ol, li {
    margin: 0;
    padding: 0;
    list-style-position: outside;
    list-style-type: none;
}

h1, h2, h3, h4, h5, h6, ul, li, ol, form, fieldset {
    margin: 0;
    padding: 0;
}

*, *:before, *:after {
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

ul, menu, dir {
    display: block;
    list-style-type: disc;
    -webkit-margin-before: 1em;
    -webkit-margin-after: 1em;
    -webkit-margin-start: 0px;
    -webkit-margin-end: 0px;
    -webkit-padding-start: 0px;
}

.hr {
  border-color:rgba(255,255,255,0.3);
  margin-left:9px;
  width: 210px;
}

body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 16px;
    padding: 0;
    margin: 0;
    background-color: #fcfcfc;
    color: #555;
    min-width: 20em;
}

.info-boxes li {
  width: 310px;
    height: 535px;
    background: #f2f2f2;
    border: 0px solid #efefef;
    border-radius: 4px;
    margin: 0 0px 0px;
    cursor:pointer;
    position: relative;
    overflow: hidden;
    transform: scale(0.75); 
}

.ptext
{
  text-align:center;
  font-size:20px;

}

.profile
{
  margin-top:10%;
  margin-left:1px;
  width:75px;
  height:75px;
}

.performance
{
  margin-left:1px;
  margin-top:10%;
  width:75px;
  height:75px;
}

.learning
{
  margin-top:10%;
  width:75px;
  height:75px;
}

.team
{
  margin-top:10%;
  width:75px;
  height:75px;
}

.smallicons
{
  width:50px;
  height:50px;
}

.profileclose {
margin-top:215px;
}

.infobox-list li {
  display: inline-block;
}

a {
  color: white;
  text-decoration: none;
  font-weight:lighter;
}

.info-boxes li .infobox {
  display: table-cell;
  text-align:center;
  vertical-align: middle;
  height: 535px;
  width: 310px;
}

.info-boxes li .infobox:before { 
  content: '';
  position: absolute;
  left: 20%;
  width: 160%; 
  height: 188%; 
  background-color: rgb(255, 255, 255); /* fallback */
  background-color: rgba(255, 255, 255, 0.2);
  top: 0;
  -webkit-transform: rotate(46deg);
  -moz-transform: rotate(46deg);
  transform: rotate(30deg);
} 

.info-boxes li.hover .shade {
  animation-name: windowshade;
  cursor:pointer;
}

.info-boxes li .shade,
.info-boxes li.hover .shade {
  animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
  animation-duration: 1s;
  animation-iteration-count: 1;
  animation-fill-mode: forwards;
}

section p {
  line-height: 1.3em;
  color: #6d6e71;
  width: 100%;
  padding: 0 10px;
  margin-top: 5px;
  margin-left: 0px;
  text-align:left;
}

p {
  display: block;
}

.info-boxes li .shade {
  position: absolute;
  width: 310px;
  height: 555px;
  left: 0;
  top: 0;
  background-color: #6caf46;
  color: #fff;
  display: table;
  vertical-align: middle;
  padding: 20px 10px 0;
  transform: translateY(-340px);
  animation-name: windowshade-out;
}

.info-boxes li .shade h3 {
  color: #fff;
  padding: 10px;
  font-weight: bold
}

.info-boxes li .shade p {
  color: #fff;
  line-height: 3em;
  font-weight: lighter;
}

.ie9 .info-boxes li.hover .shade {
  top: 245px
}

.info-boxes li.hover a {
  text-decoration: none
}

@keyframes windowshade {
  0% {
    transform: translateY(-535px)
  }
  100% {
    transform: translateY(0)
  }
}

@keyframes windowshade-out {
  0% {
    transform: translateY(0)
  }
  100% {
    transform: translateY(-535px)
  }
}

  <section class="info-boxes">
    <ul class="infobox-list">
      <li>
        <a href="#">
          <div class="infobox">
            <table>
              <tr>
                <img class="profile" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497637065/profile_i0evlz.png"></tr>
              <tr>
                <p class="ptext">Profile</p>
              </tr>
            </table>
          </div>
          <div class="shade">
            <table>
              <tr>
                <td>
                  <img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497301295/About-Me_dudglr.png">
                </td>
                <td>
                  <p>About Me</p>
                  <hr class="hr">
                </td>
              </tr>
              <tr>
                <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/Resume_tb7t02.png"> </td>
                  <td>
                    <p>Resume</p>
                    <hr class="hr">
                  </td>
              </tr>
              <tr>
                <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497280574/Accountabilities_qfdcns.png"> </td>
                  <td>
                    <p>Accountabilities
                      <p/>
                      <hr class="hr">
                  </td>
              </tr>
            </table>
            <div class="profileclose">Close</div>
          </div>
        </a>
      </li>
      <li>
        <div class="infobox">
          <table>
            <tr>
              <img class="learning" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497637065/Learning-Icon_hqtc2k.png"></tr>
            <tr>
              <p class="ptext">Learning</p>
            </tr>
          </table>
        </div>
        <div class="shade">
          <table>
            <tr>
              <td>
                <img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497297687/Training-Plan_v43ne7.png">
              </td>
              <td>
                <p>Training Plan</p>
                <hr class="hr">
              </td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497297687/Training-History_czttv1.png"> </td>
                <td>
                  <p>Training History</p>
                  <hr class="hr">
                </td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497298863/coursefeedback_qdh1wm.png"> </td>
                <td>
                  <p>Course Feedback</p>
                  <hr class="hr">
                </td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497299106/Favourites_y9gkce.png"> </td>
                <td>
                  <p>Training Favourites
                    <p/>
                    <hr class="hr">
                </td>
            </tr>
          </table>
        </div>
      </li>
      
      <li>
        <a href="#">
          <div class="infobox">
            <table>
              <tr>
                <img class="performance" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497637065/Performance-Icon_ozaldt.png"></tr>
              <tr>
                <p class="ptext">Performance</p>
              </tr>
            </table>
          </div>
          <div class="shade">
            <table>
              <tr>
                <td>
                  <img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/Goals_aw4nso.png">
                </td>
                <td>
                  <p>Goals</p>
                  <hr class="hr">
                </td>
              </tr>
              <tr>
                <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284934/EA_n8lvj1.png" </td>
                  <td>
                    <p>Effectiveness Assessment</p>
                    <hr class="hr">
                  </td>
              </tr>
              <tr>
                <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/development_yfv6o1.png" </td>
                  <td>
                    <p>Development Plan</p>
                    <hr class="hr">
                  </td>
              </tr>
              <tr>
                <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497285433/resources_b3r88g.png"> </td>
                  <td>
                    <p>Resources
                      <p/>
                      <hr class="hr">
                  </td>
              </tr>
            </table>
          </div>
        </a>
      </li>
      
      <li>
        <div class="infobox">
          <table>
            <tr>
              <img class="team" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497637065/team_nl2cwh.png"></tr>
            <tr>
              <p class="ptext">Team</p>
            </tr>
          </table>
        </div>
        <div class="shade">
          <table>
            <tr>
              <td>
                <img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497300652/Team_iovnl5.png">
              </td>
              <td>
                <p>Team</p>
                <hr class="hr">
              </td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/Goals_aw4nso.png"> </td>
                <td>
                  <p>Team Goals</p>
                  <hr class="hr">
                </td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/development_yfv6o1.png"> </td>
                <td>
                  <p>Team Development</p>
                  <hr class="hr">
                </td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497297687/Training-Plan_v43ne7.png"> </td>
                <td>
                  <p>Team Training</p>
                  <hr class="hr">
                </td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497301295/Approval_f9y1da.png"> </td>
                <td>
                  <p>Approvals
                    <p/>
                    <hr class="hr">
                </td>
            </tr>
          </table>
        </div>

      </li>
    </ul>
  </section>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

您需要做的就是使用babel编译您的javascript。它将输出以下代码,该代码适用于当前浏览器:

'use strict';

function open(e) {
  var li = e.target;
  li.classList.add('hover');
}

function close(e) {
  var hoverable = e.target.closest('li.hover');
  hoverable.classList.remove('hover');
}

var infoBoxListItems = document.querySelectorAll('.info-boxes li');
var closeButtons = document.querySelectorAll('.profileclose');

infoBoxListItems.forEach(function (li) {
  li.addEventListener('mouseenter', open);
});

closeButtons.forEach(function (btn) {
  btn.addEventListener('click', close);
});

更进一步,添加支持IE9及以下版本,替换

li.classList.add('hover');
li.className += " hover"

hoverable.classList.remove('hover')
hoverable.className = hoverable.className.replace('hover', '');

答案 1 :(得分:1)

看起来你的代码对于IE11来说有点先进,并且在浏览器实现方面存在一些差异。

对于IE11中不支持的箭头功能和一些其他ES6功能,您可以使用transpiler将JS从ES6转换为ES5。虽然这不会解决所有问题。转换器不会转换target.closest()。您需要找到searching ancestor elements的不同实现。

IE11不允许forEach()使用 NodeList Object ,这是document.querySelectorAll()返回的内容。您可以使用[].slice.call( document.querySelectorAll() )或类似方法将 NodeList 转换为数组。一旦集合是一个数组forEach()将在IE11中按预期工作。

&#13;
&#13;
// https://stackoverflow.com/questions/22119673/find-the-closest-ancestor-element-that-has-a-specific-class#22119674
function findAncestor(el, cls) {
  while ((el = el.parentElement) && !el.classList.contains(cls));
  return el;
}

function open(e) {
  let li = e.target;
  li.classList.add('hover');
}

function close(e) {
  const hoverable = findAncestor(e.target, 'hover');
  hoverable.classList.remove('hover');
}

const infoBoxListItems = [].slice.call(document.querySelectorAll('.info-boxes li'));
const closeButtons = [].slice.call(document.querySelectorAll('.profileclose'));

infoBoxListItems.forEach(function(li) {
  li.addEventListener('mouseenter', open);
});

closeButtons.forEach(function(btn) {
  btn.addEventListener('click', close);
});
&#13;
.everything {
  text-align: center;
  height: 1000px;
}

.everything.hover {
  cursor: default;
}

#wrapper {
  margin: 0 auto;
}

#wrapper img {
  width: 100%;
  /* the image will now scale down as its parent gets smaller */
}

.infobox-list {
  display: inline-block;
  text-align: center;
}

ul,
ol,
li {
  margin: 0;
  padding: 0;
  list-style-position: outside;
  list-style-type: none;
}

h1,
h2,
h3,
h4,
h5,
h6,
ul,
li,
ol,
form,
fieldset {
  margin: 0;
  padding: 0;
}

*,
*:before,
*:after {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

ul,
menu,
dir {
  display: block;
  list-style-type: disc;
  -webkit-margin-before: 1em;
  -webkit-margin-after: 1em;
  -webkit-margin-start: 0px;
  -webkit-margin-end: 0px;
  -webkit-padding-start: 0px;
}

.hr {
  border-color: rgba(255, 255, 255, 0.3);
  margin-left: 9px;
  width: 210px;
}

body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 16px;
  padding: 0;
  margin: 0;
  background-color: #fcfcfc;
  color: #555;
  min-width: 20em;
}

.info-boxes li {
  width: 310px;
  height: 535px;
  background: #f2f2f2;
  border: 0px solid #efefef;
  border-radius: 4px;
  margin: 0 0px 0px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transform: scale(0.75);
}

.ptext {
  text-align: center;
  font-size: 20px;
}

.profile {
  margin-top: 10%;
  margin-left: 1px;
  width: 75px;
  height: 75px;
}

.performance {
  margin-left: 1px;
  margin-top: 10%;
  width: 75px;
  height: 75px;
}

.learning {
  margin-top: 10%;
  width: 75px;
  height: 75px;
}

.team {
  margin-top: 10%;
  width: 75px;
  height: 75px;
}

.smallicons {
  width: 50px;
  height: 50px;
}

.profileclose {
  margin-top: 215px;
}

.infobox-list li {
  display: inline-block;
}

a {
  color: white;
  text-decoration: none;
  font-weight: lighter;
}

.info-boxes li .infobox {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
  height: 535px;
  width: 310px;
}

.info-boxes li .infobox:before {
  content: '';
  position: absolute;
  left: 20%;
  width: 160%;
  height: 188%;
  background-color: rgb(255, 255, 255);
  /* fallback */
  background-color: rgba(255, 255, 255, 0.2);
  top: 0;
  -webkit-transform: rotate(46deg);
  -moz-transform: rotate(46deg);
  transform: rotate(30deg);
}

.info-boxes li.hover .shade {
  animation-name: windowshade;
  cursor: pointer;
}

.info-boxes li .shade,
.info-boxes li.hover .shade {
  animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
  animation-duration: 1s;
  animation-iteration-count: 1;
  animation-fill-mode: forwards;
}

section p {
  line-height: 1.3em;
  color: #6d6e71;
  width: 100%;
  padding: 0 10px;
  margin-top: 5px;
  margin-left: 0px;
  text-align: left;
}

p {
  display: block;
}

.info-boxes li .shade {
  position: absolute;
  width: 310px;
  height: 555px;
  left: 0;
  top: 0;
  background-color: #6caf46;
  color: #fff;
  display: table;
  vertical-align: middle;
  padding: 20px 10px 0;
  transform: translateY(-340px);
  animation-name: windowshade-out;
}

.info-boxes li .shade h3 {
  color: #fff;
  padding: 10px;
  font-weight: bold
}

.info-boxes li .shade p {
  color: #fff;
  line-height: 3em;
  font-weight: lighter;
}

.ie9 .info-boxes li.hover .shade {
  top: 245px
}

.info-boxes li.hover a {
  text-decoration: none
}

@keyframes windowshade {
  0% {
    transform: translateY(-535px)
  }
  100% {
    transform: translateY(0)
  }
}

@keyframes windowshade-out {
  0% {
    transform: translateY(0)
  }
  100% {
    transform: translateY(-535px)
  }
}
&#13;
<section class="info-boxes">
  <ul class="infobox-list">
    <li>
      <a href="#">
        <div class="infobox">
          <table>
            <tr>
              <img class="profile" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497637065/profile_i0evlz.png"></tr>
            <tr>
              <p class="ptext">Profile</p>
            </tr>
          </table>
        </div>
        <div class="shade">
          <table>
            <tr>
              <td>
                <img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497301295/About-Me_dudglr.png">
              </td>
              <td>
                <p>About Me</p>
                <hr class="hr">
              </td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/Resume_tb7t02.png"> </td>
              <td>
                <p>Resume</p>
                <hr class="hr">
              </td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497280574/Accountabilities_qfdcns.png"> </td>
              <td>
                <p>Accountabilities
                  <p/>
                  <hr class="hr">
              </td>
            </tr>
          </table>
          <div class="profileclose">Close</div>
        </div>
      </a>
    </li>
    <li>
      <div class="infobox">
        <table>
          <tr>
            <img class="learning" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497637065/Learning-Icon_hqtc2k.png"></tr>
          <tr>
            <p class="ptext">Learning</p>
          </tr>
        </table>
      </div>
      <div class="shade">
        <table>
          <tr>
            <td>
              <img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497297687/Training-Plan_v43ne7.png">
            </td>
            <td>
              <p>Training Plan</p>
              <hr class="hr">
            </td>
          </tr>
          <tr>
            <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497297687/Training-History_czttv1.png"> </td>
            <td>
              <p>Training History</p>
              <hr class="hr">
            </td>
          </tr>
          <tr>
            <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497298863/coursefeedback_qdh1wm.png"> </td>
            <td>
              <p>Course Feedback</p>
              <hr class="hr">
            </td>
          </tr>
          <tr>
            <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497299106/Favourites_y9gkce.png"> </td>
            <td>
              <p>Training Favourites
                <p/>
                <hr class="hr">
            </td>
          </tr>
        </table>
      </div>
    </li>

    <li>
      <a href="#">
        <div class="infobox">
          <table>
            <tr>
              <img class="performance" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497637065/Performance-Icon_ozaldt.png"></tr>
            <tr>
              <p class="ptext">Performance</p>
            </tr>
          </table>
        </div>
        <div class="shade">
          <table>
            <tr>
              <td>
                <img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/Goals_aw4nso.png">
              </td>
              <td>
                <p>Goals</p>
                <hr class="hr">
              </td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284934/EA_n8lvj1.png" </td>
                <td>
                  <p>Effectiveness Assessment</p>
                  <hr class="hr">
                </td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/development_yfv6o1.png" </td>
                <td>
                  <p>Development Plan</p>
                  <hr class="hr">
                </td>
            </tr>
            <tr>
              <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497285433/resources_b3r88g.png"> </td>
              <td>
                <p>Resources
                  <p/>
                  <hr class="hr">
              </td>
            </tr>
          </table>
        </div>
      </a>
    </li>

    <li>
      <div class="infobox">
        <table>
          <tr>
            <img class="team" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497637065/team_nl2cwh.png"></tr>
          <tr>
            <p class="ptext">Team</p>
          </tr>
        </table>
      </div>
      <div class="shade">
        <table>
          <tr>
            <td>
              <img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497300652/Team_iovnl5.png">
            </td>
            <td>
              <p>Team</p>
              <hr class="hr">
            </td>
          </tr>
          <tr>
            <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/Goals_aw4nso.png"> </td>
            <td>
              <p>Team Goals</p>
              <hr class="hr">
            </td>
          </tr>
          <tr>
            <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497284615/development_yfv6o1.png"> </td>
            <td>
              <p>Team Development</p>
              <hr class="hr">
            </td>
          </tr>
          <tr>
            <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497297687/Training-Plan_v43ne7.png"> </td>
            <td>
              <p>Team Training</p>
              <hr class="hr">
            </td>
          </tr>
          <tr>
            <td><img class="smallicons" src="http://res.cloudinary.com/djxai1v1e/image/upload/v1497301295/Approval_f9y1da.png"> </td>
            <td>
              <p>Approvals
                <p/>
                <hr class="hr">
            </td>
          </tr>
        </table>
      </div>

    </li>
  </ul>
</section>
&#13;
&#13;
&#13;

相关问题