javascript 格网内分段叠加

b5buobof  于 2023-05-12  发布在  Java
关注(0)|答案(1)|浏览(86)

我正在构建一个使用网格的日历应用程序,我目前遇到了一个问题,日历项目都堆叠在一起,而不是在彼此之下。你们能帮我吗

现在的样子

应该的样子

$('.task').mouseover(function () {
   $(this).children('.task-detail').attr('hidden', false)
}).mouseleave(function () {
   $(this).children('.task-detail').attr('hidden', true)
})
.calendar {
    display: grid;
    width: 100%;
    grid-template-columns: repeat(7, minmax(120px, 1fr));
    grid-template-rows: 50px;
    grid-auto-rows: 150px;
    overflow: auto;
}

.calendar-header {
    color: #99a1a7;
    font-size: 25px;
    text-align: center;
}

.calender-navigation-item {
    font-size: 30px;
}

a.calender-navigation-item:hover {
    transform: scale(1.5);
}

.calendar-container {
    width: 85%;
    margin: auto;
    overflow: hidden;
    border-radius: 10px;
}

.calender-navigation {
    display: flex;
    justify-content: space-between;
    font-size: 20px;
    color: #99a1a7;
    line-height: 50px;
    font-weight: 500;
}

.calendar-language {
    width: 250px;
    padding-right: 10px;
    padding-top: 10px;
    margin-left: auto;
    margin-right: 0;
}

.day {
    border-bottom: 1px solid rgba(166, 168, 179, 0.12);
    border-right: 1px solid rgba(166, 168, 179, 0.12);
    text-align: right;
    padding: 14px 20px;
    letter-spacing: 1px;
    font-size: 12px;
    box-sizing: border-box;
    color: #98a0a6;
    position: relative;
    pointer-events: none;
}

.day:nth-of-type(7n + 7) {
    border-right: 0;
}

.day:nth-of-type(n + 1):nth-of-type(-n + 7) {
    grid-row: 2;
}

.day:nth-of-type(n + 8):nth-of-type(-n + 14) {
    grid-row: 3;
}

.day:nth-of-type(n + 15):nth-of-type(-n + 21) {
    grid-row: 4;
}

.day:nth-of-type(n + 22):nth-of-type(-n + 28) {
    grid-row: 5;
}

.day:nth-of-type(n + 29):nth-of-type(-n + 35) {
    grid-row: 6;
}

.day:nth-of-type(7n + 1) {
    grid-column: 1/1;
}

.day:nth-of-type(7n + 2) {
    grid-column: 2/2;
}

.day:nth-of-type(7n + 3) {
    grid-column: 3/3;
}

.day:nth-of-type(7n + 4) {
    grid-column: 4/4;
}

.day:nth-of-type(7n + 5) {
    grid-column: 5/5;
}

.day:nth-of-type(7n + 6) {
    grid-column: 6/6;
}

.day:nth-of-type(7n + 7) {
    grid-column: 7/7;
}

.day-name {
    font-size: 12px;
    text-transform: uppercase;
    color: #99a1a7;
    text-align: center;
    border-bottom: 1px solid rgba(166, 168, 179, 0.12);
    line-height: 50px;
    font-weight: 500;
}

.day-disabled {
    color: rgba(152, 160, 166, 0.6);
    background-color: #ffffff;
    background-image: url('../day_disabled.svg');
    cursor: not-allowed;
}

.task {
    margin: 15px;
    font-size: 14px;
    position: relative;
    background: black;
    border-radius: 4px;
    color: #fff;
    height: 10px;
}

.task.task-start {
    border-left-style: solid;
}

.task-detail {
    position: absolute;
    left: 0;
    top: calc(100% + 10px);
    background: #fff;
    border: 1px solid rgba(166, 168, 179, 0.2);
    color: #000;
    padding: 20px;
    box-sizing: border-box;
    border-radius: 4px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
    z-index: 999;
}

.task-detail:after, .task-detail:before {
    bottom: 100%;
    left: 30%;
    border: solid transparent;
    content: " ";
    height: 0;
    width: 0;
    position: absolute;
    pointer-events: none;
}

