html - 使键盘选项卡 Action 充当鼠标悬停

标签 html css tabs hover tabindex

我使用的是稍微修改过的版本:https://codepen.io/ferry/pen/ZYVwxz (谢谢迈克尔·费里)。

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)

非常感谢!

最佳答案

如果您使用 tabindex="1"在你的 li喜欢<li tabindex="1"> , 你应该能够用 :focus 来定位他们作为

li {
  &:focus {
    // your stuff here
  }
}

现在发生的事情是,当您按下 Tab 键时,它会选择 a。元素,但转换应用于 li .

如果我从 Michael Ferry 的代码笔中获取代码,您将得到以下几行代码。

HTML:

    <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>

SCSS:

@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针对李的 sibling 和li:focus到现有代码以重现动画。

我还建议您禁用 a元素标签 tabindex=0为了避免用户在通过 slider 切换后再次滚动到顶部。

关于html - 使键盘选项卡 Action 充当鼠标悬停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49760275/

相关文章:

Android:如何在 TabWidget 上删除选定的选项卡突出显示颜色和按下时突出显示

jquery - 从外部 URL Bootstrap 加载选项卡

javascript - 在新选项卡中打开重定向页面并在多个重定向 Django 的同一选项卡中重新打开

java - GWT 安全 HTML 框架 : When to use, 为什么?

html - 在 Word 2007 编写的文档中哪里可以找到 HTML 源代码?

html - CSS:当不知道div的固定大小时垂直对齐div

javascript - 溢出-x : hidden breaks smooth scroll

javascript - CSS 交付优化 : How to defer css loading?

html - 如何在第一个字符之前按字母顺序从上到下排列名称

javascript - 如何在灯箱中显示动态数据