Aman Sharma
Aman Sharma

Reputation: 11

how to use scroll, when i resize the window

I want to use scoll images or div's when i was resize the window, but it actually does not work. the only the part of scoll is working when i write it seperately. Anyone here to help?

$(window).resize(function() {
       if ($(window).width() >= 767) {
         $(window).scroll(function() {

           $(".careers-philosophy__image").css({
             "bottom": ($(window).scrollTop()/15) + "px"
           });

           $(".careers-philosophy__image2").css({
             "bottom": ($(window).scrollTop()/25) + "px"
           });

           $(".carrer-block").css({
             "bottom": ($(window).scrollTop()/10) + "px"
           });

           $(".fast").css({
             "bottom": ($(window).scrollTop()/5) + "px"
           });

         });
       }
    });

Upvotes: 2

Views: 389

Answers (1)

Hao Wu
Hao Wu

Reputation: 20867

You shouldn't put $(window).scroll() in an event handler since itself is alos an event registration.

If you need the handler to be triggered on both resize and scroll, you can write it like this:

function handler(){
    if ($(window).width() >= 767) {
        $(".careers-philosophy__image").css({
            "bottom": ($(window).scrollTop() / 15) + "px"
        });

        $(".careers-philosophy__image2").css({
            "bottom": ($(window).scrollTop() / 25) + "px"
        });

        $(".carrer-block").css({
            "bottom": ($(window).scrollTop() / 10) + "px"
        });

        $(".fast").css({
            "bottom": ($(window).scrollTop() / 5) + "px"
        });
    }
}

$(window).resize(handler);
$(window).scroll(handler);

Upvotes: 4

Related Questions