Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working on a responsive website. My site uses Bootstrap 3.1 and AngularJS. Bootstrap has a class called "visible-xs". This class basically says let something be visible on small screens and hidden on larger screens. It does this by changing the CSS display property value between none and block. Currently, I have a div that looks like the following:

<span id="smallContent" class="visible-xs">Mobile Content</span>

I need to do some stuff programmatically in my controller when smallContent changes from visible to hidden and vice-versa. My question is, how do I watch for changes on the display property of smallContent? I've noticed the $watch method on the scope (http://docs.angularjs.org/api/ng/type/$rootScope.Scope). However, this seems to watch for changes to a property in the scope, not for changes to a property in the DOM.

Is there a way to do what I'm trying? If so, how?

Thank you!

share|improve this question
    
"I need to do some stuff programmatically in my controller" => what exactly ? –  Bixi Mar 20 at 11:22
    
I have a custom control elsewhere on my page. When the page is on a large screen I need to disable the control (but have it still visible). When the page is on a small screen, I need to ensure the control is enabled. –  JQuery Mobile Mar 20 at 11:24
    
I asked a related question on watching for CSS change here: stackoverflow.com/questions/21693064/… –  mikew Mar 20 at 11:32

1 Answer 1

up vote 0 down vote accepted

You don't need javascript watchers to do what you want. You can, but it would be kind of hacky and potentially bad on performance.

Another point is that "responsiveness" should be handled (a maximum) by HTML/CSS only. If you start having JS different for each resolutions, it's no good.

What you could do :

<span id="smallContent" class="visible-xs">Mobile Content</span>
<span id="smallContent" class="hidden-xs">Not Mobile Content</span>

Keep in mind that you can also simulate media-query in JS with Modernizr :

if (Modernizr.mq('only all and (min-width: 768px)') ) {
}

That can be usefull (you can alos add this to a watcher but, well my answer was primarily CSS-based and you should stick to CSS solutions when possible)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.