最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Removeadd css classes on breakpoint with matchmedia.js - Stack Overflow

programmeradmin1浏览0评论

I have following code which works fine when the screen size is 770px and below (as determined by breakpoints):

    var handleMatchMedia = function (mediaQuery) {
      if (mediaQuery.matches) {

         $j(".view-all a").removeClass("button");
         $j(".view-all").removeClass("view-all");

    } else {

        $j(".view-all a").addClass("button");
        $j(".view-all").addClass("view-all");
    }
},

mql = window.matchMedia('all and (max-width: 770px)');

handleMatchMedia(mql); 
mql.addListener(handleMatchMedia); 

The problem is when the window is resized to 770px and up I lose my classes.

How to achive to change class on window resize?

I have following code which works fine when the screen size is 770px and below (as determined by breakpoints):

    var handleMatchMedia = function (mediaQuery) {
      if (mediaQuery.matches) {

         $j(".view-all a").removeClass("button");
         $j(".view-all").removeClass("view-all");

    } else {

        $j(".view-all a").addClass("button");
        $j(".view-all").addClass("view-all");
    }
},

mql = window.matchMedia('all and (max-width: 770px)');

handleMatchMedia(mql); 
mql.addListener(handleMatchMedia); 

The problem is when the window is resized to 770px and up I lose my classes.

How to achive to change class on window resize?

Share Improve this question edited May 4, 2015 at 10:28 cнŝdk 32.2k7 gold badges60 silver badges80 bronze badges asked Apr 25, 2015 at 13:20 user642523user642523 1514 silver badges16 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

You need to cache your selectors. See jsfiddle as well :

var viewAll = $j(".view-all")
  , buttons = $j(".view-all a")
  , handleMatchMedia = function (mediaQuery) {
     if (mediaQuery.matches) {
       buttons.removeClass("button");
       viewAll.removeClass("view-all");
    } else {
       buttons.addClass("button");
       viewAll.addClass("view-all");
    }
  }
  , mql = window.matchMedia('all and (max-width: 770px)');

handleMatchMedia(mql); 
mql.addListener(handleMatchMedia); 

Guessing what you're going for is changing the design page when media changes, by adding classes.

Simply using css and media queries will achieve this:

@media all and (max-width: 770px) {
  .viewall a {
    color: blue;
  }
}

but if you truly want it too be handled with javascript I'll remend using another class as marker, like .adapt and changing the code to:

var handleMatchMedia = function (mediaQuery) {
    if (mediaQuery.matches) {
       $j(".adapt a").removeClass("button");
       $j(".adapt").removeClass("view-all");
    } else {
       $j(".adapt a").addClass("button");
       $j(".adapt").addClass("view-all");
    }
},

mql = window.matchMedia('all and (max-width: 770px)');

handleMatchMedia(mql); 
mql.addListener(handleMatchMedia); 

I would suggest to save the needed classes in a data-770-classes attribute.

if (mediaQuery.matches) {
   buttons.removeClass(buttons.attr('data-770-classes'));
   viewAll.removeClass(viewAll.attr('data-770-classes'));
} else {
   buttons.addClass(buttons.attr('data-770-classes'));
   viewAll.addClass(viewAll.attr('data-770-classes'));
}

I assume $j creates a jQuery object.

The HTML would look like:

<div class="view-all" data-700-classes="view-all">...</div>

You can use element.className += "button" to add class and .className = ""to remove class, here's the code you need:

var viewAll = document.getElementsByClassName("view-all")[0];
var buttons = viewAll.getElementsByTagName("a");
var handleMatchMedia = function (mediaQuery) {
  if (mediaQuery.matches) {
     buttons.className += "button";
     viewAll.className = "";
  } else {
     buttons.className += "button";
     viewAll.className += "view-all";
  }
}
var mql = window.matchMedia('all and (max-width: 770px)');

handleMatchMedia(mql); 
mql.addListener(handleMatchMedia); 
发布评论

评论列表(0)

  1. 暂无评论