【问题标题】:How do I customize the screen definition?如何自定义屏幕定义?
【发布时间】:2018-10-21 13:01:34
【问题描述】:

美好的一天!

在网络上找到了一个确定屏幕大小的脚本,但我不知道如何正确设置isMobile、isTablet、isDesktop的定义。

function() {
    var i = {
        mobile: "mobile",
        tablet: "tablet",
        desktop: "desktop",
        oldHeight: null,
        oldWidth: null,
        current: function() {
            return this.define()
        },
        isMobile: function() {
            return this.define() === this.mobile
        },
        isTablet: function() {
            return this.define() === this.tablet
        },
        isDesktop: function() {
            return this.define() === this.desktop
        },
        define: function() {
            return this.width() < 768 ? this.mobile : this.width() < 1360 ? this.tablet : this.desktop
        },
        height: function() {
            return window.innerHeight
        },
        width: function() {
            return window.innerWidth
        },
        init: function() {
            this.oldHeight = window.innerHeight, this.oldWidth = this.define()
        }
    };

请告诉我...

现在我用这个方法:

if(
    (screen.width <= 640) || 
    (window.matchMedia && 
     window.matchMedia('only screen and (max-width: 640px)').matches
    )
  ){
   // Do the mobile thing
}

但我认为上面的脚本更通用。

附:能吃更正确的定义屏幕大小的方法吗?

谢谢

【问题讨论】:

    标签: javascript function screen matchmedia


    【解决方案1】:

    你如何调用isMobile 方法?如果您删除外部匿名function 并调用i.isMobile() 之类的方法,它似乎工作正常 - 对我来说,当我最大化浏览器时报告false,如果我将浏览器缩小到小于 768px 宽,则为 true ...

    【讨论】:

    • 我明白了,谢谢。在这里它可以工作: if (i.isMobile()) { pageMain.classList.add("main--hide"); }
    • 如果这个答案对你有帮助,请考虑选择它作为接受的答案,或者给它一个upvote
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多