简体   繁体   中英

resize function with window width jQuery

  if ($(window).width() >= 320 && $(window).width() <= 480) {
    $(".projects").slice(1, 8).css("margin", "10px");
  } else {
    $(".projects").slice(3, 6).css("margin", "10px");
  };

Its working good with default without resize finction. I try set this with:

 $(window).resize(function() {           
 })

But not working. Any idea why?

JSFIDDLE

You are never resetting the div margins. Therefore, as soon as it get to the point where slice 1 through 8 have margins, they will never get changed back. You need to reset the divs:

$(window).resize(function () {
    $(".projects").css('margin', '0px'); // reset the divs
    if ($(window).width() >= 320 && $(window).width() <= 480) {
        $(".projects").slice(1, 8).css("margin", "10px");
    } else {
        $(".projects").slice(3, 6).css("margin", "10px");
    };
});

Here is Fiddle

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