0

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!

3
  • "I need to do some stuff programmatically in my controller" => what exactly ? Commented Mar 20, 2014 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. Commented Mar 20, 2014 at 11:24
  • I asked a related question on watching for CSS change here: stackoverflow.com/questions/21693064/… Commented Mar 20, 2014 at 11:32

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.