如何延迟Javascript函数,直到它位于网页中间?

问题描述 投票:0回答:2

你好,我的网页上有一个数字动画,我不希望动画开始直到它在网页中间。我尝试使用Google onscroll和其他选项,但无法正常运行。

我希望动画直到访客向下滚动到472px才开始播放。从现在开始,网页加载后,动画就会自动开始。任何帮助,我都会非常感激。

// 472 px  -  Starts Yellow Counter Section

const counters = document.querySelectorAll('.counter');
const speed = 200; // The lower the slower

counters.forEach(counter => {
	const updateCount = () => {
		const target = +counter.getAttribute('data-target');
		const count = +counter.innerText;

		// Lower inc to slow and higher to slow
		const inc = target / speed;

		// console.log(inc);
		// console.log(count);

		// Check if target is reached
		if (count < target) {
			// Add inc to count and output in counter
			counter.innerText = count + inc;
			// Call function every ms
			setTimeout(updateCount, 1);
		} else {
			counter.innerText = target;
		}
	};

	updateCount();
});
.bg-yellow-white {
    background: #f7c51e;
    color: white;
}


.container {
    max-width: 1404px;
    margin: auto;
    padding: 0 2rem;
    overflow: hidden;
}


.l-heading {
    font-weight: bold;
    font-size: 4rem;
    margin-bottom: 0.75rem;
    line-height: 1.1;
}





/* Padding */

.py-1 {
    padding: 1.5rem 0;
}

.py-2 {
    padding: 2rem 0;
}

.py-3 {
    padding: 3rem 0;
}


/* All Around Padding */

.p-1 {
    padding: 1.5rem;
}

.p-2 {
    padding: 2rem;
}

.p-3 {
    padding: 3rem;
}

 /*  ======================== Red Block ======================== */


 .red-block {

    height: 472px;
    width: 100%;
    background-color: red;
 }


 /*  ======================== PROJECS COMPLETED  ======================== */

#projects-completed .container .items {

    display: flex;
    justify-content: center;
    flex-wrap: wrap;

}

#projects-completed .container .items .item .circle {

    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}
<div class="red-block">

    <p>red block</p>

</div>



<section id="projects-completed" class="counters bg-yellow-white">
    <div class="container">
        <div class="items">
            <div class="item text-center p-3">
                <div class="circle">
                    <div class="counter l-heading" data-target="1750">500</div>
                </div>
                <h2 class="py-2">Projects Completed</h2>
            </div>
            <div class="item text-center p-3">
                <div class="circle py-2">
                    <div class="l-heading counter" data-target="5">500</div>
                </div>
                <h2 class="py-2">Staff Members</h2>
            </div>
            <!-- <div class="item text-center p-3">
                <div class="circle">
                    <h3 class="l-heading ">1750</h3>
                </div>
                <h2 class="py-2">Projects Completed</h2>
            </div>
            <div class="item text-center p-3">
                <div class="circle py-2">
                    <h3 class="l-heading">5</h3>
                </div>
                <h2 class="py-2">Staff Members</h2>
            </div> -->
        </div>
    </div>
</section>
javascript html css
2个回答
0
投票

尝试getBoundingClientRect()document.querySelector( 'some element' ).getBoundingClientRect()将为您提供特定元素的属性

例如,要知道某个元素是否在用户的屏幕上(在可见的视口中)可见,并调用一个函数,可以使用此功能

    let calledStatus = 0; // some flag variable to remember if function is called
    window.onscroll  = function(){ 
        element =  document.querySelector( '.some element' );
        clientRect = element.getBoundingClientRect();

        if( clientRect.top < window.innerHeight && clientRect.top > ( clientRect.height * -1) && calledStatus == 0){
            //call your function or do other stuff
            console.log('called' )
              calledStatus = 1;

        }
    }

0
投票

如果您希望代码在472 px之后专门启动:

js

$(document).ready(function () {

    $(document).scroll(function () {

        if ($(document).scrollTop() > 472) {
           //call your function here
            console.log( "reached 472")

        }
    });
});

如果您希望函数在到达中间位置后开始文件的您将div放在html代码的中间位置:

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    ...
<div id="middle"></div>
...
</body>
</html>

js

$(document).ready(function () {

    $(document).scroll(function () {

        if ($(document).scrollTop() >=$('#middle').position().top) {
           //call your function here
            console.log( "reached middle")

        }
    });
});
© www.soinside.com 2019 - 2024. All rights reserved.