JQuery Detect class changes

前端 未结 3 621
北荒
北荒 2020-12-05 10:41

I am using a plugin that added a class open to .slide-out-div when opened.

So I am trying to change some css if the open is detected.

3条回答
  •  一整个雨季
    2020-12-05 11:30

    The question's a bit old, but since I came across while looking for a similar problem, thought I'd share the solution I went with here - Mutation Observers

    In your case, I'd create a mutation observer

    var mut = new MutationObserver(function(mutations, mut){
      // if attribute changed === 'class' && 'open' has been added, add css to 'otherDiv'
    });
    mut.observe(document.querySelector(".slide-out-div"),{
      'attributes': true
    });
    

    The function in mutation observer is called any time an attribute of .slide-out-div is changed, so need to verify the actual change before acting.

    More details here on Mozilla's documentation page

提交回复
热议问题