简体   繁体   中英

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"
           });

         });
       }
    });

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM