首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JQUERY $(document).click vs $(document).ready

JQUERY $(document).click vs $(document).ready
EN

Stack Overflow用户
提问于 2019-02-18 10:04:20
回答 1查看 92关注 0票数 0

代码语言:javascript
运行
复制
$(document).ready(function(e) {
  if ($('#sidebar').hasClass('active')) {
    $('#dismiss, .overlay').on('click', function() {
      $('#sidebar').removeClass('active');
      $('.overlay').removeClass('active');
    });
  } else {
    $('#sidebarCollapse').on('click', function() {
      $('#sidebar').addClass('active');
      $('.overlay').addClass('active');
      $('.collapse.in').toggleClass('in');
      $('a[aria-expanded=true]').attr('aria-expanded', 'false');
    });
  }
});
代码语言:javascript
运行
复制
.fixed-top {
  z-index: 1 !important;
}

#sidebar {
  width: 250px;
  position: fixed;
  top: 0;
  left: -250px;
  height: 100vh;
  z-index: 999;
  background: #7386D5;
  color: #fff;
  transition: all 0.3s;
  overflow-y: hidden;
  box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.2);
}

#sidebar.active {
  left: 0;
}

#dismiss {
  width: 35px;
  height: 35px;
  line-height: 35px;
  text-align: center;
  background: #7386D5;
  position: absolute;
  top: 10px;
  right: 10px;
  cursor: pointer;
  -webkit-transition: all 0.3s;
  -o-transition: all 0.3s;
  transition: all 0.3s;
}

#dismiss:hover {
  background: #fff;
  color: #7386D5;
}

.overlay {
  display: none;
  position: fixed;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.7);
  z-index: 998;
  opacity: 0;
  transition: all 0.5s ease-in-out;
}

.overlay.active {
  display: block;
  opacity: 1;
}

#sidebar .sidebar-header {
  padding: 0px;
  background: #6d7fcc;
  text-align: center;
}

#sidebar ul.components {
  padding: 20px 0;
  border-bottom: 1px solid #47748b;
}

#sidebar ul p {
  color: #fff;
  padding: 10px;
}

#sidebar ul li a {
  padding: 10px;
  font-size: 1.1em;
  display: block;
}

#sidebar ul li a:hover {
  color: #7386D5;
  background: #fff;
}

#sidebar ul li.active>a,
a[aria-expanded="true"] {
  color: #fff;
  background: #6d7fcc;
}

a[data-toggle="collapse"] {
  position: relative;
}

.dropdown-toggle::after {
  display: block;
  position: absolute;
  top: 50%;
  right: 20px;
  transform: translateY(-50%);
}

ul ul a {
  font-size: 0.9em !important;
  padding-left: 30px !important;
  background: #6d7fcc;
}

ul ul ul a {
  font-size: 0.7em !important;
  padding-left: 40px !important;
  background: #6d7fcc;
}

ul.CTAs {
  padding: 20px;
}

ul.CTAs a {
  text-align: center;
  font-size: 0.9em !important;
  display: block;
  border-radius: 5px;
  margin-bottom: 5px;
}

a.download {
  background: #fff;
  color: #7386D5;
}

a.article,
a.article:hover {
  background: #6d7fcc !important;
  color: #fff !important;
}
代码语言:javascript
运行
复制
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />

<nav id="sidebar">
  <div class="sidebar-header">
    <div id="dismiss">
      <i class="fas fa-arrow-left"></i>
    </div>
  </div>
</nav>

<nav class="navbar navbar-expand-lg navbar-light fixed-top" style="background-color:#6d7fcc;">
  <div class="container-fluid">
    <button type="button" id="sidebarCollapse" class="btn btn-info">
      <i class="fas fa-align-left"></i>
      <span>Table Of Contents</span>
    </button>
  </div>
</nav>

<div class="overlay"></div>

有了这段$(document).ready(function (e){...});代码,我打开侧边栏的按钮就可以工作了,但是解职按钮不工作吗?

但是,如果将其更改为$(document).click(function (e) {...});,它可以工作,但我必须第一次按下按钮两次才能打开侧栏。为什么是这样?

另外,当点击侧边栏的时候,可以请求帮助关闭它吗?我有这个

代码语言:javascript
运行
复制
$(document).click(function(e) {
    if ($('#sidebar').('active') && !$(e.target).is('#sidebar')) {
        $('#sidebar').removeClass('active');
        $('.overlay').removeClass('active');
    }
});

代码,但这不起作用。

已经编辑了代码。我组织了引导文件的导入。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-18 11:22:13

在您的代码中,if被检查为ready状态。

您需要将隐藏侧边栏的代码移到if条件之外,这样才能工作。

您可以检查#sidebarclick函数中是否有活动类。

代码语言:javascript
运行
复制
$('#dismiss, .overlay').on('click', function() {
    if ($('#sidebar').hasClass('active')) {
        $('#sidebar').removeClass('active');
        $('.overlay').removeClass('active');
    }
});

$('#sidebarCollapse').on('click', function() {
    if (! $('#sidebar').hasClass('active')) {
        $('#sidebar').addClass('active');
        $('.overlay').addClass('active');
        $('.collapse.in').toggleClass('in');
        $('a[aria-expanded=true]').attr('aria-expanded', 'false');
    }
});

或者我们可以说,您需要绑定if条件才能单击事件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54744700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档