JS matchMedia method()

Greek islands

The Greek islands are a famous world destination and an integral part of the culture of Greece throughout the centuries. Greek has about 6,000 islands scattered in the Greek Seas, of which only 227 are inhabited. The Greek islands offer a diverse landscape: beaches stretching over many kilometers, sheltered bays and caves, and sandy and pebble beaches. Some of the most popular Greek islands are Santorini, Mykonos, Crete, Zakynthos, Corfu, Thasos, and Lefkada.

Lefkada
Corfu
Thasos
Mykonos

Island

Crete
Zakynthos
Santorini

In this project the matchMedia method is used to turn on and off the background image for the image gallery at different screen sizes (below and above 768px). The matchMedia method is a JavaScript method that allows us to work with CSS media queries directly in JavaScript when we need functions that should do different things at different screen sizes on different devices or in different browsers. The matchMedia method accepts one parameter as its value, which is the media query to be checked. A call to this method returns a MediaQueryList object which contains information on the media query.

It contains two properties: media, which stores the media query as a serialized string; matches, a boolean value of either true or false, depending on whether the media query is matched. By checking whether the matches property has a true or false value, we can implement functions that do different things for different screen sizes. In order to detect the change in screen size, we can use an event listener. We can add a change event to the MediaQueryList object using the addEventListener method.