Code inside attached behavior not executed

by LaPriWa   Last Updated July 11, 2019 15:07 PM - source

I nested jQuery code inside a behavior. I don't understand, why the code does not execute. I would be appleased if you could give me a hint on what I probably did wrong.

My code:

$(function (){
    Drupal.behaviors.block_sidebar_register_buttons = {
        attach: function (context, settings) {
            var element = $('#block_register_buttons');
            if (element != undefined && $.trim(element) != "") {
                if (element.offset() != undefined) {
                    $('#block_register_buttons').children('.btn').css('margin', 0);
                    var originalY = element.offset().top;
                    var topMargin = $('#main-menu-navigation').outerHeight() + 50;
                    var maxScroll = $(document).height() - $(window).height();
                    var totalHeightOfFooter = $('#footer').outerHeight();
                    var heightOfMyElement = element.outerHeight();
                    $(window).on('scroll', function (event) {
                        var windowSize = $(window).width();
                        var scrollTop = $(window).scrollTop();
                        if (windowSize > 767) {
                            if ((maxScroll - totalHeightOfFooter) - (heightOfMyElement / 4) > scrollTop) {
                                element.stop(false, false).animate({
                                    top: scrollTop < originalY ? 0 : scrollTop - originalY + topMargin
                                }, 100);
                            }
                        }
                    });
                }
            }
        }
    };
});
Tags : javascript


Related Questions


Use drupal.settings variables on javascript

Updated March 27, 2015 14:18 PM

Passing Javascript variable in my module file

Updated July 27, 2015 14:03 PM

Remove jquery script

Updated April 20, 2015 21:03 PM

Calling a contact form in a certain page

Updated August 23, 2015 17:03 PM