热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在同一页面中导航锚标签时关闭向左滑动菜单?-CloseSlide-LeftMenuwhennavigatinganchortagsinthesamepage?

WhenItoggleopenmySlide-Leftmenuandselectananchortaglink,itnaviagtestotheanchoredse

When I toggle open my Slide-Left menu and select an anchor tag link, it naviagtes to the anchored section on the same page, but the menu remains open.

当我切换打开我的Slide-Left菜单并选择一个锚标记链接时,它会导航到同一页面上的锚定部分,但菜单仍保持打开状态。

I would like a simple Javascript function to close the toggle menu everytime I click on an achor link and navigate to the desire section

每当我点击achor链接并导航到欲望部分时,我想要一个简单的Javascript函数来关闭切换菜单

https://jsfiddle.net/trsjL9th/

I've tried all the other solutions I found on stack overflow including:

我已经尝试了我在堆栈溢出时发现的所有其他解决方案,包括:

$('#menu li').on('click', function(){ $("#menu").hide(); $("#menu-icon").removeClass("active"); });

$('#menin li')。on('click',function(){$(“#menu”)。hide(); $(“#menu-icon”)。removeClass(“active”);}) ;

But nothing seems to be working, I'm still really new at Javascript so any help or push in the right direction will be greatly appreciated.

但似乎没有任何工作,我仍然是Javascript的新手,所以任何帮助或推动正确的方向将不胜感激。

Javascript:

(function(window) {

  'use strict';

  /**
   * Extend Object helper function.
   */
  function extend(a, b) {
    for(var key in b) { 
      if(b.hasOwnProperty(key)) {
        a[key] = b[key];
      }
    }
    return a;
  }

  /**
   * Each helper function.
   */
  function each(collection, callback) {
    for (var i = 0; i 

HTML:




About

Contact

How it Works

Tools

Help

Customers

Business

Location

CSS:

.c-menu {
  position: fixed; 
  z-index: 200;
  background-color: #333;
  transition: transform 0.3s;
}

.c-menu__items {
  list-style: none;
  margin: 0;
  padding: 0; margin-top:90px;
}



.c-menu__items li {
  font-size:20px; margin-top:20px;
}

/**
 * Close button resets.
 */
.c-menu__close {
  color: #fff; 
  background-color:#333;
  font-size: 14px;
  border: none; 
  box-shadow: none;
  border-radius: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  cursor: pointer;
}

/**
 * Close button resets.
 */
.c-menu__close:focus {
  outline: none;
}

/**
 * Body states.
 *
 * When a menu is active, we want to hide the overflows on the body to prevent
 * awkward document scrolling.
 */
body.has-active-menu {
  overflow: hidden;
}

 .c-button {

    background-color: blue; 
    background-repeat: no-repeat;  
    background-position: 0px 0px;  
    border: none; margin-top:13px; margin-left:5px;          
    cursor: pointer; z-index:1;       
    height: 25px;          
    padding-left: 25px;     
    vertical-align: middle;      
 }


 .c-menu__close { 
    background-color: white; 
    background-repeat: no-repeat;  
    background-position: 0px 0px;  
    border: none; margin-top:10px; margin-right:2px;          
    cursor: pointer;        
    height: 25px;          
    padding-left: 25px;     
    vertical-align: middle;      
 }



.c-mask {
  position: fixed;
  z-index: 100;
  top: 0;
  left: 0;
  overflow: hidden;
  width: 0;
  height: 0;
  background-color: #000;
  opacity: 0;
  transition: opacity 0.3s, width 0s 0.3s, height 0s 0.3s;
}

.c-mask.is-active {
  width: 100%;
  height: 100%;
  opacity: 0.7;
  transition: opacity 0.3s;
}

.c-menu--slide-left,
.c-menu--slide-right,
.c-menu--push-left,
.c-menu--push-right {
  width: 100%;
  height: 100%;
  overflow-y: auto;
}

@media all and (min-width: 320px) {
  .c-menu--slide-left,
  .c-menu--slide-right,
  .c-menu--push-left,
  .c-menu--push-right {
    width: 300px;
  }
}

.c-menu--slide-left .c-menu__item,
.c-menu--slide-right .c-menu__item,
.c-menu--push-left .c-menu__item,
.c-menu--push-right .c-menu__item {
  display: block;
  text-align: left;

}

.c-menu--slide-left .c-menu__item:first-child,
.c-menu--slide-right .c-menu__item:first-child,
.c-menu--push-left .c-menu__item:first-child,
.c-menu--push-right .c-menu__item:first-child {
  border-top: none;
}

.c-menu--slide-left .c-menu__item:last-child,
.c-menu--slide-right .c-menu__item:last-child,
.c-menu--push-left .c-menu__item:last-child,
.c-menu--push-right .c-menu__item:last-child {
  border-bottom: none;
}

.c-menu--slide-left .c-menu__link,
.c-menu--slide-right .c-menu__link,
.c-menu--push-left .c-menu__link,
.c-menu--push-right .c-menu__link {
  display: block; text-decoration:none; margin-top:15px;
  padding: 4px 24px; 
  color: #fff;
}

.c-menu__link:hover {color:#c8d100;}

.c-menu--slide-left .c-menu__close,
.c-menu--slide-right .c-menu__close,
.c-menu--push-left .c-menu__close,
.c-menu--push-right .c-menu__close {
  display: block;
  padding: 10px 10px; clear:both;
  width: 30px;float:right;
}

.c-menu--slide-left,
.c-menu--push-left {
  top: 0;
  left: 0;
  transform: translateX(-100%);
}

@media all and (min-width: 320px) {
  .c-menu--slide-left,
  .c-menu--push-left {
    transform: translateX(-300px);
  }
}

.c-menu--slide-left.is-active,
.c-menu--push-left.is-active {
  transform: translateX(0);
}

.c-menu--slide-right,
.c-menu--push-right {
  top: 0;
  right: 0;
  transform: translateX(100%);
}

@media all and (min-width: 320px) {
  .c-menu--slide-right,
  .c-menu--push-right {
    transform: translateX(300px);
  }
}

.c-menu--slide-right.is-active,
.c-menu--push-right.is-active {
  transform: translateX(0);
}

.o-wrapper.has-push-left {
  transform: translateX(100%);
}

@media all and (min-width: 320px) {
  .o-wrapper.has-push-left {
    transform: translateX(300px);
  }
}

.o-wrapper.has-push-right {
  transform: translateX(-100%);
}

@media all and (min-width: 320px) {
  .o-wrapper.has-push-right {
    transform: translateX(-300px);
  }
}




#about { background:blue; height:100vh; width:100%; color:white; font-size:100px;}

#contact { background:yellow; height:100vh; width:100%; color:white; font-size:100px;}

#howitworks { background:green; height:100vh; width:100%; color:white; font-size:100px;}

#tools { background:red; height:100vh; width:100%; color:white; font-size:100px;}

#about { background:black; height:100vh; width:100%; color:white; font-size:100px;}

#help { background:green; height:100vh; width:100%; color:white; font-size:100px;}

#customers { background:purple; height:100vh; width:100%; color:white; font-size:100px;}

#business { background:orange; height:100vh; width:100%; color:white; font-size:100px;}

#location { background:brown; height:100vh; width:100%; color:white; font-size:100px;}

2 个解决方案

#1


2  

I added this code to the bottom of your Javascript:

我将此代码添加到Javascript的底部:

// Find all menu links
var navLinks = document.querySelectorAll('.c-menu__link');
// For each menu link
var index = 0, length = navLinks.length;
for ( ; index 

Fiddle: https://jsfiddle.net/trsjL9th/3/

Basically finds all the links in the menu by their class and attaches a click event that calls your menu close function.

基本上可以通过类找到菜单中的所有链接,并附加一个调用菜单关闭功能的单击事件。

#2


0  

Add an ID to a link:

在链接中添加ID:

    
  • about
  • Javascript add an onclick listener:

    Javascript添加一个onclick监听器:

    document.getElementById('about-link').addEventListener('click', function(e) {
      e.preventDefault;
      slideLeft.close();
    });
    

    Repeat as necessary for other ID's. I find this to be the most easily-readable code for when someone reviews the code 1-2 years down the line, but YMMV.

    根据需要重复其他ID。我发现这是最容易阅读的代码,当有人在1 - 2年后审查代码时,但是YMMV。


    推荐阅读
    author-avatar
    嗯哼
    很清新
    PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
    Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有