javascript - 如何在当前页面上保留最后一个关键帧上的导航栏元素的动画?
问题描述
我有一个自定义动画,当您将链接悬停时,一条线从左向右流动(参见示例:https ://jsfiddle.net/mxfiddle/fubLo45j/2/ )。
我希望这条线停留在我所在的当前页面上。
一般来说,我对 Web 开发完全陌生,因此非常感谢任何帮助。
提前致谢。
我添加活动类的代码:
// Get the container element
var navbarContainer = document.getElementById("navbar");
// Get all navbar elements with class="navElement" inside the container
var navElement = navbarContainer.getElementsByClassName("navElement");
// Loop through the navbar elements and add the active class to the current/clicked button
for (var i = 0; i < navElement.length; i++) {
navElement[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
});
}
解决方案
将此添加到您的页面:
document.body.onload = function() {
// on page load
document.querySelectorAll("#navbar a").forEach(a => {
//catch all links in navbar
a.href === location.pathname ? a.classList.add("active") : a.classList.remove("active")
// if navbar link is the same as current loacation pathname add active class, if not remove it.
})
};
您需要类似这样的以太或将单击的项目保存在本地存储中并在其他页面上阅读...
我希望你明白你可以简单地从家里删除活动课程并根据你所在的页面在 HTML 中放置一个当前链接......
推荐阅读
- javascript - 你如何在 JavaScript 中创建一个 12 小时的时间
- r - 使用 facet_wrap() 时在 ggplot 中手动标记轴
- r - 要在 R 中列出的数据框列的唯一值
- jenkins - 如何使用 curl 和身份验证令牌将参数传递给 Jenkins
- python - 将行中的所有值加 100 并更改零的函数
- database - 在 laravel 8 中使用 vuejs 从数据库中获取数据
- django - 设计问题:如何设计临时用户表?
- angular - 在Angular中将一个数据字段绑定到另一个?
- docker - 用户定义的桥接网络上的 Docker 容器无法通信
- python - 将什么 dtype 数据传递到 Tensorflow 模型中进行预测是否重要?