使 javascript 推送菜单可滚动(无滚动条)

我有一个从页面左侧滑出的推送菜单(从左到右),将页面向右滑动。我试图为它添加一个保护措施,如果链接超过页面高度,菜单是可滚动的。现在,它不起作用并且链接被隐藏。有人可以帮我弄清楚如何使菜单可滚动。


我试过添加scroll-y: scroll几个 css 元素,但这似乎没有任何区别。我希望滚动功能在手机上也能通过触摸滚动。


而且,我想看看如何在菜单打开时禁用“主体”上的滚动。


$(document).ready(function() {

  $menuLeft = $('.pushmenu-left');

  $nav_list = $('#nav_list');

  $nav_list.click(function() {

    $(this).toggleClass('active');

    $('.pushmenu-push').toggleClass('pushmenu-push-toright');

    $menuLeft.toggleClass('pushmenu-open');

  });

});

.pushmenu {

  background: #444;

  text-align: center;

  font-family: Tahoma, Geneva, sans-serif;

  width: 300px;

  height: 100%;

  top: 0;

  z-index: 1000;

  position: fixed;


}


.pushmenu h3 {

  color: #f1f1f1;

  font-size: 1.3em;

  font-weight: 400;

  padding: 15px 25px;

  margin: 0;

  background: #333;

  height: 16px;

}


.links {

  list-style-type: none;

  padding: 0;

  margin: 0 0 0 25%;

  width: 50%;


}


.links li { margin-top: 30px; }


.links li a {

  position: relative;

  display: block;

  color: #f1f1f1;

  font-size: 1.3em;

  font-weight: 400;

  text-decoration: none;

  padding: 14px;

}


.links li a:after {

  content: '';

  display: block;

  position: absolute;

  left: 20px;

  bottom: -5px;

  width: 0;

  height: 4px;

  background-color: #f1f1f1;

  -webkit-transition: width 0.3s ease;

  -moz-transition: width 0.3s ease;

  transition: width 0.3s ease;

}


.links li a:hover:after { width: 70%; }


