웨이 포인트 요소의 ID를 가져온 다음 스크롤이 해당 웨이 포인트에 도달 할 때 해당 ID 값을 클래스로 바디에 추가하려고합니다. 이것은 작동하지 않는 것 같습니다.
HTML
<body class="bg-1">
<div id="content">
<div class="cover">
<h2>Title</h2>
<p class="keep-scrolling">Keep scrolling along</p>
</div>
<section class="stats">
<article id="bg-1">
/* stuff */
</article>
<article id="bg-2">
/* stuff */
</article>
<article id="bg-3">
/* stuff */
</article>
<article id="bg-4">
/* stuff */
</article>
</section>
</div>
</body>
$(function() { $("article").waypoint(function(direction) { //callback when waypoint is reached, with direction of scroll as a parameter var $this = $(this); // caching the waypoint element if($this.attr("id") == "bg-1") { $("body").removeClass(); $("body").addClass('bg-1'); } else if($this.attr("id") == "bg-2") { $("body").removeClass(); $("body").addClass("bg-2"); } else if($this.attr("id") == "bg-3") { $("body").removeClass(); $("body").addClass("bg-3"); } else if($this.attr("id") == "bg-4") { $("body").removeClass(); $("body").addClass("bg-4"); } else { $("body").addClass("bg-1"); }; }); });
나는 ID를 잡기 위해 방법의 수를했습니다 자바 스크립트
하지만 구문 권리를 얻을 수 없습니다.
이 중대하다, 감사합니다! 그러나, 나는'.removeClass()'에 대해 [this answer] (http://stackoverflow.com/questions/1424981/how-to-remove-all-css-classes-using-jquery#1424991)을 읽을 필요가 없다. 매개 변수. 또한 웨이 포인트에 대한 첫 번째 인수로 핸들러 옵션을 전달할 수 있음을 [http://imakewebthings.com/waypoints/guides/jquery-zepto/]에서 배웠습니다. – mapk
@mapk 네, removeClass와 맞습니다. 나는 그것을 망쳤다. 그러나 handler-function의'$ (this)'에는 웨이 포인트가 포함되어 있지 않습니다. '이'는 실제 요소가 포함 된 웨이 포인트 객체입니다. – empiric
설명해 주셔서 감사합니다. – mapk