1

function openCity(evt, cityName) {

  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
   }
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.className += " active";
}

document.getElementById("defaultOpen").click();
body {font-family: Arial;}

/* Style the tab */
.tab {
 overflow: hidden;
 border: 1px solid #ccc;
 background-color: #f1f1f1;
 }

/* Style the buttons inside the tab */
.tab button {
 background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
 font-size: 17px;
}

/* Change background color of buttons on hover */
.tab button:hover {
  background-color: #ddd;
}

/* Create an active/current tablink class */
.tab button.active {
  background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
  display: none;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://unpkg.com/aos@next/dist/aos.css" />
</head>
<body>
<p data-aos="zoom-out-up">In this example, we use JavaScript to "click" on the London button, to open the tab on page load.</p>

<div class="tab">
 <button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen">London</button>
 <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
 <button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
</div>

<div id="London" class="tabcontent" data-aos="zoom-in">
  <h3>London</h3>
  <p>London is the capital city of England.</p>
   </div>

<div id="Paris" class="tabcontent" data-aos="fade-down">
  <h3>Paris</h3>
  <p>Paris is the capital of France.</p> 
</div>

<div id="Tokyo" class="tabcontent" data-aos="flip-up">
 <h3>Tokyo</h3>
 <p>Tokyo is the capital of Japan.</p>
</div>
<script src="https://unpkg.com/aos@next/dist/aos.js"></script>
  <script>
    AOS.init();
  </script>
  </body>
</html> 

我正在尝试了解 AOS 动画。上面的代码是我尝试使用的示例代码。在加载动画期间,第一个选项卡内容有效。动画不适用于第二个和第三个选项卡。然后在更改选项卡时动画不起作用。我没有使用任何其他库。有什么建议吗?

4

1 回答 1

0

当元素在垂直滚动期间进入视口时,AOS 会对元素进行动画处理。这就是为什么第一个选项卡在页面加载时动画,而隐藏的其他元素没有;即使在它们变得可见之后。如果您想在选项卡可见后为元素设置动画,您应该寻找另一个基于添加动画元素的库,以便您可以通过编程classes方式添加适当的元素。classes我可以建议Animate.css

于 2021-08-08T20:54:27.570 回答