单击手风琴图像后保持DIV处于活动状态

时间:2018-03-19 00:54:06

标签: javascript html css accordion

我想将:hover替换为:active对于此codepen中可见的手风琴:https://codepen.io/ferry/pen/ZYVwxz

页面代码为:

 var cardDeck = document.getElementById('listDeck').getElementsByTagName('li')

    for (var i = 0; i < cardDeck.length; i++) {
      cardDeck[i].addEventListener('click', flip, false)
    }

    function flip(e) {
      if (e.target && e.target.nodeName == "LI") {
      var firstMove = document.getElementById(e.target.id)
      firstMove.classList.add('open', 'show')
        console.log(e.target.id + 'card was clicked');
      }
    }
@import url(https://fonts.googleapis.com/css?family=Open+Sans);
@import url(https://fonts.googleapis.com/css?family=Montserrat:700);
h1 {
  text-align: center;
  font-family: Montserrat,sans-serif;
  color: #333;
}

.accordion {
  width: 100%;
  max-width: 1080px;
  height: 250px;
  overflow: hidden;
  margin: 50px auto;
}
.accordion ul {
  width: 100%;
  display: table;
  table-layout: fixed;
  margin: 0;
  padding: 0;
}
.accordion ul li {
  display: table-cell;
  vertical-align: bottom;
  position: relative;
  width: 16.666%;
  height: 250px;
  background-repeat: no-repeat;
  background-position: center center;
  transition: all 500ms ease;
}
.accordion ul li div {
  display: block;
  overflow: hidden;
  width: 100%;
}
.accordion ul li div span {
  display: block;
  width: 100%;
  height: 250px;
  overflow: hidden;
  position: absolute;
}
.accordion ul li div a {
  display: block;
  height: 250px;
  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;
}
.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 {
  font-family: Montserrat,sans-serif;
  text-overflow: clip;
  font-size: 24px;
  text-transform: uppercase;
  margin-bottom: 2px;
  top: 160px;
}
.accordion ul li div a p {
  top: 160px;
  font-size: 13.5px;
}
.accordion ul:active li {
  width: 8%;
}
.accordion ul:active li:active {
  width: 60%;
}
.accordion ul:active li:active a {
  background: rgba(0, 0, 0, 0.4);
}
.accordion ul:active li:active 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:active, .accordion ul:active li, .accordion ul:active li:active {
    position: relative;
    display: table;
    table-layout: fixed;
    width: 100%;
    -webkit-transition: none;
    transition: none;
  }
  .accordion ul li div, .accordion ul li span, .accordion ul li:active div, .accordion ul li:active span, .accordion ul:active li div, .accordion ul:active li span, .accordion ul:active li:active div, .accordion ul:active li:active span {
    display: table-cell;
    width: 100%;
    vertical-align: bottom;
  }
}
.about {
  text-align: center;
  font-family: 'Open Sans', sans-serif;
  font-size: 12px;
  color: #666;
}
.about a {
  color: blue;
  text-decoration: none;
}
.about a:active {
  text-decoration: underline;
}

我希望在用户点击后保持手风琴打开。

我不知道最好的方法是什么,使用复选框hack?使用js或jquery还是别的什么?

我尝试添加以下代码行:

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Responsive Accordion (Inline Images)</title>
  
  
	  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <h1>Responsive Accordion</h1>
<div class="accordion">
  <ul>
    <li>
      <div>
        <span>
		
          <img src="http://michael-ferry.com/assets/accordion1.jpg">
        </span>
        <a href="#">
          <h2>Lorem Ipsum</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
    <li>
      <div>
        <span>
          <img src="http://michael-ferry.com/assets/accordion2.jpg">
        </span>
        <a href="#">
          <h2>Lorem Ipsum</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
    <li>
      <div>
        <span>
          <img src="http://michael-ferry.com/assets/accordion3.jpg">
        </span>
        <a href="#">
          <h2>Lorem Ipsum</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
    <li>
      <div>
        <span>
          <img src="http://michael-ferry.com/assets/accordion4.jpg">
        </span>
        <a href="#">
          <h2>Lorem Ipsum</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
    <li>
      <div>
        <span>
          <img src="http://michael-ferry.com/assets/accordion5.jpg">
        </span>
        <a href="#">
          <h2>Lorem Ipsum</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
    <li>
      <div>
        <span>
          <img src="http://michael-ferry.com/assets/accordion6.jpg">
        </span>
        <a href="#">
          <h2>Lorem Ipsum</h2>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
        </a>
      </div>
    </li>
  </ul>
</div>
</html>

还有jQuery:

var $box_each = $(".accordion ul li a");

$box_each.click(function() {
    console.log('test');
    $box_each.removeClass('active');
    $(this).addClass('active');
});

1 个答案:

答案 0 :(得分:0)

你需要一点css工作。

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

.accordion {
  width: 100%;
  max-width: 1080px;
  height: 250px;
  overflow: hidden;
  margin: 50px auto;
}
.accordion ul {
  width: 100%;
  display: table;
  table-layout: fixed;
  margin: 0;
  padding: 0;
}
.accordion ul li {
  display: table-cell;
  vertical-align: bottom;
  position: relative;
  width: 16.666%;
  height: 250px;
  background-repeat: no-repeat;
  background-position: center center;
  transition: all 500ms ease;
}
.accordion ul li div {
  display: block;
  overflow: hidden;
  width: 100%;
}
.accordion ul li div span {
  display: block;
  width: 100%;
  height: 250px;
  overflow: hidden;
  position: absolute;
}
.accordion ul li div a {
  display: block;
  height: 250px;
  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;
}
.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 {
  font-family: Montserrat,sans-serif;
  text-overflow: clip;
  font-size: 24px;
  text-transform: uppercase;
  margin-bottom: 2px;
  top: 160px;
}
.accordion ul li div a p {
  top: 160px;
  font-size: 13.5px;
}
.accordion ul:hover li {
  width: 8%;
}
.accordion ul:hover li:hover, .accordion ul li.open {
  width: 60%;
}
.accordion ul:hover li:hover a, .accordion ul li.open a {
  background: rgba(0, 0, 0, 0.4);
}
.accordion ul:hover li:hover a *, .accordion ul li.open 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;
  }
  .accordion ul li div, .accordion ul li span, .accordion ul li:hover div, .accordion ul li:hover span, .accordion ul:hover li div, .accordion ul:hover li span, .accordion ul:hover li:hover div, .accordion ul:hover li:hover span {
    display: table-cell;
    width: 100%;
    vertical-align: bottom;
  }
}
.about {
  text-align: center;
  font-family: 'Open Sans', sans-serif;
  font-size: 12px;
  color: #666;
}
.about a {
  color: blue;
  text-decoration: none;
}
.about a:hover {
  text-decoration: underline;
}

这个Javascript:

$('.accordion li').click(function(){
  $(this).toggleClass('open');
});

使用与您正在使用的相同的HTML。