Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

I need to make Jquery plugin work with a media query

On the website i am developing i use a jQuery image zoom plugin, but i need to make it work only when viewport with is over 940px.

so this is my js code:

$(window).resize(function(){ if ($(window).width() >= 940){
$("#zoom").elevateZoom({ zoomType : "inner", cursor: "crosshair" }); }
});

It is working, but only after i resize my browser window. What could be the problem?

Never mind... without the resize function everything works fine!

1 Answer

Steven Parker
Steven Parker
243,318 Points

Do you need it to run when the window loads and when it is resized? You might define the function separately and both call it once immediately and also establish it as the resize event handler.