admin管理员组文章数量:1353593
I want to start a function when a div is scrolled into the viewport. My problem is, that the function is triggered/started again every time I continue to scroll.
HTML:
<div class="box"></div>
JS:
$(document).ready(function() {
function start() {
alert("hello");
}
$(window).scroll(function() {
if ( $(window).scrollTop() >= $('.box').offset().top - ($(window).height() / 2)) {
$(".box").addClass("green");
start();
} else {
$(".box").removeClass("green");
}
});
});
To sum up: the function "start" should be started, when the div is scrolled into the viewport. But it should be not triggered again, after it was triggered once.
Fiddle
I want to start a function when a div is scrolled into the viewport. My problem is, that the function is triggered/started again every time I continue to scroll.
HTML:
<div class="box"></div>
JS:
$(document).ready(function() {
function start() {
alert("hello");
}
$(window).scroll(function() {
if ( $(window).scrollTop() >= $('.box').offset().top - ($(window).height() / 2)) {
$(".box").addClass("green");
start();
} else {
$(".box").removeClass("green");
}
});
});
To sum up: the function "start" should be started, when the div is scrolled into the viewport. But it should be not triggered again, after it was triggered once.
Fiddle
Share Improve this question asked Sep 16, 2019 at 17:16 Maximilian NeuseMaximilian Neuse 1631 gold badge5 silver badges15 bronze badges 2-
2
what about a variable as a flag, that is setted to true inside
start()
, then inside thescroll()
you check if this flag is false before runstart()
? – Calvin Nunes Commented Sep 16, 2019 at 17:19 - my final fiddle: jsfiddle/eu8ab9Lq/1 – Maximilian Neuse Commented Sep 17, 2019 at 5:31
5 Answers
Reset to default 6You can setup a flag like:
var started = false;
function start() {
if(!started) {
alert("hello");
}
started = true;
}
Demo
$(document).ready(function() {
var started = 0;
function start() {
if(started==0) {
alert("Alert only once");
}
started = 1;
}
$(window).scroll(function() {
if ( $(window).scrollTop() >= $('.box').offset().top - ($(window).height() / 2)) {
$(".box").addClass("green");
start();
} else {
$(".box").removeClass("green");
}
});
});
*{margin:0;}
.box { background: red; height: 200px; width: 100%; margin: 800px 0 800px 0; }
.green { background: green; }
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<br />
<center>
<br />
<h1>scroll down</h1>
</center>
<div class="box"></div>
There's a bunch of ways to go about this. You could just remove the event listener (since you're using jQuery, I'll use the on
and off
methods):
$(window).on('scroll', function() {
if ( $(window).scrollTop() >= $('.box').offset().top - ($(window).height() / 2)) {
$(".box").addClass("green");
start();
} else {
$(".box").removeClass("green");
}
$(window).off('scroll');
});
if you want window scroll method to stop once the start method meet its requirement.. you can do it this way
$(document).ready(function() {
var toggleScroll = false;
function start() {
alert("hello");
}
$(window).one("scroll", checkToggleScroll);
function checkToggleScroll(){
if ( $(window).scrollTop() >= $('.box').offset().top - ($(window).height() / 2)) {
$(".box").addClass("green");
toggleScroll = true;
start();
} else {
$(".box").removeClass("green");
}
if(!toggleScroll){
$(window).one("scroll", checkToggleScroll);
}
}
});
Just run the start()
function when the $(".box)
doesn't have the class "green"
(that is added after a certain amount of scroll).
$(document).ready(function() {
function start() {
alert("hello");
}
$(window).scroll(function() {
if ($(window).scrollTop() >= $('.box').offset().top - ($(window).height() / 2)) {
if (!$(".box").hasClass("green")) {
$(".box").addClass("green");
start();
}
} else {
$(".box").removeClass("green");
}
});
});
.box {
background: red;
height: 200px;
width: 100%;
margin: 800px 0 800px 0;
}
.green {
background: green;
}
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="box"></div>
本文标签: javascriptTrigger function only once on scrollStack Overflow
版权声明:本文标题:javascript - Trigger function only once on scroll - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743889829a2556717.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论