3

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

2
  • 2
    what about a variable as a flag, that is setted to true inside start(), then inside the scroll() you check if this flag is false before run start()? Commented Sep 16, 2019 at 17:19
  • my final fiddle: jsfiddle.net/eu8ab9Lq/1 Commented Sep 17, 2019 at 5:31

5 Answers 5

6

You can setup a flag like:

var started = false;
function start() {
  if(!started) {
    alert("hello");
  }
  started = true;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<br />

<center>
    
  <br />
  <h1>scroll down</h1>
  </center>

<div class="box"></div>

Comments

0

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');
});

Comments

0

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);
        }
    }

  });

Comments

0

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.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="box"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.