.task-detail:before {
    border-bottom-color: rgba(166, 168, 179, 0.2);
    border-width: 8px;
    margin-left: -8px;
}

.task-detail:after {
    border-bottom-color: #fff;
    border-width: 6px;
    margin-left: -6px;.calendar {
    display: grid;
    width: 100%;
    grid-template-columns: repeat(7, minmax(120px, 1fr));
    grid-template-rows: 50px;
    grid-auto-rows: 150px;
    overflow: auto;
}

.calendar-header {
    color: #99a1a7;
    font-size: 25px;
    text-align: center;
}

.calender-navigation-item {
    font-size: 30px;
}

a.calender-navigation-item:hover {
    transform: scale(1.5);
}

.calendar-container {
    width: 85%;
    margin: auto;
    overflow: hidden;
    border-radius: 10px;
}

.calender-navigation {
    display: flex;
    justify-content: space-between;
    font-size: 20px;
    color: #99a1a7;
    line-height: 50px;
    font-weight: 500;
}

.calendar-language {
    width: 250px;
    padding-right: 10px;
    padding-top: 10px;
    margin-left: auto;
    margin-right: 0;
}

.day {
    border-bottom: 1px solid rgba(166, 168, 179, 0.12);
    border-right: 1px solid rgba(166, 168, 179, 0.12);
    text-align: right;
    padding: 14px 20px;
    letter-spacing: 1px;
    font-size: 12px;
    box-sizing: border-box;
    color: #98a0a6;
    position: relative;
    pointer-events: none;
}

.day:nth-of-type(7n + 7) {
    border-right: 0;
}

.day:nth-of-type(n + 1):nth-of-type(-n + 7) {
    grid-row: 2;
}

.day:nth-of-type(n + 8):nth-of-type(-n + 14) {
    grid-row: 3;
}

.day:nth-of-type(n + 15):nth-of-type(-n + 21) {
    grid-row: 4;
}

.day:nth-of-type(n + 22):nth-of-type(-n + 28) {
    grid-row: 5;
}

.day:nth-of-type(n + 29):nth-of-type(-n + 35) {
    grid-row: 6;
}

.day:nth-of-type(7n + 1) {
    grid-column: 1/1;
}

.day:nth-of-type(7n + 2) {
    grid-column: 2/2;
}

.day:nth-of-type(7n + 3) {
    grid-column: 3/3;
}

.day:nth-of-type(7n + 4) {
    grid-column: 4/4;
}

.day:nth-of-type(7n + 5) {
    grid-column: 5/5;
}

.day:nth-of-type(7n + 6) {
    grid-column: 6/6;
}

.day:nth-of-type(7n + 7) {
    grid-column: 7/7;
}

.day-name {
    font-size: 12px;
    text-transform: uppercase;
    color: #99a1a7;
    text-align: center;
    border-bottom: 1px solid rgba(166, 168, 179, 0.12);
    line-height: 50px;
    font-weight: 500;
}

.day-disabled {
    color: rgba(152, 160, 166, 0.6);
    background-color: #ffffff;
    background-image: url('../day_disabled.svg');
    cursor: not-allowed;
}

.task {
    margin: 15px;
    font-size: 14px;
    position: relative;
    background: black;
    border-radius: 4px;
    color: #fff;
    height: 10px;
}

.task.task-start {
    border-left-style: solid;
}

.task-detail {
    position: absolute;
    left: 0;
    top: calc(100% + 10px);
    background: #fff;
    border: 1px solid rgba(166, 168, 179, 0.2);
    color: #000;
    padding: 20px;
    box-sizing: border-box;
    border-radius: 4px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
    z-index: 999;
}

.task-detail:after, .task-detail:before {
    bottom: 100%;
    left: 30%;
    border: solid transparent;
    content: " ";
    height: 0;
    width: 0;
    position: absolute;
    pointer-events: none;
}

.task-detail:before {
    border-bottom-color: rgba(166, 168, 179, 0.2);
    border-width: 8px;
    margin-left: -8px;
}

.task-detail:after {
    border-bottom-color: #fff;
    border-width: 6px;
    margin-left: -6px;
}

.task-detail h2 {
    font-size: 15px;
    margin: 0;
    color: #51565d;
}

.task-detail p {
    margin-top: 4px;
    font-size: 12px;
    margin-bottom: 0;
    font-weight: 500;
    color: rgba(81, 86, 93, .7);
}
}

