简体   繁体   English

魔术下拉导航菜单?

[英]Magic Dropdown Nav Menu?

Please see the desktop hover dropdown animation/style at this site:请在此站点上查看桌面悬停下拉动画/样式:

https://weworkremotely.com/ https://weworkremotely.com/

I've seen this style of navbar menu on a few sites recently;我最近在一些网站上看到了这种风格的导航栏菜单; where the dropdown moves with the cursor.下拉菜单随光标移动的位置。 Does anyone have an idea of what framework this is a part of?有谁知道这是哪个框架的一部分? Or any link to a tutorial/guide to get something like this working?或者任何指向教程/指南的链接以获得这样的工作? I've tried viewing the source code and googling around for Magic Nav's but to no avail!我试过查看源代码并在谷歌上搜索 Magic Nav,但无济于事!

Cheers,干杯,

Kyle凯尔

In this case, it looks like a custom JavaScript made after Prototype Framework, but not really related to it.在这种情况下,它看起来像是在Prototype Framework 之后制作的自定义 JavaScript,但实际上与它无关。
It is one of the last scripts loaded on the end of the body and here is a piece of the code related to the magic menu that you can analyse to understand how it works.它是加载到正文末尾的最后一个脚本之一,这里有一段与魔法菜单相关的代码,您可以对其进行分析以了解其工作原理。

var triggers = document.querySelectorAll(".wwr__nav > li.magic__nav");
var background = document.querySelector(".dropdownBackground");
var nav = document.querySelector(".top");
var search_bar = document.getElementById("index-search-bar");
var search_term = document.getElementById("search_term")

function searchDropdown() {
  if (search_bar.classList.contains('hidden')) {
    search_bar.classList.remove('hidden')
    search_bar.classList.add('toggle-slidein')
    setTimeout(function(){
      search_term.focus()
    }, 30);
    // Hide search bar if not targeted
    document.addEventListener('mouseup', function (e) {
      if (!search_bar.contains(e.target)) {
          search_bar.classList.add('hidden')
      }
    }.bind(this));
  } else {
    search_bar.classList.add('hidden')
  }
}

function handleEnter() {
  this.classList.add("trigger-enter");
  setTimeout(
    () =>
    this.classList.contains("trigger-enter") &&
    this.classList.add("trigger-enter-active"),
    150
  );
  background.classList.add("open");

  var dropdown = this.querySelector(".dropdown");
  var dropdownCoords = dropdown.getBoundingClientRect();
  var navCoords = nav.getBoundingClientRect();

  var coords = {
    height: dropdownCoords.height,
    width: dropdownCoords.width,
    top: dropdownCoords.top - navCoords.top,
    left: dropdownCoords.left - navCoords.left
  };

  background.style.setProperty("width", `${coords.width}px`);
  background.style.setProperty("height", `${coords.height}px`);
  background.style.setProperty(
    "transform",
    `translate(${coords.left}px, ${coords.top}px)`
  );
}

function handleLeave() {
  this.classList.remove("trigger-enter", "trigger-enter-active");
  background.classList.remove("open");
}

triggers.forEach(trigger =>
  trigger.addEventListener("mouseenter", handleEnter)
);
triggers.forEach(trigger =>
  trigger.addEventListener("mouseleave", handleLeave)
);

/* Toggle between showing and hiding the navigation menu links when the user clicks on the hamburger menu / bar icon */
function myFunction() {
  var x = document.getElementById("myLinks");

  if (x.style.display === "block") {
    x.style.display = "none";
  } else {
    x.style.display = "block";
  }
}

/* Accordian within nav */
var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function () {
    this.classList.toggle("active-options");
    var panel = this.nextElementSibling;
    if (panel.style.maxHeight) {
      panel.style.maxHeight = null;
    } else {
      panel.style.maxHeight = panel.scrollHeight + "px";
    }
  });
}
;

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

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