【问题标题】:Why is my waypoint added class not removing when scrolling down为什么向下滚动时我的航点添加类没有删除
【发布时间】:2018-04-17 17:41:28
【问题描述】:

我遇到的情况是,我有一个 #projMarker 的盒子,最初设置为 display: none。当它到达#subSolSec 时,它变为display:block。我正在使用 jQuery 的 Waypoints 函数来执行此操作。

这方面效果很好,但我不明白的是,当我退出 #subSolSec 时,projMarker 不会回到 display: none 它在退出航路点时向上滚动时会这样做,但不是向下退出时。

要添加/删除类,我使用的是toggleClass

有人看到我做错了吗?

$('#subSolSec').waypoint(function() {
  $('#projMarker').toggleClass('active');
}, {
  offset: '0'
});
.area {
  width: 100%;
  height: 750px;
}
#sec1 {
  background: blue;
}
#sec3 {
  background: red;
}
#projMarker {
	position: fixed;
	top: 30%;
	right: 10px;
	width: 150px;
	height: auto;
	border: 1px solid black;
	z-index: 2;
	display: none;
}
#projMarker.active {
	display: block;
}
.markerItem {
	display: block;
	margin-bottom: 15px;
	color: #4D4D4D;
	font-family: 'Nunito', sans-serif;
	letter-spacing: .1rem;
	font-size: .8rem;
	line-height: 1.5em;
}

#subSolSec {
	width: 100%;
	height: auto;
}
.secSlant {
	height: auto;
}
.slantTop {
	background: #FFF;
	z-index: 1;
	position: relative;
}
.slantTop:before {
    content: '';
    display: block;
    background: inherit;
    position: absolute;
    top: -6px;
    height: 500px;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: -1;
    -webkit-transform: skewY(10.5deg);
    -moz-transform: skewY(10.5deg);
    -ms-transform: skewY(10.5deg);
    -o-transform: skewY(10.5deg);
    transform: skewY(10.5deg);
    -webkit-transform-origin: 100%;
    -moz-transform-origin: 100%;
    -ms-transform-origin: 100%;
    -o-transform-origin: 100%;
    transform-origin: 100%;
    -webkit-box-shadow: 0 -8px 8px 0 rgba(0, 0, 0, 0.05), 0 -12px 28px 0 rgba(0, 0, 0, 0.05);
    -moz-box-shadow: 0 -8px 8px 0 rgba(0, 0, 0, 0.05), 0 -12px 28px 0 rgba(0, 0, 0, 0.05);
    box-shadow: 0 -8px 8px 0 rgba(0, 0, 0, 0.05), 0 -12px 28px 0 rgba(0, 0, 0, 0.05);
}
.slantTop-noshadow:before {
    -webkit-box-shadow: none;
    -moz-box-shadow: none;
    box-shadow: none;
}
.slantContainer {
	padding: 20px 0;
	margin: 0 10%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/waypoints/4.0.1/jquery.waypoints.min.js"></script>
<section class="area" id="sec1">

</section>
<section id="subSolSec">
  <div id="projMarker">
    <div class="markerItem">
      Section 1
    </div>
    <div class="markerItem">
      Section 2
    </div>
  </div>
  <div class="secSlant" id="secSlant1">
    <div class="slantTop">
      <div class="slantContainer">
        <div class="slantBlock1">
          <h2 class="hG2 slantTitle">1</h2>
          <p class="slantDesc">
            Area for 1
          </p>
        </div>
      </div>
    </div>
  </div>
  <div class="secSlant" id="secSlant2">
    <div class="slantTop">
      <div class="slantContainer">
        <div class="slantBlock1">
          <h2 class="hG2 slantTitle">2</h2>
          <p class="slantDesc">
            Area for 2
          </p>
        </div>
      </div>
    </div>
  </div>
</section>
<section class="area" id="sec3">

</section>

【问题讨论】:

    标签: javascript jquery css toggle jquery-waypoints


    【解决方案1】:

    您需要添加另一个航点以按照您想要的方式脱离航点。所以:

    $('#sec3').waypoint(function() {
      $('#projMarker').toggleClass('active');
    }, {
      offset: '0'
    });
    

    【讨论】:

    • 嗯,我刚刚在您更新的代码中尝试过,它似乎可以工作。将其添加到当前代码中,更新并在此处运行它,看看它是否适合您
    猜你喜欢
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多