0

I want to do something when window scrolls to a certain position

Fiddle : https://jsfiddle.net/to1uuvnb/16/

HTML:

<div id="container"></div>
<div id="target"></div>

CSS:

#container {
    min-height:300px;
    max-width:400px;
    background-color:#000;
}
#target { 
    min-height:1000px;
    max-width:400px;
    background-color:red;
}

JavaScript:

$('#target').scroll(function() {
    if($(this).scrollTop() > 10) {
        alert('');
    }
});

1 Answer 1

1

You are attaching the scroll event to a div that has no overflow: auto; or overflow: scroll; set. And the question title does not seem to match with your code, I believe you want to attach the event to window instead:

$(window).scroll(function() {

https://jsfiddle.net/to1uuvnb/17/

Sign up to request clarification or add additional context in comments.

3 Comments

can you please update the fiddle code it will be easy for me
i just want when the scrolls cross the black container then it shows alert how can we do that
i can't use (window).scroll() in my project cause there are too many div elements in my project and if i use window.scroll() then i have to give it top spacing from the starting of the page

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.