I got a question regarding a function that will be called if the object is within my screen. But when the object is within my screen the function is been called and a alert
Let's try to solve your problem in javascript as all of the answers here are in jquery. You can use global variable as browser retains its value as long as the page is not refreshed. All those variables declared outside the function are called global variables and can be accessed by any function.
window.onscroll = myScroll;
var counter = 0; // Global Variable
function myScroll(){
var val = document.getElementById("value");
val.innerHTML = 'pageYOffset = ' + window.pageYOffset;
if(counter == 0){ // if counter is 1, it will not execute
if(window.pageYOffset > 300){
alert('You have scrolled to second div');
counter++; // increment the counter by 1, new value = 1
}
}
}
#wrapper,#tester {
width: 300px;
height: 300px;
border: 1px solid black;
padding: 10px;
}
#wrapper p {
text-align: center;
}
#tester {
border: 1px solid crimson;
}
#value {
position: fixed;
left: auto;
right: 40px;
top: 10px;
}
scroll down to div to see the alert