.links li a:active { color: #dbdbdb; }


.pushmenu-left { left: -300px; }


.pushmenu-left.pushmenu-open { left: 0; }


.pushmenu-push {

  overflow-x: hidden;

  position: relative;

  left: 0;

}


.pushmenu-push-toright { left: 300px; }




.pushmenu, .pushmenu-push {

-webkit-transition:all 0.5s ease;

-moz-transition:all 0.5s ease;

transition:all 0.5s ease;

}

<body class="pushmenu-push">

<nav class="pushmenu pushmenu-left">

  <ul class="links">

    <li><a href="index">Home</a></li>

    <li><a href="#">About Us</a></li>

    <li><a href="#">Missions</a></li>

    <li><a href="#">Partners</a></li>

    <li><a href="#">Events</a></li>

    <li><a href="#">Contact</a></li>

    <li><a href="#">Give</a></li>

   

慕运维8079593
浏览 140回答 3
3回答

饮歌长啸

我最终找到了一种方法,通过添加一个内部容器并在两者上使用溢出,在没有滚动条的情况下做我想做的事情。.pushmenu {&nbsp; background: #444;&nbsp; text-align: center;&nbsp; font-family: Tahoma, Geneva, sans-serif;&nbsp; width: 300px;&nbsp; height: 100%;&nbsp; top: 0;&nbsp; z-index: 1000;&nbsp; position: fixed;&nbsp; &nbsp; overflow: hidden;}.pushmenu_inner{&nbsp; &nbsp; width: 100%;&nbsp; &nbsp; height: 99%;&nbsp; &nbsp; overflow: auto;&nbsp; &nbsp; padding-right: 15px;}

拉丁的传说

有一个名为 overflow 的 CSS 属性,它控制超出可用空间时的内容。将其设置为自动会自动添加一个滚动条。pushmenu {&nbsp; background: #444;&nbsp; text-align: center;&nbsp; font-family: Tahoma, Geneva, sans-serif;&nbsp; width: 300px;&nbsp; height: 100%;&nbsp; top: 0;&nbsp; z-index: 1000;&nbsp; position: fixed;&nbsp; overflow: auto;}

倚天杖

您可以使用此代码<!doctype html><html><head>&nbsp; <meta charset="utf-8">&nbsp; <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">&nbsp; <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">&nbsp; <title>demo</title>&nbsp; <style type="text/css">&nbsp; &nbsp; body {&nbsp; &nbsp; &nbsp; margin: 0;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .pushmenu {&nbsp; &nbsp; &nbsp; background: #444;&nbsp; &nbsp; &nbsp; text-align: center;&nbsp; &nbsp; &nbsp; font-family: Tahoma, Geneva, sans-serif;&nbsp; &nbsp; &nbsp; width: 300px;&nbsp; &nbsp; &nbsp; height: 100%;&nbsp; &nbsp; &nbsp; top: 0;&nbsp; &nbsp; &nbsp; z-index: 1000;&nbsp; &nbsp; &nbsp; position: fixed;&nbsp; &nbsp; &nbsp; overflow: hidden;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .main {&nbsp; &nbsp; &nbsp; width: 100%;&nbsp; &nbsp; &nbsp; height: 100%;&nbsp; &nbsp; &nbsp; overflow: auto;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .pushmenu h3 {&nbsp; &nbsp; &nbsp; color: #f1f1f1;&nbsp; &nbsp; &nbsp; font-size: 1.3em;&nbsp; &nbsp; &nbsp; font-weight: 400;&nbsp; &nbsp; &nbsp; padding: 15px 25px;&nbsp; &nbsp; &nbsp; margin: 0;&nbsp; &nbsp; &nbsp; background: #333;&nbsp; &nbsp; &nbsp; height: 16px;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .links {&nbsp; &nbsp; &nbsp; list-style-type: none;&nbsp; &nbsp; &nbsp; padding: 0;&nbsp; &nbsp; &nbsp; margin: 0 0 0 25%;&nbsp; &nbsp; &nbsp; width: 50%;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .links li {&nbsp; &nbsp; &nbsp; margin-top: 30px;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .links li a {&nbsp; &nbsp; &nbsp; position: relative;&nbsp; &nbsp; &nbsp; display: block;&nbsp; &nbsp; &nbsp; color: #f1f1f1;&nbsp; &nbsp; &nbsp; font-size: 1.3em;&nbsp; &nbsp; &nbsp; font-weight: 400;&nbsp; &nbsp; &nbsp; text-decoration: none;&nbsp; &nbsp; &nbsp; padding: 14px;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .links li a:after {&nbsp; &nbsp; &nbsp; content: '';&nbsp; &nbsp; &nbsp; display: block;&nbsp; &nbsp; &nbsp; position: absolute;&nbsp; &nbsp; &nbsp; left: 20px;&nbsp; &nbsp; &nbsp; bottom: -5px;&nbsp; &nbsp; &nbsp; width: 0;&nbsp; &nbsp; &nbsp; height: 4px;&nbsp; &nbsp; &nbsp; background-color: #f1f1f1;&nbsp; &nbsp; &nbsp; -webkit-transition: width 0.3s ease;&nbsp; &nbsp; &nbsp; -moz-transition: width 0.3s ease;&nbsp; &nbsp; &nbsp; transition: width 0.3s ease;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .links li a:hover:after {&nbsp; &nbsp; &nbsp; width: 70%;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .links li a:active {&nbsp; &nbsp; &nbsp; color: #dbdbdb;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .pushmenu-left {&nbsp; &nbsp; &nbsp; left: -150px;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .pushmenu-left.pushmenu-open {&nbsp; &nbsp; &nbsp; left: 0;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .pushmenu-push {&nbsp; &nbsp; &nbsp; overflow-x: hidden;&nbsp; &nbsp; &nbsp; position: relative;&nbsp; &nbsp; &nbsp; left: 0;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .pushmenu-push-toright {&nbsp; &nbsp; &nbsp; left: 300px;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .pushmenu,&nbsp; &nbsp; .pushmenu-push {&nbsp; &nbsp; &nbsp; -webkit-transition: all 0.5s ease;&nbsp; &nbsp; &nbsp; -moz-transition: all 0.5s ease;&nbsp; &nbsp; &nbsp; transition: all 0.5s ease;&nbsp; &nbsp; }&nbsp; </style></head><body class="pushmenu-push">&nbsp; <nav class="pushmenu pushmenu-left main">&nbsp; &nbsp; <ul class="links">&nbsp; &nbsp; &nbsp; <li><a href="index">Home</a></li>&nbsp; &nbsp; &nbsp; <li><a href="#">About Us</a></li>&nbsp; &nbsp; &nbsp; <li><a href="#">Missions</a></li>&nbsp; &nbsp; &nbsp; <li><a href="#">Partners</a></li>&nbsp; &nbsp; &nbsp; <li><a href="#">Events</a></li>&nbsp; &nbsp; &nbsp; <li><a href="#">Contact</a></li>&nbsp; &nbsp; &nbsp; <li><a href="#">Give</a></li>&nbsp; &nbsp; &nbsp; <li><a href="index">Home</a></li>&nbsp; &nbsp; &nbsp; <li><a href="#">About Us</a></li>&nbsp; &nbsp; &nbsp; <li><a href="#">Missions</a></li>&nbsp; &nbsp; &nbsp; <li><a href="#">Partners</a></li>&nbsp; &nbsp; &nbsp; <li><a href="#">Events</a></li>&nbsp; &nbsp; &nbsp; <li><a href="#">Contact</a></li>&nbsp; &nbsp; &nbsp; <li><a href="#">Give</a></li>&nbsp; &nbsp; </ul>&nbsp; </nav>&nbsp; <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>&nbsp; <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>&nbsp; <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>&nbsp; <script type="text/javascript">&nbsp; &nbsp; $(document).ready(function() {&nbsp; &nbsp; &nbsp; $menuLeft = $('.pushmenu-left');&nbsp; &nbsp; &nbsp; $nav_list = $('#nav_list');&nbsp; &nbsp; &nbsp; $nav_list.click(function() {&nbsp; &nbsp; &nbsp; &nbsp; $(this).toggleClass('active');&nbsp; &nbsp; &nbsp; &nbsp; $('.pushmenu-push').toggleClass('pushmenu-push-toright');&nbsp; &nbsp; &nbsp; &nbsp; $menuLeft.toggleClass('pushmenu-open');&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });&nbsp; </script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript