Domanda

This jQuery code is working well, but I want to implement a fixed point, I want to replace the 510 with a relative point, from css: #content

var _rys = jQuery.noConflict();
            _rys("document").ready(function () {
                _rys(window).scroll(function () {
                    if (_rys(this).scrollTop() > 510) {
                        _rys('.navigation').addClass("fixed");
                    } else {
                        _rys('.navigation').removeClass("fixed");
                    }
                });
            });

I converted this code, but its not working, and I did not realize why :) Thanks for the help in advance.

jQuery(document).ready(function() {
var aboveHeight = $('header').outerHeight();
$(window).scroll(function(){
        if ($(window).scrollTop() > aboveHeight){
              $('.navigation').addClass("fixed");
                    }
        else {
              $('.navigation').removeClass("fixed");
                    }
        });
    });

The working version: thanks Dinesh Kumar DJ

jQuery(window).load(function() {
var aboveHeight = jQuery('#content').offset().top;
            console.log(jQuery('#content'));
jQuery(document).scroll(function(){
    if (jQuery(window).scrollTop() > aboveHeight){
      jQuery('.navigation').addClass("fixed");
    } else {
      jQuery('.navigation').removeClass("fixed");
    }
});   });
È stato utile?

Soluzione

Replace all $ by jQuery, it will work fine,

jQuery(document).ready(function() {
    var aboveHeight = jQuery('header').outerHeight();
    jQuery(window).scroll(function(){
        if (jQuery(window).scrollTop() > aboveHeight){
          jQuery('.navigation').addClass("fixed");
        } else {
          jQuery('.navigation').removeClass("fixed");
        }
    });
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top