I'm attempting to use the .on() from jQuery to catch a scroll event that is inside a tag.
so this was my solution:
getScrollTop() is a javascript function to return the top value (works)
$(document).on("scroll#popup", '#popup', function(){
alert('scrolling');
$(".fixedHeader").css("position", "relative");
$(".fixedHeader").css("top", getScrollTop());
});
jQuery scroll() MethodThe scroll event occurs when the user scrolls in the specified element. The scroll event works for all scrollable elements and the window object (browser window). The scroll() method triggers the scroll event, or attaches a function to run when a scroll event occurs.
Use the . scroll() event on window , like this: $(window). scroll(function() { if($(window).
The scroll() method is used to trigger the scroll event or attach a function to run when scrolling occurs. The scroll event occurs when a scrollbar is used for an element. The event is triggered when the user moves the scrollbar up or down. We can use the CSS overflow property for creating a scrollbar.
The confusion is in how "on()" works. In jQuery when you say $(document).on(xx, "#popup", yy) you are trying to run yyy whenever the xx event reaches document but the original target was "#popup".
If document is not getting the xx event, then that means it isn't bubbling up! The details are in the jQuery On documentation, but the three events "load", "error" and "scroll" do not bubble up the DOM.
This means you need to attach the event directly to the element receiving it. $("#popup").on(xx,yy);
The event is simply scroll
, not scroll#popup
.
// http://ejohn.org/blog/learning-from-twitter
// Also, be consistent with " vs '
var $fixedHeader = $('.fixedHeader').css('position', 'relative');
$(document).on('scroll', '#popup', function() {
console.log('scrolling'); // you *really* don't want to alert in a scroll
$fixedHeader.css("top", getScrollTop());
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With