Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

JQuery distinct between smartphones, tablet or computers with different sizes but the same $(window).width()

I got a problem I would like to solve trying to differentiate between different devices.

The point is I have 3 different devices to test my code:

- Moto G (3rd gen) (Smartphone):
    - Width screen: 6'3 cm
    - Height screen: 11 cm
    - Resolution: 720 x 1280
- Huawei T1-701w (Small tablet):
    - Width screen: 9 cm and
    - Height screen: 15'3 cm
    - Resolution: 600 x 1200
- Samsung sm-t580 (Big Tablet):
    - Width screen: 13'8 cm
    - Height screen : 21'9 cm
    - Resolution: 800 x 1280

I measured with my hands the cm of width and height and searched it in internet in the specifications from each device the resolution in px of the screen.

But all 3 devices in the browsers have the same width in pixels (980 px in the vertical orientation that I get from "alert($(window).width());"). So I can't distinct between an smartphone and the rest of devices.

Obviously the three devices have a clearly different size and I would like to apply different rules of css for each one but I can't find a solution.

Any idea about what I'm doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You will have to use innerWidth instead of width property for that purpose.

    $window.innerWidth 

or try this

    $window.screen.width

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...