简体   繁体   English

更改移动设备的网站设计或浏览器调整大小

[英]Change website design for mobile devices or browser resize

Im sorry if this has been asked before im sure somewhere there must be an answer for this but for some or other reason I cant find it, probably using wrong search query 我很抱歉,如果这个问题已被问到我确定某处必须有一个答案,但由于某些或其他原因我无法找到它,可能使用错误的搜索查询

I know you can use media queries to target different devices like so: 我知道您可以使用媒体查询来定位不同的设备,如下所示:

@media only screen and (max-device-width: 480px) {
    div#wrapper {
        width: 400px;
    }
}

But what I would like to know is, how to change my websites design based on device it is viewed on? 但我想知道的是,如何根据所查看的设备更改我的网站设计?

Example

Lets say my normal site structure is like this: 让我们说我的正常网站结构是这样的:

if desktop 如果桌面

<div id="user">
    <div id="profilePic">
        <img src="images/responSiveTest/ppic.PNG" class="p-img" />
    </div>
    <div id="uname">
        <h4 style="color:white">Welcome  Guest</h4>
        <p style="color:white">Please Log in</p>
    </div>
</div>

Now when user views my site on a mobile device how do I change my div / site layout, lets say to something different like this 现在,当用户在移动设备上查看我的网站时,如何更改我的div /网站布局,让我们说一些不同的东西

if Mobile device 如果移动设备

<div id="mobileNav">
    <div id="namePic">
        <!-- Mobile user -->
    </div>
</div>

Hope what im asking makes sense, thanks to everyone on this amazing site for helping 希望我的要求是有道理的,感谢这个神奇的网站上的每个人的帮助

I usually do this in my projects. 我通常在我的项目中这样做。 I prepare content by default but its set to display: none, when media query detects the mobile device width it will render appropriate content for the device. 我默认准备内容,但设置为display:none,当媒体查询检测到移动设备宽度时,它将为设备呈现适当的内容。

with CSS Media Query 使用CSS Media Query

HTML HTML

<div id="content">
    <div class="desktop">
        <!--
            some content and markups here. by default this is loaded in desktop
        -->
    </div>

    <div class="mobile_device_380px">
        <!-- content and some markups for mobile -->
    </div>

    <div class="mobile_device_480px">
        <!-- content and some markups for mobile -->
    </div>
</div>

CSS CSS

  /* if desktop */
    .mobile_device_380px {
        display: none;
    }
    .mobile_device_480px {
        display: none;
    }


    /* if mobile device max width 380px */
    @media only screen and (max-device-width: 380px) {
        .mobile_device_380px{display: block;}       
        .desktop {display: none;}
    } 

    /* if mobile device max width 480px */
    @media only screen and (max-device-width: 480px) {
       .mobile_device_480px{display: block;}
       .desktop {display: none;}
    }


Take note your page might take long to load. 请注意,您的页面可能需要很长时间才能加载 just limit what you need to change and be specific. 只是限制你需要改变和具体的东西。

If you want to do this with pure JavaScript, here it is- 如果你想用纯JavaScript做到这一点,这里是 -

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
// some code.. 
}

Again you can do this with jquery- 再次,您可以使用jquery-

$.browser.device = (/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(navigator.userAgent.toLowerCase()));

If you want to do this with PHP the check out this article 如果你想用PHP做这个, 请查看这篇文章

You can use this module 您可以使用此模块

To detect a mobile device such as phone or tablet you can use this code. 要检测手机或平板电脑等移动设备,您可以使用此代码。

require_once '../Mobile_Detect.php';
$detect = new Mobile_Detect;
$deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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