.task-detail h2 {
    font-size: 15px;
    margin: 0;
    color: #51565d;
}

.task-detail p {
    margin-top: 4px;
    font-size: 12px;
    margin-bottom: 0;
    font-weight: 500;
    color: rgba(81, 86, 93, .7);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="calendar">
   <span class="day-name">Sunday</span> 
   <span class="day-name">Monday</span> 
   <span class="day-name">Tuesday</span> 
   <span class="day-name">Wednesday</span> 
   <span class="day-name">Thursday</span> 
   <span class="day-name">Friday</span> 
   <span class="day-name">Saturday</span> 
   <div class="day day-disabled">30</div>
   <div class="day">1</div>
   <div class="day">2</div>
   <div class="day">3</div>
   <div class="day">4</div>
   <div class="day">5</div>
   <div class="day">6</div>
   <div class="day">7</div>
   <div class="day">8</div>
   <div class="day">9</div>
   <div class="day">10</div>
   <div class="day">11</div>
   <div class="day">12</div>
   <div class="day">13</div>
   <div class="day">14</div>
   <div class="day">15</div>
   <div class="day">16</div>
   <div class="day">17</div>
   <div class="day">18</div>
   <div class="day">19</div>
   <div class="day">20</div>
   <div class="day">21</div>
   <div class="day">22</div>
   <div class="day">23</div>
   <div class="day">24</div>
   <div class="day">25</div>
   <div class="day">26</div>
   <div class="day">27</div>
   <div class="day">28</div>
   <div class="day">29</div>
   <div class="day">30</div>
   <div class="day">31</div>
   <div class="day day-disabled">1</div>
   <div class="day day-disabled">2</div>
   <div class="day day-disabled">3</div>
   <div class="task" style="grid-column: 1 / span 7; grid-row: 3;">
      <div hidden="" class="task-detail">
         <h2>foo033 - Type 1</h2>
         <p>Created by Foo </p>
      </div>
   </div>
   <div class="task" style="grid-column: 1 / span 7; grid-row: 3;">
      <div hidden="" class="task-detail">
         <h2>foo033 - Type 2</h2>
         <p>Created by Foo</p>
      </div>
   </div>
   <div class="task" style="grid-column: 1 / span 7; grid-row: 3;">
      <div hidden="" class="task-detail">
         <h2>foo033 - Type 3</h2>
         <p>Created by Foo</p>
      </div>
   </div>
   <div class="task" style="grid-column: 1 / span 7; grid-row: 3;">
      <div hidden="" class="task-detail">
         <h2>foo033 - Type 4</h2>
         <p>Created by Foo</p>
      </div>
   </div>
   <div class="task" style="grid-column: 1 / span 7; grid-row: 3;">
      <div hidden="" class="task-detail">
         <h2>foo033 - Type 5</h2>
         <p>Created by Foo</p>
      </div>
   </div>
   <div class="task" style="grid-column: 1 / span 7; grid-row: 3;">
      <div hidden="" class="task-detail">
         <h2>foo033 - Type 6</h2>
         <p>Created by Foo</p>
      </div>
   </div>
   <div class="task" style="grid-column: 1 / span 7; grid-row: 3;">
      <div hidden="" class="task-detail">
         <h2>foo033 - Type 7</h2>
         <p>Created by Foo</p>
      </div>
   </div>
   <div class="task" style="grid-column: 1 / span 7; grid-row: 3;">
      <div hidden="" class="task-detail">
         <h2>foo033 - Type 8</h2>
         <p>Created by Foo</p>
      </div>
   </div>
   <div class="task" style="grid-column: 1 / span 7; grid-row: 3;">
      <div hidden="" class="task-detail">
         <h2>foo033 - Type 9</h2>
         <p>Created by Foo</p>
      </div>
   </div>
   <div class="task" style="grid-column: 1 / span 7; grid-row: 3;">
      <div hidden="" class="task-detail">
         <h2>foo033 - Type 10</h2>
         <p>Created by Foo</p>
      </div>
   </div>
</div>

我试过使用一些填充和空白,但这并没有改变什么

vkc1a9a2

vkc1a9a21#

您需要将.task元素 Package 在容器中,并使用grid-column: 1 / span 7; grid-row: 3;定位该元素。问题源于显式地将元素放在网格中的同一行/列中。网格将堆叠元素(z索引)。它们不是彼此相对放置的,而是它们所放置的网格列/行。
这将从.task元素中删除所有网格定位,并将它们 Package 在具有网格定位的.task-list元素中。

$('.task').mouseover(function() {
  $(this).children('.task-detail').attr('hidden', false)
}).mouseleave(function() {
  $(this).children('.task-detail').attr('hidden', true)
})
.calendar {
  display: grid;
  width: 100%;
  grid-template-columns: repeat(7, minmax(120px, 1fr));
  grid-template-rows: 50px;
  grid-auto-rows: 150px;
  overflow: auto;
}

.calendar-header {
  color: #99a1a7;
  font-size: 25px;
  text-align: center;
}

.calender-navigation-item {
  font-size: 30px;
}

a.calender-navigation-item:hover {
  transform: scale(1.5);
}

.calendar-container {
  width: 85%;
  margin: auto;
  overflow: hidden;
  border-radius: 10px;
}

.calender-navigation {
  display: flex;
  justify-content: space-between;
  font-size: 20px;
  color: #99a1a7;
  line-height: 50px;
  font-weight: 500;
}

.calendar-language {
  width: 250px;
  padding-right: 10px;
  padding-top: 10px;
  margin-left: auto;
  margin-right: 0;
}

.day {
  border-bottom: 1px solid rgba(166, 168, 179, 0.12);
  border-right: 1px solid rgba(166, 168, 179, 0.12);
  text-align: right;
  padding: 14px 20px;
  letter-spacing: 1px;
  font-size: 12px;
  box-sizing: border-box;
  color: #98a0a6;
  position: relative;
  pointer-events: none;
}

.day:nth-of-type(7n + 7) {
  border-right: 0;
}

.day:nth-of-type(n + 1):nth-of-type(-n + 7) {
  grid-row: 2;
}

.day:nth-of-type(n + 8):nth-of-type(-n + 14) {
  grid-row: 3;
}

.day:nth-of-type(n + 15):nth-of-type(-n + 21) {
  grid-row: 4;
}

.day:nth-of-type(n + 22):nth-of-type(-n + 28) {
  grid-row: 5;
}

.day:nth-of-type(n + 29):nth-of-type(-n + 35) {
  grid-row: 6;
}

.day:nth-of-type(7n + 1) {
  grid-column: 1/1;
}

.day:nth-of-type(7n + 2) {
  grid-column: 2/2;
}

.day:nth-of-type(7n + 3) {
  grid-column: 3/3;
}

.day:nth-of-type(7n + 4) {
  grid-column: 4/4;
}

.day:nth-of-type(7n + 5) {
  grid-column: 5/5;
}

.day:nth-of-type(7n + 6) {
  grid-column: 6/6;
}

.day:nth-of-type(7n + 7) {
  grid-column: 7/7;
}

.day-name {
  font-size: 12px;
  text-transform: uppercase;
  color: #99a1a7;
  text-align: center;
  border-bottom: 1px solid rgba(166, 168, 179, 0.12);
  line-height: 50px;
  font-weight: 500;
}

.day-disabled {
  color: rgba(152, 160, 166, 0.6);
  background-color: #ffffff;
  background-image: url('../day_disabled.svg');
  cursor: not-allowed;
}

.task {
  margin: 15px;
  font-size: 14px;
  position: relative;
  background: black;
  border-radius: 4px;
  color: #fff;
  height: 10px;
}

.task.task-start {
  border-left-style: solid;
}

.task-detail {
  position: absolute;
  left: 0;
  top: calc(100% + 10px);
  background: #fff;
  border: 1px solid rgba(166, 168, 179, 0.2);
  color: #000;
  padding: 20px;
  box-sizing: border-box;
  border-radius: 4px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
  z-index: 999;
}

.task-detail:after,
.task-detail:before {
  bottom: 100%;
  left: 30%;
  border: solid transparent;
  content: " ";
  height: 0;
  width: 0;
  position: absolute;
  pointer-events: none;
}

.task-detail:before {
  border-bottom-color: rgba(166, 168, 179, 0.2);
  border-width: 8px;
  margin-left: -8px;
}

.task-detail:after {
  border-bottom-color: #fff;
  border-width: 6px;
  margin-left: -6px;
}

.calendar {
  display: grid;
  width: 100%;
  grid-template-columns: repeat(7, minmax(120px, 1fr));
  grid-template-rows: 50px;
  grid-auto-rows: 150px;
  overflow: auto;
}

.calendar-header {
  color: #99a1a7;
  font-size: 25px;
  text-align: center;
}

.calender-navigation-item {
  font-size: 30px;
}

a.calender-navigation-item:hover {
  transform: scale(1.5);
}

.calendar-container {
  width: 85%;
  margin: auto;
  overflow: hidden;
  border-radius: 10px;
}

.calender-navigation {
  display: flex;
  justify-content: space-between;
  font-size: 20px;
  color: #99a1a7;
  line-height: 50px;
  font-weight: 500;
}

.calendar-language {
  width: 250px;
  padding-right: 10px;
  padding-top: 10px;
  margin-left: auto;
  margin-right: 0;
}

.day {
  border-bottom: 1px solid rgba(166, 168, 179, 0.12);
  border-right: 1px solid rgba(166, 168, 179, 0.12);
  text-align: right;
  padding: 14px 20px;
  letter-spacing: 1px;
  font-size: 12px;
  box-sizing: border-box;
  color: #98a0a6;
  position: relative;
  pointer-events: none;
}

.day:nth-of-type(7n+7) {
  border-right: 0;
}

.day:nth-of-type(n+1):nth-of-type(-n+7) {
  grid-row: 2;
}

.day:nth-of-type(n+8):nth-of-type(-n+14) {
  grid-row: 3;
}

.day:nth-of-type(n+15):nth-of-type(-n+21) {
  grid-row: 4;
}

.day:nth-of-type(n+22):nth-of-type(-n+28) {
  grid-row: 5;
}

.day:nth-of-type(n+29):nth-of-type(-n+35) {
  grid-row: 6;
}

.day:nth-of-type(7n+1) {
  grid-column: 1/1;
}

.day:nth-of-type(7n+2) {
  grid-column: 2/2;
}

.day:nth-of-type(7n+3) {
  grid-column: 3/3;
}

.day:nth-of-type(7n+4) {
  grid-column: 4/4;
}

.day:nth-of-type(7n+5) {
  grid-column: 5/5;
}

.day:nth-of-type(7n+6) {
  grid-column: 6/6;
}

.day:nth-of-type(7n+7) {
  grid-column: 7/7;
}

.day-name {
  font-size: 12px;
  text-transform: uppercase;
  color: #99a1a7;
  text-align: center;
  border-bottom: 1px solid rgba(166, 168, 179, 0.12);
  line-height: 50px;
  font-weight: 500;
}

.day-disabled {
  color: rgba(152, 160, 166, 0.6);
  background-color: #ffffff;
  background-image: url('../day_disabled.svg');
  cursor: not-allowed;
}

.task-list {
  grid-column: 1 / span 7;
  grid-row: 3;
}

.task {
  margin: 15px;
  font-size: 14px;
  position: relative;
  background: black;
  border-radius: 4px;
  color: #fff;
  height: 10px;
}

.task.task-start {
  border-left-style: solid;
}

.task-detail {
  position: absolute;
  left: 0;
  top: calc(100% + 10px);
  background: #fff;
  border: 1px solid rgba(166, 168, 179, 0.2);
  color: #000;
  padding: 20px;
  box-sizing: border-box;
  border-radius: 4px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
  z-index: 999;
}

.task-detail:after,
.task-detail:before {
  bottom: 100%;
  left: 30%;
  border: solid transparent;
  content: " ";
  height: 0;
  width: 0;
  position: absolute;
  pointer-events: none;
}

.task-detail:before {
  border-bottom-color: rgba(166, 168, 179, 0.2);
  border-width: 8px;
  margin-left: -8px;
}

.task-detail:after {
  border-bottom-color: #fff;
  border-width: 6px;
  margin-left: -6px;
}

.task-detail h2 {
  font-size: 15px;
  margin: 0;
  color: #51565d;
}

.task-detail p {
  margin-top: 4px;
  font-size: 12px;
  margin-bottom: 0;
  font-weight: 500;
  color: rgba(81, 86, 93, .7);
}

}
.task-detail h2 {
  font-size: 15px;
  margin: 0;
  color: #51565d;
}
.task-detail p {
  margin-top: 4px;
  font-size: 12px;
  margin-bottom: 0;
  font-weight: 500;
  color: rgba(81, 86, 93, .7);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="calendar">
  <span class="day-name">Sunday</span>
  <span class="day-name">Monday</span>
  <span class="day-name">Tuesday</span>
  <span class="day-name">Wednesday</span>
  <span class="day-name">Thursday</span>
  <span class="day-name">Friday</span>
  <span class="day-name">Saturday</span>
  <div class="day day-disabled">30</div>
  <div class="day">1</div>
  <div class="day">2</div>
  <div class="day">3</div>
  <div class="day">4</div>
  <div class="day">5</div>
  <div class="day">6</div>
  <div class="day">7</div>
  <div class="day">8</div>
  <div class="day">9</div>
  <div class="day">10</div>
  <div class="day">11</div>
  <div class="day">12</div>
  <div class="day">13</div>
  <div class="day">14</div>
  <div class="day">15</div>
  <div class="day">16</div>
  <div class="day">17</div>
  <div class="day">18</div>
  <div class="day">19</div>
  <div class="day">20</div>
  <div class="day">21</div>
  <div class="day">22</div>
  <div class="day">23</div>
  <div class="day">24</div>
  <div class="day">25</div>
  <div class="day">26</div>
  <div class="day">27</div>
  <div class="day">28</div>
  <div class="day">29</div>
  <div class="day">30</div>
  <div class="day">31</div>
  <div class="day day-disabled">1</div>
  <div class="day day-disabled">2</div>
  <div class="day day-disabled">3</div>
  <div class="task-list">
    <div class="task" style="">
      <div hidden="" class="task-detail">
        <h2>foo033 - Type 1</h2>
        <p>Created by Foo </p>
      </div>
    </div>
    <div class="task">
      <div hidden="" class="task-detail">
        <h2>foo033 - Type 2</h2>
        <p>Created by Foo</p>
      </div>
    </div>
    <div class="task">
      <div hidden="" class="task-detail">
        <h2>foo033 - Type 3</h2>
        <p>Created by Foo</p>
      </div>
    </div>
    <div class="task">
      <div hidden="" class="task-detail">
        <h2>foo033 - Type 4</h2>
        <p>Created by Foo</p>
      </div>
    </div>
    <div class="task">
      <div hidden="" class="task-detail">
        <h2>foo033 - Type 5</h2>
        <p>Created by Foo</p>
      </div>
    </div>
    <div class="task">
      <div hidden="" class="task-detail">
        <h2>foo033 - Type 6</h2>
        <p>Created by Foo</p>
      </div>
    </div>
    <div class="task">
      <div hidden="" class="task-detail">
        <h2>foo033 - Type 7</h2>
        <p>Created by Foo</p>
      </div>
    </div>
    <div class="task">
      <div hidden="" class="task-detail">
        <h2>foo033 - Type 8</h2>
        <p>Created by Foo</p>
      </div>
    </div>
    <div class="task">
      <div hidden="" class="task-detail">
        <h2>foo033 - Type 9</h2>
        <p>Created by Foo</p>
      </div>
    </div>
    <div class="task">
      <div hidden="" class="task-detail">
        <h2>foo033 - Type 10</h2>
        <p>Created by Foo</p>
      </div>
    </div>
  </div>
</div>

相关问题