Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slice jquery to default value

Tags:

javascript

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

How i can reset from slice 0,8 to default when windows greater than 480? Because when resized the slice 0,8 rule still active? I want to default if greater than 480 how can do that?

like image 481
user3327101 Avatar asked Feb 25 '26 02:02

user3327101


1 Answers

Try to use classes. It's much more flexible than .css. You need to remove previous class/styles before you slice and assign new:

if ($(window).width() >= 320 && $(window).width() <= 480) {
    $projects.removeClass('md').slice(0, 8).addClass('sm');
} else if ($(window).width() > 480) {
    $projects.removeClass('sm').slice(3, 6).addClass('md');
};

Demo: http://jsfiddle.net/96Rxg/

Also consider caching $('.projects'), you don't want to select it on each of resize event fire.

like image 76
dfsq Avatar answered Feb 26 '26 14:02

dfsq