简体   繁体   English

使悬停时的同级变小。 jQuery转Vanilla JS

[英]Fade siblings on hover. jQuery to vanilla JS

I have the following navigation. 我有以下导航。 Upon mouseover of a link, I want the other two links' opacity to lower and return to full opacity upon mouseout. 当鼠标悬停在链接上时,我希望其他两个链接的不透明度降低,并在鼠标移出时恢复为完全不透明。
I have accomplished this with jQuery and have found other questions/answer with a mix, but am struggling with a pure vanilla javascript solution as I do not want to use jQuery at all for this project. 我已经用jQuery完成了这一工作,并且还找到了其他问题/答案,但是我正苦苦寻求纯正的javascript解决方案,因为我根本不想为此项目使用jQuery。

HTML 的HTML

<nav id="nav-main">
  <h1><a>Home</a></h1>
  <h1><a>About</a></h1>
  <h1><a>Contact</a></h1>
</nav>

jQuery jQuery的

$(function(){
  $('#nav-main a').hover(function(){
    var $this = $(this);
    $this.siblings().stop().fadeTo(300, 1);
    $this.parent().siblings().stop().fadeTo(300, 0.3);
  },function(){
    var $this = $(this);
    $this.siblings().stop().fadeTo(300, 1);
    $this.parent().siblings().stop().fadeTo(300, 1);
  });
});

Fiddle 小提琴

If you don't want to reinvent the wheel, you could use Google Closure's library which compiles to pure Javascript whilst still allowing you to use library-style functions. 如果您不想重新发明轮子,可以使用Google Closure的库,该库可以编译为纯Javascript,同时仍然允许您使用库样式的函数。 (Unlike jQuery, Closure includes all the library machinery in the compiled code.) (与jQuery不同,Closure在编译后的代码中包括了所有库机制。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM