jquery - 元素上的事件类(导航栏)

标签 jquery html css navigation navbar

我一直在研究导航栏,并努力了解我收到的一些 jQuery。

我将在下面发布我的代码,以及代码笔 (https://codepen.io/JustNayWillo/pen/aXpKbb) 的链接,以便您更好地理解。

HTML:

<div id="navbar">

    <li class="tab">

        <a class="link active" href="#home">
            <div class="text">Home</div>
        </a></li>

    <li class="tab">

        <a class="link" href="#work">
            <div class="text">Work</div>
    </a></li>

    <li class="tab">

        <a class="link" href="#about">
            <div class="text">About</div>
    </a></li>
</div>

CSS:

#navbar {
  position: absolute;
  text-align: right;
  top: 3.5em;
  right: 3.5em;
  z-index: 2;
  list-style-type: none;
}

.tab {
  background-color: white;
  opacity: 0.3;
  height: 3.5em;
  width: 0.2em;
  margin-bottom: 1em;
}

.active, .tab:hover {
  opacity:1;
  transition:0.7s ease;
}

.active, .link:hover {
  opacity:1;
  transition:0.7s ease;
}

.active, .text:hover {
  opacity:1;
  transition:0.7s ease;
}

JS/jQuery:

$(function() {
    var links = $('.tab > .link');
    links.on('click', function() {
        links.closest('.tab').removeClass('active');
        $(this).closest('.tab').addClass('active');
    });
});

一切正常,但导航不是以事件类开始的,我希望“主页”在开始时处于事件状态。另一个问题是文本不会保持事件状态,仅在悬停时显示,我希望它保持不透明度:事件时为 1。

谢谢你们。

最佳答案

添加这个 CSS

.text:hover, .active .text {
  opacity:1;
  transition:0.7s ease;
}

HTML 更改

选项卡上添加事件

<li class="tab active">

https://codepen.io/anon/pen/yZgExB

关于jquery - 元素上的事件类(导航栏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54460874/

相关文章:

html - 仅CSS的砌体布局

html - 100% 的子 div 高度被忽略

javascript - 删除特定的事件处理程序

javascript - touchSwipe jquery点击问题

html - li float left 的小 css 问题

javascript - 使用按钮更改 Google 图表数据和外观

html - 在 SASS 中,有什么方法可以在可扩展选择器中引用选择器值吗?

jquery - 滚动过去时,导航栏会停留在屏幕顶部

javascript - 将多个字符串值作为参数从服务器端传递给 JavaScript 函数

html - CSS3动画div位置导致不可点击区域并且不触发动画