简体   繁体   中英

Detect screen resolution and modify layout

I need to check whether user's resolution is <1300px or >1300px and if it is <1300px to load layout with horizontal divs and if it is >1300px to load layout with vertical divs.

Template is the same I only need to put one div in horizontal or vertical position depening on resolution.

Since I cannot do it with PHP, I have to do it with javascript, so I would have to use "load" function to load part of my template where is vertical or horizontal div? I'm using Yii framework

Typically this sort of thing is in the CSS domain (via media queries ), not the JavaScript domain.

You can use the screen object , though, to get the dimensions of the user's screen:

var width = screen.width; // Or screen.availWidth;

You can use CSS media query for that:

// For screen < 1300px
@media screen and (max-width: 1300px) {
    .foo {
        ...
    }
}

// For screen > 1300px
@media screen and (min-width: 1300px) {
    .foo {
        ...
    }
}

In JavaScript you can detect it by:

window.screen.availHeight
window.screen.availWidth

while in CSS you need to use media queries :

@media screen and (min-width: 1200px)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM