简体   繁体   English

JQuery 在 img click 上折叠 div

[英]JQuery collapse div on img click

I have a two part question concerning CSS styling and JQuery functionality.我有一个关于 CSS 样式和 JQuery 功能的两部分问题。

  1. (most important): In my code below, when the user clicks the round profile image, hypothetically the "profiledrop" div should appear. (最重要的):在我下面的代码中,当用户点击圆形头像时,假设应该出现“profiledrop”div。 If I replace the tag with plain text, the code works just fine.如果我用纯文本替换标签,代码就可以正常工作。 However, with an image instead of text as the link, the code no longer works.但是,使用图像而不是文本作为链接,代码不再有效。

  2. (less important): What is causing the "notification-tab" div to be so large? (不太重要):是什么导致“通知标签” div 如此之大? It ends up coming out to almost 100px for each div, which is massive.最终每个 div 的大小接近 100 像素,这是巨大的。 I want to at least half this size?我想要这个尺寸的至少一半? What part of the CSS code do I need to modify to accomplish this?我需要修改 CSS 代码的哪一部分来完成此操作?

I've been typing this code for the last 10 hours, so I'm basically braindead at this point.在过去的 10 个小时里,我一直在输入这段代码,所以此时我基本上已经脑残了。 I'm sure both answers are simple, but I'm just not seeing the solution.我确信这两个答案都很简单,但我只是没有看到解决方案。 Thank you in advance for your help!预先感谢您的帮助!

Codepin: https://codepen.io/dansbyt/pen/xxgayPa?editors=1010密码: https://codepen.io/dansbyt/pen/xxgayPa?editors=1010

HTML: HTML:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://mrdansby.com/private/style.css">

<div class="dropdown-container">
  <div class="profile"><a href="#" id='launch'><img src='https://mrdansby.com/resources/pics/1.png'></a></div>
  <ul class="profiledrop">
    <li class="notification-group nopic">
      <div class="notification-tab">
        <h4>Tasks</h4>
        <span class="label">1</span>
      </div>
      <ul class="notification-list">
        <li class="notification-list-item">
          <p class="message"><b>Mr. Teacher</b> is requesting you complete the assignment you need to do before the deadline on Monday.</p>
          <span class="date">2m ago</span>
        </li>
      </ul>
    </li>
    <li class="notification-group">
      <div class="notification-tab">
        <h4>Behavior</h4>
        <span class="label">4</span>
      </div>
      <ul class="notification-list">
        <li class="notification-list-item">
          <img src="https://mrdansby.com/resources/pics/4.png">
          <p class="message"><b>Student</b> was written up by Mr. Teacher.</p>
          <span class="date">5s ago</span>
        </li>
        <li class="notification-list-item">
          <img src="https://mrdansby.com/resources/pics/23.png">
          <p class="message"><b>Student</b> was written up by Mr. Teacher.</p>
          <span class="date">15m ago</span>
        </li>
        <li class="notification-list-item">
          <img src="https://mrdansby.com/resources/pics/1.png">
          <p class="message"><b>Student</b> was written up by Mr. Teacher.</p>
          <span class="date">5h ago</span>
        </li>
        <li class="notification-list-item">
          <img src="https://mrdansby.com/resources/pics/13.png">
          <p class="message"><b>Student</b> was written up by Mr. Teacher.</p>
          <span class="date">3d ago</span>
        </li>
      </ul>
    </li>
    <li class="notification-group">
      <div class="notification-tab">
        <h4>Homework</h4>
        <span class="label">3/3</span>
      </div>
      <ul class="notification-list">
        <li class="notification-list-item">
          <img src="https://mrdansby.com/resources/pics/1.png">
          <p class="message">Math homework was added by <b>Mr. Teacher</b>.</p>
          <span class="date">3d ago</span>
        </li>
        <li class="notification-list-item">
          <img src="https://mrdansby.com/resources/pics/1.png">
          <p class="message">Math homework was added by <b>Mr. Teacher</b>.</p>
          <span class="date">3d ago</span>
        </li>
        <li class="notification-list-item">
          <img src="https://mrdansby.com/resources/pics/1.png">
          <p class="message">Math homework was added by <b>Mr. Teacher</b>.</p>
          <span class="date">3d ago</span>
        </li>
      </ul>
    </li>
  </ul>
</div>

CSS: CSS:

/* Notification Infastructure */
.profiledrop {
    position: absolute;
    right: 15px; top: 65px;
    display: none;
    width: 350px; height: auto;
    max-height: 600px;
    padding: 0; margin: 0;
    overflow-y: hidden;
    background: #eee;
    border-top: 4px solid #5B7042;
    border-bottom-left-radius: 15px;
    border-bottom-right-radius: 15px;
    -webkit-box-shadow: 2px 2px 10px -5px rgba(0,0,0,0.75);
    box-shadow: 2px 2px 10px -5px rgba(0,0,0,0.75);}

.notification-group{
  border-bottom: 1px solid #e3e3e3;
  overflow: hidden;}

.notification-tab {
  width: 100%;
  display: inline-block;
  border-bottom: 1px solid #e3e3e3;}

.notification-list{
  height: 0px;
  max-height: 250px;
  padding: 0;
  overflow-y: auto;
  transition: height .5s;}

.notification-list-item{
  display: block;
  min-height: 60px;
  overflow: hidden !important;
  box-sizing: border-box !important;
  padding: 15px 15px 15px 10px;
  font-size: 16px;
  border-bottom: 1px solid #e3e3e3}
  .notification-list-item:nth-child(even) {background-color: #E3E3E3}

.notification-list-item img {
  clip-path: circle();
  float: left;
  margin-right: 10px;
  width: 60px; height: 60px;
  object-fit: cover}


/* Misc Settings */
.message::not(.nopic) {margin-top: 0px; margin-left: 80px}     /* Style for notification groups without image */


/* Notification text styling */
.label{
  float: right;
  padding: 0px 7px;
  margin-top: 20px;
  margin-right: 10px;
  border: 1px solid #5B7042;
  border-radius: 15px;}
  h4 {margin-left: 10px}
  h4, .label{display: inline-block;}

.message {margin-top: 0px}
.date {float: right; color: darkgray}


/* Active Section */
.active .notification-list {height: 250px;}
.active .notification-tab, .notification-tab:hover {background-color: #5B7042}
.active .label, .notification-tab:hover .label {border: 1px solid white}
.notification-tab:hover {color: white}
.active .label, .active h4 {color: white}


/* Responsive design */
@media only screen and (max-width : 514px) {
  body {margin: 0px}
  .profiledrop{
    width: 100%;
    margin: 0px;
    left: 0;}
}



.profile{
  position: absolute;
  top: 0%; right: 15px;
  width: 40px;
  clip-path: circle();}
  .profile img{float:right; max-width: 100%; max-height: 100%; display: block;}

JQUERY: JQUERY:

// Tab collapser //
$('.notification-tab').click(function(e){
  if($(e.currentTarget).parent().hasClass('active')){
    $('.notification-group').removeClass('active');
  } else{
    $('.notification-group').removeClass('active');
    $(e.currentTarget).parent().toggleClass('active');
  }
});

// Click outside collapser //
$(document).on('click', function(e) {
  if (e.target.id != "launch") {
    if ($(e.target).closest(".profiledrop").length === 0) {
      $(".profiledrop").hide();
    }
  }
});

// Menu Launcher //
$("#launch").click(function() {
  $(".profiledrop").show();
});

'launch' should be on the img element, such as: 'launch' 应该在 img 元素上,例如:

<div class="profile">
    <a href="#">
        <img id='launch' src='https://mrdansby.com/resources/pics/1.png'>
    </a>
</div>

I'll answer your second question first.我先回答你的第二个问题。 The reason the notification tab is so large is that the.profiledrop class has a fixed width of 300px.通知选项卡这么大的原因是 .profiledrop class 的固定宽度为 300px。 Each notification group is inheriting the width of the parent, so those are also 300px.每个通知组都继承了父组的宽度,因此它们也是 300px。 Each notification tab has a width of 100%, so its width becomes 100% of the nearest parent, which is the notification group, which is 300px, so that also becomes 300px.每个通知标签的宽度都是 100%,所以它的宽度变成了最近的父级的 100%,也就是通知组,也就是 300px,所以也变成了 300px。

To summarize this point, either change the width: 100% on the notification tab, or change the width: 300px on the profiledrop.总结这一点,要么在通知选项卡上更改宽度:100%,要么在 profiledrop 上更改宽度:300px。 I can't recommend which to do because I don't know what you want it to look like.我不能推荐该做什么,因为我不知道你想要它看起来像什么。

The simplest solution to your first question is to employ display: none.第一个问题的最简单解决方案是使用 display: none。 Take a look at the code snippet I've provided below and let me know if this is the behavior you're looking for.看看我在下面提供的代码片段,让我知道这是否是您正在寻找的行为。

 const image = document.querySelector("#myimage"); const paragraph = document.querySelector("p"); // I attach an event listener to the image to wait for a click event image.addEventListener("click", function() { if (paragraph.style.display === 'none') { // If the paragraph is currently hidden, I show it paragraph.style.display = 'block'; } else { // If the paragraph is currently shown, I hide it paragraph.style.display = 'none'; } })
 <img id="myimage" src="https://via.placeholder.com/350x150"> <p>Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. Click on the image. </p>

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

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