For making a div fixed on scroll down

For making a div fixed on scroll down and return to its previous form on scroll up to its previous position

by Jithin Raj P R

JavaScript

// For making a div fixed on scroll down and return to its previous form on scroll up to its previous position
var oGtop = 0,
    elWidth = 0;

function stopScroll() {
    var vtop = $(window).scrollTop();
    $('.ux-side-sticky').each(function() {
        var animElem = $(this),
            elTop = animElem.offset().top;
        if (oGtop == 0) {
            oGtop = elTop;
            elWidth = animElem.outerWidth();
        }
        if ((vtop + 40 >= elTop)) {
            animElem.addClass('scroll-stop');
            animElem.outerWidth(elWidth);
        } else if (oGtop >= elTop) {
            animElem.removeClass('scroll-stop');
        }
    });
};
if ($('.ux-side-sticky').length != '') {
    $(window).on('scroll resize', stopScroll);
}