我有一個窗口滾動功能,我試圖撥入。最初,我試圖用航點,但無法弄清楚。滾動功能沒有在我想要的位置觸發
我的問題是我的功能是射擊太早,而不是在我想要它的位置。它在屏幕底部到達主容器時觸發,全部位於#home-info
。我遇到的問題是,如果有人滾動緩慢,他們永遠不會看到動畫。理想情況下,我希望函數在到達#info-container
時觸發,容器中帶有動畫對象。
我在做什麼錯誤,不允許這種情況發生,或者有沒有更好的方法來做到這一點?
function boxes() {
window.addEventListener("scroll", function(event) {
var top, green, yellow, red;
top = this.scrollY;
green = document.querySelector("#green"),
yellow = document.querySelector("#yellow"),
red = document.querySelector("#red");
if(top > 100){
green.classList.add("green", "active");
yellow.classList.add("yellow", "active");
red.classList.add("red", "active");
}
}, false);
}
setTimeout(boxes,1200);
<div id="home-info">
<div id="home-info-container">
<div id="home-info-container">
<div id="home-info-container-description">
<div id="home-info-container-title">THE <span class="yellow-color sansbold">LEADING</span> PROVIDER FOR<br> DEMOLITION & WRECKING</div>
<div id="home-info-description">The Eslich Wrecking Company has been recognized as a leader in the demolition field for over 60 years. Over that time span, we have gained both the experience and reputation for doing quality work in an expedient, safe and cost efficient manner. Our track record proves that Eslich Wrecking has the people, equipment and know-how to tackle even the most complex situations and the most demanding jobs. This includes the capability to bond any project when necessary and to carry general liability, auto, and pollution insurance up to 10 million.</div>
</div>
</div>
<section id="info">
<article id="info-container">
<a href="projects">
<div id="green" class="box">
<div class="info-box-title">PROJECT AFTER PROJECT</div>
<div class="info-box-description">Over 60 years of accumulated projects.</div>
</div>
</a>
<a href="about">
<div id="yellow" class="box">
<div class="info-box-title">YEARS OF DEMOLITION HISTORY</div>
<div class="info-box-description">Find out about - The Eslich Wrecking Company.</div>
</div>
</a>
<a href="contact">
<div id="red" class="box">
<div class="info-box-title">GET IN TOUCH WITH US</div>
<div class="info-box-description">Contact us for more information.</div>
</div>
</a>
</article>
</section>
</div>
每當'top> 100'時開始動畫。你的'#info-container'就是這樣。爲什麼100?和'setTimeout'有什麼關係? –
@DavidHedlund我試過使用不同的值比100,但實際上我希望它被設置爲容器,而不是一個像素。我添加了'setTimeout'來延遲函數,有點像一個bandaid,直到我修復這個問題。 – Becky