使键盘标签操作充当鼠标悬停

时间:2018-04-10 18:03:38

标签: html css tabs hover tabindex

我正在使用稍微修改过的版本:https://codepen.io/ferry/pen/ZYVwxz(谢谢Michael Ferry)。

HTML:

    <div class="accordion">
  <ul>
    <li tabindex="0">
      <div>
        <a href="#">
          <h2>Lorem Ipsum 1</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
    <li tabindex="0">
      <div>
        <a href="#">
          <h2>Lorem Ipsum 2</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
    <li tabindex="0">
      <div>
        <a href="#">
          <h2>Lorem Ipsum 3</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
    <li tabindex="0">
      <div>
        <a href="#">
          <h2>Lorem Ipsum 4</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
    <li tabindex="0">
      <div>
        <a href="#">
          <h2>Lorem Ipsum 5</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
    <li tabindex="0">
      <div>
        <a href="#">
          <h2>Lorem Ipsum 6</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
  </ul>
</div>

CSS:

    <style>
.accordion {
  width: 100%;
  max-width: 1140px;
  height: 560px;
  overflow: hidden;
  margin: 20px auto;
}
.accordion ul {
  width: 100%;
  display: table;
  table-layout: fixed;
  margin: 0;
  padding: 0;
}
.accordion ul li {
list-style-type: none;
  display: table-cell;
  top:0px;
  vertical-align: top;
  position: relative;
  width: 16.666%;
  height: 520px;
  background-repeat: no-repeat;
  background-position: top center;
  transition: all 500ms ease;
}
.accordion ul li div {
  display: block;
  overflow: hidden;
  width: 100%;
}
.accordion ul li div a {
  display: block;
  height: 520px;
  width: 100%;
  position: relative;
  z-index: 3;
  vertical-align: bottom;
  padding: 0px;
  box-sizing: border-box;
  color: #fff;
  text-decoration: none;
  transition: all 200ms ease;
}
.accordion ul li div a * {
  opacity: 0;
  margin: 0;
  width: 100%;
  text-overflow: ellipsis;
  position: relative;
  z-index: 5;
  white-space: nowrap;
  overflow: hidden;
  -webkit-transform: translateX(-20px);
  transform: translateX(-20px);
  -webkit-transition: all 400ms ease;
  transition: all 400ms ease;
}
.accordion ul li div a h2 {
  text-overflow: clip;
  font-size: 24px;
  /*text-transform: uppercase;*/
  margin-bottom: 0px;
  top: 435px;
  color:#000;
}
.accordion ul li div a p {
  top: 440px;
  font-size: 13.5px;
  color:#000;
}
.accordion ul li:nth-child(1) {
  background-image: url("1b.jpg");max-height:400px;
}
.accordion ul li:nth-child(2) {
  background-image: url("2b.jpg");max-height:400px;
}
.accordion ul li:nth-child(3) {
  background-image: url("3b.jpg");max-height:400px;
}
.accordion ul li:nth-child(4) {
  background-image: url("4b.jpg");max-height:400px;
}
.accordion ul li:nth-child(5) {
  background-image: url("5b.jpg");max-height:400px;
}
.accordion ul li:nth-child(6) {
  background-image: url("6b.jpg");max-height:400px;
}
.accordion ul:hover li {
  width: 16%;
}
.accordion ul:hover li:hover {
  width: 100%;
}
.accordion ul:hover li:hover a {

}
.accordion ul:hover li:hover a * {
  opacity: 1;
  -webkit-transform: translateX(0);
  transform: translateX(0);
}


@media screen and (max-width: 600px) {
  body {
    margin: 0;
  }

  .accordion {
    height: auto;
  }
  .accordion ul li, .accordion ul li:hover, .accordion ul:hover li, .accordion ul:hover li:hover {
    position: relative;
    display: table;
    table-layout: fixed;
    width: 100%;
    -webkit-transition: none;
    transition: none;
  }
}
</style>

使用键盘时无法显示“幻灯片”。

我尝试添加.active,:focus,role =“button”,aria-expanded =“true”和tab-index无效。

在我去秃顶之前,有人有解决方案吗? (不使用javascript)

非常感谢!

1 个答案:

答案 0 :(得分:1)

