如何使用js正确删除按钮

时间:2018-05-29 11:16:01

标签: javascript jquery

我阻止了8个容器(project_block)和光滑的滑块。另外在底部我有一个小按钮。

滑动滑块仅在990像素中触发。当它开始滑动时,我需要删除底部的按钮。但是如何删除他不会为空的按钮。?

当我使用方法btn.remove()按钮移除时,我会在我的滑块中停留,看看左侧的白色区域

请帮助

以下是Codepen代码段



$(window).on('load', function() {
  if ($(window).width() < 990) {
    $('.container-slick').slick({
      centerMode: true,
      infinite: true,
      prevArrow: false,
      nextArrow: false,
      speed: 200,
      slidesToShow: 2,
      slidesToScroll: 1,
      autoplay: true,
      responsive: [{
        breakpoint: 568,
        settings: {
          slidesToShow: 1
        }
      }]
    });
  }
});
$(window).on('resize', function() {
  if ($(window).width() < 990) {
    $('.container-slick').not('.slick-initialized').slick({
      centerMode: true,
      infinite: true,
      prevArrow: false,
      nextArrow: false,
      speed: 200,
      slidesToShow: 2,
      slidesToScroll: 1,
      responsive: [{
        breakpoint: 568,
        settings: {
          slidesToShow: 1
        }
      }]
    });
  } else {
    $(".container-slick.slick-initialized").slick("unslick");
  }
});
&#13;
.projects {
  position: relative;
  width: 100%;
}

.projects .projects_title {
  font-family: 'montserrat';
  position: relative;
  bottom: 10%;
  font-size: 26px;
  font-weight: bold;
  font-style: normal;
  font-stretch: normal;
  line-height: 2.69;
  letter-spacing: 1.3px;
  text-align: left;
  color: #2a323d;
  text-transform: uppercase;
}

.projects .projects_title span {
  color: #ffb338;
}

.no-padding {
  padding: 0 !important;
}

.projects_block {
  margin-bottom: 2%;
  display: flex;
  flex-wrap: wrap;
}

.projects_block .project_block {
  width: 23%;
  display: flex;
  flex-direction: column;
  height: 100px;
  transition: background-image, box-shadow 0.3s ease-in-out;
  background-image: none;
  margin: 0 1%;
  background-size: cover;
  background-position: 58% 30%;
  background-blend-mode: multiply;
}

@media only screen and (max-width: 768px) {
  .projects_block .project_block {
    width: none;
  }
}

.projects_block .project_block:first-child {
  margin-left: 0 !important;
}

.projects_block .project_block:last-child {
  margin-right: 0 !important;
}

