css 如何创建一个卡片英雄滑块,当点击显示/隐藏卡?

mnowg1ta  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(127)

你好,我期待重新创建一个英雄滑块旋转木马类似在这个截图。只是寻找一个起点或任何提示如何研究这样的东西。举个例子会很有帮助。

点击卡片将打开卡片&同时隐藏不活动的卡片。它使非活动图像稍微变暗,仅显示图像的一小部分。
对不起,我没有代码显示,因为我正在寻找一个例子,如何重新创建什么的形象。谢谢!
在该示例中,中间图像是活动卡,因此该图像与其下的描述一起被完整地显示。不活动的卡只显示一半的图像,而且它们是灰色的

dy2hfwbg

dy2hfwbg1#

// Get the container element
var btnContainer = document.getElementById("item");

// Get all buttons with class="btn" inside the container
var btns = btnContainer.getElementsByClassName("items");

// Loop through the buttons and add the active class to the current/clicked button
for (var i = 0; i < btns.length; i++) {
  btns[i].addEventListener("click", function() {
  //this.classList.add("active");
    
  var current = document.getElementsByClassName("active");  
    // If there's no active class
    if (current.length > 0) {
      current[0].className = current[0].className.replace(" active", "");
    }
    
  this.className += " active";
  
    
  });
}
.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 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 *:not(.bar){
  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 .bar{
    writing-mode: tb-rl;
    transform: rotate(-180deg);
    position: absolute;
    background-color: yellowgreen;
    padding: 10px;
    height: 100%;
    width: auto;
    right: 0;
    top: 0;
    display: block;
    box-sizing: border-box;
}
.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 li:nth-child(1) { background-image: url("https://picsum.photos/id/237/200/300"); background-size: 100%;background-size: cover; }
.accordion ul li:nth-child(2) { background-image: url("https://picsum.photos/id/236/200/300"); background-size: cover;}
.accordion ul li:nth-child(3) { background-image: url("https://picsum.photos/id/235/200/300"); background-size: cover;}
.accordion ul li.active { width: 60%; }
.accordion ul li a { background: rgba(0, 0, 0, 0.5); }
.accordion ul li.active a, .accordion ul li:hover a { background: rgba(0, 0, 0, .0); }
.accordion ul li.active a * {
  opacity: 1;
  -webkit-transform: translateX(0);
  transform: translateX(0);
}
<div class="accordion">
  <ul id="item">
    <li class="items">
    <div>
      <a href="#">
        <span class="bar">DEMO1</span> 
        <h2>Title 1</h2>
        <p>Description 1</p>
      </a>
    </div>
    </li>
    <li class="items">
      <div> <a href="#">
      <span class="bar">DEMO2</span>
        <h2>Title 2</h2>
        <p>Description 2</p>
        </a> </div>
    </li>
    <li class="items">
      <div> <a href="#">
      <span class="bar">DEMO3</span>
        <h2>Title 3</h2>
        <p>Description 3</p>
        </a> </div>
    </li>
  </ul>
</div>

结果如你所愿吗?你能检查一下吗?

相关问题