如果您使用tabindex="1"上的li <li tabindex="1">:focus就可以li { &:focus { // your stuff here } } 作为

a

现在发生的事情是当您按Tab键选择li元素但转换应用于 <h1>Responsive Accordion</h1> <div class="accordion"> <ul> <li tabindex="1"> <div> <a href="#"> <h2>Lorem Ipsum</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p> </a> </div> </li> <li tabindex="1"> <div> <a href="#"> <h2>Lorem Ipsum</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p> </a> </div> </li> <li tabindex="1"> <div> <a href="#"> <h2>Lorem Ipsum</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p> </a> </div> </li> <li tabindex="1"> <div> <a href="#"> <h2>Lorem Ipsum</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p> </a> </div> </li> <li tabindex="1"> <div> <a href="#"> <h2>Lorem Ipsum</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p> </a> </div> </li> <li tabindex="1"> <div> <a href="#"> <h2>Lorem Ipsum</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p> </a> </div> </li> </ul> </div> <p class="about"> By <a href="http://michaelferry.com/">Michael Ferry</a> </p> <h1>Responsive Accordion</h1> <div class="accordion"> <ul> <li tabindex="1"> <div> <a href="#"> <h2>Lorem Ipsum</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p> </a> </div> </li> <li tabindex="1"> <div> <a href="#"> <h2>Lorem Ipsum</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p> </a> </div> </li> <li tabindex="1"> <div> <a href="#"> <h2>Lorem Ipsum</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p> </a> </div> </li> <li tabindex="1"> <div> <a href="#"> <h2>Lorem Ipsum</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p> </a> </div> </li> <li tabindex="1"> <div> <a href="#"> <h2>Lorem Ipsum</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p> </a> </div> </li> <li tabindex="1"> <div> <a href="#"> <h2>Lorem Ipsum</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p> </a> </div> </li> </ul> </div> <p class="about"> By <a href="http://michaelferry.com/">Michael Ferry</a> </p>

如果我从Michael Ferry的代码笔中获取代码,您将拥有以下代码。

HTML:

@import url(https://fonts.googleapis.com/css?family=Open+Sans);
@import url(https://fonts.googleapis.com/css?family=Montserrat:700);

// Accordion Height
$a-height: 250px;

// Position text along bottom
$text-offset: $a-height - 90;

// Page Title
h1 {
  text-align: center;
  font-family: Montserrat, sans-serif;
  color: #333;
}

.accordion {
  width: 100%;
  max-width: 1080px;
  height: $a-height;
  overflow: hidden;
  margin: 50px auto;

  ul {
    width: 100%;
    display: table;
    table-layout: fixed;
    margin: 0;
    padding: 0;

    li {
      display: table-cell;
      vertical-align: bottom;
      position: relative;
      width: 16.666%; // 6 into 100
      height: $a-height;

      background-repeat: no-repeat;
      background-position: center center;

      transition: all 500ms ease;

      div {
        display: block;
        overflow: hidden;
        width: 100%;

        a {  
          display: block;
          height: $a-height;
          width: 100%;

          position: relative;
          z-index: 3;
          vertical-align: bottom;
          padding: 15px 20px;
          box-sizing: border-box;
          color: #fff;
          text-decoration: none;
          font-family: Open Sans, sans-serif;

          transition: all 200ms ease;

          * {
            opacity: 0;
            margin: 0;
            width: 100%;
            text-overflow: ellipsis;
            position: relative;
            z-index: 5;

            white-space: nowrap;
            overflow: hidden;

            -webkit-transform: translateX(-20px);
            transform: translateX(-20px);

            -webkit-transition: all 400ms ease;
            transition: all 400ms ease;
          }

          h2 {
            font-family: Montserrat, sans-serif;
            text-overflow: clip;
            font-size: 24px;
            text-transform: uppercase;
            margin-bottom: 2px;

            top: $text-offset;
          }

          p {
            top: $text-offset;
            font-size: 13.5px;
          }
        }
      }
    }

    // Background images
    li:nth-child(1) {
      background-image: url("http://michael-ferry.com/assets/accordion1.jpg");
    }
    li:nth-child(2) {
      background-image: url("http://michael-ferry.com/assets/accordion2.jpg");
    }
    li:nth-child(3) {
      background-image: url("http://michael-ferry.com/assets/accordion3.jpg");
    }
    li:nth-child(4) {
      background-image: url("http://michael-ferry.com/assets/accordion4.jpg");
    }
    li:nth-child(5) {
      background-image: url("http://michael-ferry.com/assets/accordion5.jpg");
    }
    li:nth-child(6) {
      background-image: url("http://michael-ferry.com/assets/accordion6.jpg");
    }

    &:hover li,
    li:focus ~ li{
      width: 8%;
    }

    &:hover li:hover,
    li:focus {
      width: 60%;

      a {
        background: rgba(0, 0, 0, 0.4);

        * {
          opacity: 1;
          -webkit-transform: translateX(0);
          transform: translateX(0);
        }
      }
    }
  }
}

// Stack items
@media screen and (max-width: 600px) {
  // IE gets fussy if this isn't here
  body {
    margin: 0;
  }

  .accordion {
    height: auto;

    ul,
    ul:hover {
      li,
      li:hover {
        position: relative;
        display: table;
        table-layout: fixed;
        width: 100%;

        -webkit-transition: none;
        transition: none;
      }
    }
  }
}

.about {
  text-align: center;
  font-family: "Open Sans", sans-serif;
  font-size: 12px;
  color: #666;

  a {
    color: blue;
    text-decoration: none;

    &:hover {
      text-decoration: underline;
    }
  }
}

SCSS:

li:focus ~ li

在SCSS代码中,我已将li:focus添加到目标li的兄弟姐妹中,并a添加到现有代码中以重现动画。

我还建议您使用tabindex=0禁用{{1}}元素标签,以避免用户在浏览滑块后再次向上滚动。