.projects_block .project_block:hover {
  cursor: pointer;
  border-bottom-left-radius: 40px;
  background-blend-mode: multiply;
  border-top-right-radius: 40px;
  border: 2px solid #fff;
  background-repeat: no-repeat;
  box-shadow: 15px 16px 30px 0px rgba(0, 0, 0, 0.4);
  background-image: linear-gradient(to top, #151515, rgba(255, 255, 255, 0)), url(https://milan-holidays.com/wp-content/uploads/2017/07/milan_tournimage_16_1920x1015.jpg);
}

.projects_block .project_block:hover .project_title {
  color: #fff;
  padding: 20px 0 0 20px;
}

.projects_block .project_block:hover .project_name {
  color: #fff;
  padding-left: 20px;
}

.projects_block .project_block .project_title {
  font-size: 14px;
  font-weight: normal;
  font-style: normal;
  font-stretch: normal;
  line-height: normal;
  letter-spacing: 0.7px;
  text-align: left;
  color: #666666;
}

.projects_block .project_block .project_name {
  font-size: 14px;
  font-weight: bold;
  font-style: normal;
  font-stretch: normal;
  line-height: normal;
  letter-spacing: 0.7px;
  text-align: left;
  color: #2a323d;
}

.projects_block .project_block a {
  opacity: 0;
  transition: opacity .3s ease-in-out;
  font-family: 'montserrat';
}

.projects_block .project_block:hover a {
  opacity: 1;
  font-size: 12px;
  font-weight: bold;
  font-style: normal;
  font-stretch: normal;
  line-height: normal;
  letter-spacing: 0.6px;
  text-align: left;
  color: #ffb338;
  padding-left: 24px;
  padding-top: 6px;
  text-decoration: none;
}

.project_block:nth-child(1):hover {
  cursor: pointer;
  border-bottom-left-radius: 40px;
  background-blend-mode: multiply;
  border-top-right-radius: 40px;
  border: 2px solid #fff;
  background-repeat: no-repeat;
  box-shadow: 15px 16px 30px 0px rgba(0, 0, 0, 0.4);
  background-image: linear-gradient(to top, #151515, rgba(255, 255, 255, 0)), url(https://milan-holidays.com/wp-content/uploads/2017/07/milan_tournimage_16_1920x1015.jpg);
}

.project_block:nth-child(2):hover {
  cursor: pointer;
  border-bottom-left-radius: 40px;
  background-blend-mode: multiply;
  border-top-right-radius: 40px;
  border: 2px solid #fff;
  background-repeat: no-repeat;
  box-shadow: 15px 16px 30px 0px rgba(0, 0, 0, 0.4);
  background-image: linear-gradient(to top, #151515, rgba(255, 255, 255, 0)), url(https://milan-holidays.com/wp-content/uploads/2017/07/milan_tournimage_16_1920x1015.jpg);
  background-position: 78% 0%;
}

.project_block:nth-child(3):hover {
  cursor: pointer;
  border-bottom-left-radius: 40px;
  background-blend-mode: multiply;
  border-top-right-radius: 40px;
  border: 2px solid #fff;
  background-repeat: no-repeat;
  box-shadow: 15px 16px 30px 0px rgba(0, 0, 0, 0.4);
  background-image: linear-gradient(to top, #151515, rgba(255, 255, 255, 0)), url(https://milan-holidays.com/wp-content/uploads/2017/07/milan_tournimage_16_1920x1015.jpg);
  background-position: 78% 0%;
}

.project_block:nth-child(4):hover {
  cursor: pointer;
  border-bottom-left-radius: 40px;
  background-blend-mode: multiply;
  border-top-right-radius: 40px;
  border: 2px solid #fff;
  background-repeat: no-repeat;
  box-shadow: 15px 16px 30px 0px rgba(0, 0, 0, 0.4);
  background-image: linear-gradient(to top, #151515, rgba(255, 255, 255, 0)), url(https://milan-holidays.com/wp-content/uploads/2017/07/milan_tournimage_16_1920x1015.jpg);
}

.project_block:nth-child(5) {
  margin-left: 0 !important;
}

.project_block:nth-child(5):hover {
  cursor: pointer;
  border-bottom-left-radius: 40px;
  background-blend-mode: multiply;
  border-top-right-radius: 40px;
  border: 2px solid #fff;
  background-repeat: no-repeat;
  box-shadow: 15px 16px 30px 0px rgba(0, 0, 0, 0.4);
  background-image: linear-gradient(to top, #151515, rgba(255, 255, 255, 0)), url(https://milan-holidays.com/wp-content/uploads/2017/07/milan_tournimage_16_1920x1015.jpg);
}

.project_block:nth-child(6):hover {
  cursor: pointer;
  border-bottom-left-radius: 40px;
  background-blend-mode: multiply;
  border-top-right-radius: 40px;
  border: 2px solid #fff;
  background-repeat: no-repeat;
  box-shadow: 15px 16px 30px 0px rgba(0, 0, 0, 0.4);
  background-image: linear-gradient(to top, #151515, rgba(255, 255, 255, 0)), url(https://milan-holidays.com/wp-content/uploads/2017/07/milan_tournimage_16_1920x1015.jpg);
}

.project_block:nth-child(7):hover {
  cursor: pointer;
  border-bottom-left-radius: 40px;
  background-blend-mode: multiply;
  border-top-right-radius: 40px;
  border: 2px solid #fff;
  background-repeat: no-repeat;
  box-shadow: 15px 16px 30px 0px rgba(0, 0, 0, 0.4);
  background-image: linear-gradient(to top, #151515, rgba(255, 255, 255, 0)), url(https://milan-holidays.com/wp-content/uploads/2017/07/milan_tournimage_16_1920x1015.jpg);
  background-position: 50% 100%;
}

.project_block:nth-child(8):hover {
  cursor: pointer;
  border-bottom-left-radius: 40px;
  background-blend-mode: multiply;
  border-top-right-radius: 40px;
  border: 2px solid #fff;
  background-repeat: no-repeat;
  box-shadow: 15px 16px 30px 0px rgba(0, 0, 0, 0.4);
  background-image: linear-gradient(to top, #151515, rgba(255, 255, 255, 0)), url(https://milan-holidays.com/wp-content/uploads/2017/07/milan_tournimage_16_1920x1015.jpg);
  background-position: 17% 60%;
}

@media (max-width:768px) {
  .container-slick {
    max-width: 90% !important;
    margin: auto;
    border: 1px solid red;
  }
  .projects_block .slide {
    max-width: 95% !important;
    height: 100%;
    background-size: 100% 100% !important;
    margin: 0 -20px !important;
    outline: none;
  }
}

@media (max-width:555px) {
  .container-slick {
    max-width: 95% !important;
    margin: auto;
    border: 1px solid red;
  }
  .projects_block .slide {
    max-width: 95% !important;
    margin: 0 -30px !important;
    outline: none;
  }
}

.btn_container {
  margin-top: 100px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick.js"></script>
<div class="container-fluid">
  <div class="row">
    <div class="projects_block container-slick no-padding">
      <div class="project_block slide no-padding">
        <h6 class="project_title">Festival</h6>
        <div class="project_name">Discover Milan's religious sites</div>
        <a href="javascript:void(0)">View &rarr;</a>
      </div>
      <div class="project_block slide no-padding">
        <h6 class="project_title">Festival</h6>
        <div class="project_name">Saint Petersburg Philharmonic Orchestra</div>
        <a href="javascript:void(0)">View &rarr;</a>
      </div>
      <div class="project_block slide no-padding">
        <h6 class="project_title">Festival</h6>
        <div class="project_name">Grand Hall Music </div>
        <a href="javascript:void(0)">View &rarr;</a>
      </div>
      <div class="project_block slide no-padding">
        <h6 class="project_title">Festival</h6>
        <div class="project_name">Hand-made art & design workshop</div>
        <a href="javascript:void(0)">View &rarr;</a>
      </div>
      <div class="project_block slide no-padding">
        <h6 class="project_title">Festival</h6>
        <div class="project_name">Discover Milan's religious sites</div>
        <a href="javascript:void(0)">View &rarr;</a>
      </div>
      <div class="project_block slide no-padding">
        <h6 class="project_title">Festival</h6>
        <div class="project_name">Saint Petersburg Philharmonic Orchestra</div>
        <a href="javascript:void(0)">View &rarr;</a>
      </div>
      <div class="project_block slide no-padding">
        <h6 class="project_title">Festival</h6>
        <div class="project_name">Grand Hall Music </div>
        <a href="javascript:void(0)">View &rarr;</a>
      </div>
      <div class="project_block slide no-padding">
        <h6 class="project_title">Festival</h6>
        <div class="project_name">Hand-made art & design workshop</div>
        <a href="javascript:void(0)">View &rarr;</a>
      </div>
      <!--       this button -->
      <div class="btn_container">
        <button class="more_posts">
          <a href="javascript:void(0)">More</a>
        </button>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:-2)

1)考虑使用css media query将display: none设置为您要删除的按钮

2)考虑使用JS调整大小时将样式display: none添加到按钮(非常糟糕)

3)当你只需要隐藏它时,考虑不要用js删除按钮

4)在发布到SO之前考虑搜索网页

------- --------编辑

刚试过以下,它成功隐藏了btn

@media screen and (max-width: 990px){
  .btn_container {
    display: none !important;
  }
}